Most frequent k chars distance: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: Point out a serious flaw)
(+J (see Talk page for assumptions required to reconcile task description, test cases, and existing implementations))
Line 143: Line 143:
</pre>
</pre>
=Implementations=
=Implementations=

=={{header|J}}==
'''Solution''':<lang j>NB. String Distance Wrapper Function
mfksDF =: {:@:[ - (mfks@:(mfkh&.>)~ {.)~

NB. Most Frequent K Distance
mfks =: score@:(charMap@:[ {"1 charVals@:])/@:kHashes
score =. ([ +/ .* =)/ NB. (+ +/ .* *.&:*)/ for sum += freq_in_left + freq_in_right
charMap =. (,&< i.&> <@:~.@:,)&;/
charVals =. (; , 0:)"1
kHashes =. 0 1 |: ({.&>~ [: <./ #&>)
NB. Most Frequent K Hashing
mfkh =: _&$: : (takeK freqHash) NB. Default LHA of _ means "return complete frequency table"
takeK =. (<.#) {. ]
freqHash =. ~. (] \:~ ,.&:(<"0)) #/.~

NB. No need to fix mfksDF
mfkh =: mfkh f.
mfks =: mfks f.</lang>

'''Examples''':<lang j>verb define ''
fkh =. ;@:,@:(":&.>) NB. format k hash

assert. 'r2e2 e2s1' (-: [: fkh 2&mfkh)&>&;: 'research seeking'
assert. 2 = mfks 2 mfkh&.> 'research';'seeking'

assert. 'n1i1 n1a1' (-: [: fkh 2&mfkh)&>&;: 'night nacht'
assert. 9 = 2 10 mfksDF 'night';'nacht'

assert. 'm1y1 a1' (-: [: fkh 2&mfkh)&>&;: 'my a'
assert. 10 = 2 10 mfksDF 'my';'a'

assert. 'r2e2' -: fkh 2 mfkh 'research'
assert. 6 = 2 10 mfksDF 'research';'research' NB. task says 8; right answer is 6

assert. 'a5b4 a5b4' (-: [: fkh 2&mfkh)&>&;: 'aaaaabbbb ababababa'
assert. 1 = 2 10 mfksDF 'aaaaabbbb';'ababababa'

assert. 'i3n2 i3a2' (-: [: fkh 2&mfkh)&>&;: 'significant capabilities'
assert. 7 = 2 10 mfksDF 'significant';'capabilities' NB. task says 5; right answer is 7

assert. 'L9T8 F9L8' (-: [: fkh 2&mfkh)&>&;: 'LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG'
assert. 100 = 2 100 mfksDF 'LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV';'EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG'
NB. task says 83; right answer is 100

'pass'
)
pass</lang>
'''Notes''': As of press time, there are significant discrepancies between the task description, its pseudocode, the test cases provided, and the two other existing implementations. See the [[Talk:Most frequent k chars distance#Prank_Page.3F|talk page]] for the assumptions made in this implementation to reconcile these discrepancies (in particular, in the scoring function).



=={{header|Java}}==
=={{header|Java}}==
{{incorrect|Java|This will fail catastrophically on “<tt>ABACADAEAFAEADACABA</tt>” as that has 10 ‘<tt>A</tt>’ characters in it.}}
{{incorrect|Java|This will fail catastrophically on “<tt>ABACADAEAFAEADACABA</tt>” as that has 10 ‘<tt>A</tt>’ characters in it.}}