Jump to content

Most frequent k chars distance: Difference between revisions

(Added Go)
Line 831:
<pre>Bag(i2n2(151))</pre>
One... Nope, I was right, it ''is'' pretty much worthless.
 
=={{header|Phix}}==
{{trans|Go}}
<lang Phix>function mostFreqKHashing(string input, integer k)
string cfs = ""
sequence cfsnx = {}
for i=1 to length(input) do
integer r = input[i],
ix = find(r,cfs)
if ix>0 then
cfsnx[ix][1] -= 1
else
cfs &= r
cfsnx = append(cfsnx,{-1,length(cfs)})
end if
end for
cfsnx = sort(cfsnx) -- (aside: the idx forces stable sort)
sequence acc := {}
for i=1 to min(length(cfs),k) do
integer {n,ix} = cfsnx[i]
acc &= {cfs[ix], -n}
end for
return acc
end function
function mostFreqKSimilarity(sequence input1, input2)
integer similarity := 0
for i=1 to length(input1) by 2 do
for j=1 to length(input2) by 2 do
if input1[i] == input2[j] then
integer freq1 = input1[i+1],
freq2 = input2[j+1]
if freq1=freq2 then
similarity += freq1
end if
end if
end for
end for
return similarity
end function
 
function flat(sequence s)
string res = ""
for i=1 to length(s) by 2 do
res &= sprintf("%c%d",s[i..i+1])
end for
return res
end function
 
procedure mostFreqKSDF(string input1, input2, integer k, maxDistance)
printf(1,"input1 : %s\n", {input1})
printf(1,"input2 : %s\n", {input2})
sequence s1 := mostFreqKHashing(input1, k),
s2 := mostFreqKHashing(input2, k)
printf(1,"mfkh(input1, %d) = %s\n", {k,flat(s1)})
printf(1,"mfkh(input2, %d) = %s\n", {k,flat(s2)})
integer result := maxDistance - mostFreqKSimilarity(s1, s2)
printf(1,"SDF(input1, input2, %d, %d) = %d\n\n", {k, maxDistance, result})
end procedure
constant tests = {{"research", "seeking"},
{"night", "nacht"},
{"my", "a"},
{"research", "research"},
{"aaaaabbbb", "ababababa"},
{"significant", "capabilities"}}
for i=1 to length(tests) do
string {t1,t2} = tests[i]
mostFreqKSDF(t1, t2, 2, 10)
end for
string s1 := "LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV",
s2 := "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG"
mostFreqKSDF(s1, s2, 2, 100)
s1 = "abracadabra12121212121abracadabra12121212121"
s2 = reverse(s1)
mostFreqKSDF(s1, s2, 2, 100)</lang>
Output matches Go and Kotlin.
 
=={{header|Python}}==
7,818

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.