Most frequent k chars distance: Difference between revisions

Added Kotlin
m (→‎{{header|Sidef}}: updated code)
(Added Kotlin)
Line 437:
 
}</lang>
 
=={{header|Kotlin}}==
The code for the MostFreqKSimilarity function differs in this task from that in the associated Wikipedia article. Also the description is inconsistent with the code in both cases.
 
What I've decided to assume is that the frequency for commonly occurring characters must be the same in both strings for it to be added to the 'similarity' variable which seems to be the implication of both descriptions. This gives the same results as the Wikipedia article for all except the last example where it gives 100 rather than 83.
 
It's also evident that you can't just add the frequency of each character to the output string of the MostFreqKHashing function and then expect to be able to parse it afterwards. This is because, in the general case, any printing characters (including digits) could occur in the input string and the frequency could be more than 9. I've therefore encoded the frequency as the character with the same unicode value rather than the frequency itself.
 
I have no idea what NULL0 is supposed to mean so I've ignored that.
<lang scala>// version 1.1.51
 
fun mostFreqKHashing(input: String, k: Int): String =
input.groupBy { it }.map { Pair(it.key, it.value.size) }
.sortedByDescending { it.second } // preserves original order when equal
.take(k)
.fold("") { acc, v -> acc + "${v.first}${v.second.toChar()}" }
 
fun mostFreqKSimilarity(input1: String, input2: String): Int {
var similarity = 0
for (i in 0 until input1.length step 2) {
for (j in 0 until input2.length step 2) {
if (input1[i] == input2[j]) {
val freq1 = input1[i + 1].toInt()
val freq2 = input2[j + 1].toInt()
if (freq1 != freq2) continue // assuming here that frequencies need to match
similarity += freq1
}
}
}
return similarity
}
 
fun mostFreqKSDF(input1: String, input2: String, k: Int, maxDistance: Int) {
println("input1 : $input1")
println("input2 : $input2")
val s1 = mostFreqKHashing(input1, k)
val s2 = mostFreqKHashing(input2, k)
print("mfkh(input1, $k) = ")
for ((i, c) in s1.withIndex()) print(if (i % 2 == 0) c.toString() else c.toInt().toString())
print("\nmfkh(input2, $k) = ")
for ((i, c) in s2.withIndex()) print(if (i % 2 == 0) c.toString() else c.toInt().toString())
val result = maxDistance - mostFreqKSimilarity(s1, s2)
println("\nSDF(input1, input2, $k, $maxDistance) = $result\n")
}
 
fun main(args: Array<String>) {
val pairs = listOf(
Pair("research", "seeking"),
Pair("night", "nacht"),
Pair("my", "a"),
Pair("research", "research"),
Pair("aaaaabbbb", "ababababa"),
Pair("significant", "capabilities")
)
for (pair in pairs) mostFreqKSDF(pair.first, pair.second, 2, 10)
 
var s1 = "LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV"
var s2 = "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG"
mostFreqKSDF(s1, s2, 2, 100)
s1 = "abracadabra12121212121abracadabra12121212121"
s2 = s1.reversed()
mostFreqKSDF(s1, s2, 2, 100)
}</lang>
 
{{out}}
<pre>
input1 : research
input2 : seeking
mfkh(input1, 2) = r2e2
mfkh(input2, 2) = e2s1
SDF(input1, input2, 2, 10) = 8
 
input1 : night
input2 : nacht
mfkh(input1, 2) = n1i1
mfkh(input2, 2) = n1a1
SDF(input1, input2, 2, 10) = 9
 
input1 : my
input2 : a
mfkh(input1, 2) = m1y1
mfkh(input2, 2) = a1
SDF(input1, input2, 2, 10) = 10
 
input1 : research
input2 : research
mfkh(input1, 2) = r2e2
mfkh(input2, 2) = r2e2
SDF(input1, input2, 2, 10) = 6
 
input1 : aaaaabbbb
input2 : ababababa
mfkh(input1, 2) = a5b4
mfkh(input2, 2) = a5b4
SDF(input1, input2, 2, 10) = 1
 
input1 : significant
input2 : capabilities
mfkh(input1, 2) = i3n2
mfkh(input2, 2) = i3a2
SDF(input1, input2, 2, 10) = 7
 
input1 : LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV
input2 : EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG
mfkh(input1, 2) = L9T8
mfkh(input2, 2) = F9L8
SDF(input1, input2, 2, 100) = 100
 
input1 : abracadabra12121212121abracadabra12121212121
input2 : 12121212121arbadacarba12121212121arbadacarba
mfkh(input1, 2) = 112a10
mfkh(input2, 2) = 112210
SDF(input1, input2, 2, 100) = 88
</pre>
 
=={{header|Perl}}==
9,477

edits