Most frequent k chars distance: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 146:
{{trans|Python}}
<
DefaultDict[Char, Int] occuDict
L(c) inputString
Line 175:
print(dict2, end' ":\n")
print(dict2.map((c, cnt) -> c‘’String(cnt)).join(‘’))
print(most_freq_ksdf(str1, str2, K, maxDistance))</
{{out}}
Line 187:
=={{header|C++}}==
<
#include <vector>
#include <map>
Line 242:
return 0 ;
}
</syntaxhighlight>
{{out}}
<pre>MostFreqKHashing( s1 , 2 ) = L9T8
Line 250:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 368:
s2 = reverseStr(s1)
mostFreqKSDF(s1, s2, 2, 100)
}</
{{out}}
Line 422:
=={{header|Haskell}}==
<
where
import Data.List ( nub , sortBy )
Line 461:
mostFreqKSDF :: String -> String -> Int ->Int
mostFreqKSDF s t n = mostFreqKSimilarity ( mostFreqKHashing s n ) (mostFreqKHashing t n )
</syntaxhighlight>
{{out}}
<pre>mostFrequentKHashing "LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV" 2
Line 470:
=={{header|J}}==
'''Solution''':<
mfksDF =: {:@:[ - (mfks@:(mfkh&.>)~ {.)~
Line 487:
NB. No need to fix mfksDF
mfkh =: mfkh f.
mfks =: mfks f.</
'''Examples''':<
fkh =. ;@:,@:(":&.>) NB. format k hash
Line 516:
'pass'
)
pass</
'''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).
Line 523:
Translation of the pseudo-code of the Wikipedia article [[wp:Most frequent k characters]] to [[wp:java]] implementation of three functions given in the definition section are given below with [[wp:JavaDoc]] comments:
<
import java.util.Comparator;
import java.util.HashMap;
Line 649:
}
}</
Line 655:
Defines the functions for counting and frequency outside of the hashing function, as it is easier to calculate the comparison from a structured result rather than parsing a hash.
<
const kCounts = str => {
const counts = {};
Line 703:
const mostFreqKSDF = (str1, str2, k, maxDistance) => {
return maxDistance - mostFreqKSimilarity(str1, str2, k);
};</
'''Testing with Jest'''
<
expect(mostFreqKHashing(str1, 2)).toBe("L9T8");
});
Line 716:
test("SDF of strings 1 and 2 with k=2 and max=100 is 83", () => {
expect(mostFreqKSDF(str1, str2, 2, 100)).toBe(83);
});</
'''Testing in Console'''
<
const str2 = "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG";
const K = 2;
Line 725:
console.log(mostFreqKHashing(str1, K));
console.log(mostFreqKHashing(str2, K));
console.log(mostFreqKSDF(str1, str2, K, maxDistance));</
{{out}}
Line 737:
'''Preliminaries'''
<
def bow(stream):
reduce stream as $word ({}; .[($word|tostring)] += 1);
Line 748:
| sort_by((.[1]|f), .[0])
| reverse
| map(.[1]);</
'''The Tasks'''
<
def MostFreqKHashing($K):
def chars: explode | range(0;length) as $i | [.[$i]] | implode;
Line 789:
| MostFreqKSDF(2; 100);
task2, task100</
{{out}}
<pre>
Line 808:
I have no idea what NULL0 is supposed to mean so I've ignored that.
<
fun mostFreqKHashing(input: String, k: Int): String =
Line 861:
s2 = s1.reversed()
mostFreqKSDF(s1, s2, 2, 100)
}</
{{out}}
Line 919:
In order to avoid the limitation to 10 occurrences, we chose to use an ordered table (equivalent to Python OrderedDict). We could have used Kotlin solution or used a sequence of tuples (char, count) but the ordered table gives better performance.
<
type CharCounts = OrderedTable[char, int]
Line 970:
echo "mostFreqKHashing(str1, 2) = ", mostFreqKHashing(S1, 2)
echo "mostFreqKHashing(str2, 2) = ", mostFreqKHashing(S2, 2)
echo "mostFreqKSDF(str1, str2, 2, 100) = ", mostFreqKSDF(S1, S2, 2, 100)</
{{out}}
Line 1,010:
=={{header|Perl}}==
<
use strict ;
use warnings ;
Line 1,060:
print "MostFreqKHashing ( " . '$firststring , 2)' . " is " . mostFreqHashing( $firststring , 2 ) . "\n" ;
print "MostFreqKHashing ( " . '$secondstring , 2)' . " is " . mostFreqHashing( $secondstring , 2 ) . "\n" ;
</syntaxhighlight>
{{out}}
<pre>MostFreqKHashing ( $firststring , 2) is L9T8
Line 1,067:
=={{header|Phix}}==
{{trans|Go}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mostFreqKHashing</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
Line 1,143:
<span style="color: #000000;">s2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mostFreqKSDF</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">)</span>
<!--</
Output matches Go and Kotlin.
Line 1,149:
{{works with|Python|2.7+}}
'''unoptimized and limited'''
<
def MostFreqKHashing(inputString, K):
occuDict = collections.defaultdict(int)
Line 1,172:
def MostFreqKSDF(inputStr1, inputStr2, K, maxDistance):
return maxDistance - MostFreqKSimilarity(MostFreqKHashing(inputStr1,K), MostFreqKHashing(inputStr2,K))</
'''optimized'''
A version that replaces the intermediate string with OrderedDict to reduce the time complexity of lookup operation:
<
def MostFreqKHashing(inputString, K):
occuDict = collections.defaultdict(int)
Line 1,197:
def MostFreqKSDF(inputStr1, inputStr2, K, maxDistance):
return maxDistance - MostFreqKSimilarity(MostFreqKHashing(inputStr1,K), MostFreqKHashing(inputStr2,K))</
Test:
<
str2 = "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG"
K = 2
Line 1,209:
print("%s:"%dict2)
print(''.join(c + str(cnt) for c, cnt in dict2.items()))
print(MostFreqKSDF(str1, str2, K, maxDistance))</
{{out}}
<pre>
Line 1,221:
=={{header|Racket}}==
<
(define (MostFreqKHashing inputString K)
Line 1,248:
;; (Should add more tests, but it looks like there's a bunch of mistakes
;; in the given tests...)
</syntaxhighlight>
=={{header|Raku}}==
Line 1,259:
Maybe I am too hasty though. Lets give it a try. Implement a MFKC routine and run an assortment of words through it to get a feel for how it hashes different words.
<syntaxhighlight lang="raku"
# which can be joined to make a string or manipulated further.
Line 1,297:
>;
say @test-words.map( { join '', mfkh($_)».kv } ).Bag;</
{{out}}
<pre>Bag(i2n2(151))</pre>
Line 1,303:
=={{header|Ring}}==
<
# Project : Most frequent k chars distance
Line 1,347:
next
return aList
</syntaxhighlight>
Output:
<pre>
Line 1,357:
=={{header|Sidef}}==
<
var seen = Hash()
Line 1,437:
MostFreqKSDF(s, t, k, limit),
)
}</
{{out}}
<pre>
Line 1,456:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
proc MostFreqKHashing {inputString k} {
Line 1,481:
set h2 [MostFreqKHashing $inputStr2 $k]
expr {$limit - [MostFreqKSimilarity $h1 $h2]}
}</
Demonstrating:
<
set str2 "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG"
puts [MostFreqKHashing $str1 2]
puts [MostFreqKHashing $str2 2]
puts [MostFreqKSDF $str1 $str2 2 100]</
{{out}}
<pre>
Line 1,496:
;A more efficient metric calculator
This version is appreciably more efficient because it does not compute the intermediate string representation “hash”, instead working directly on the intermediate dictionaries and lists:
<
set c1 [set c2 {}]
foreach ch [split $inputStr1 ""] {dict incr c1 $ch}
Line 1,508:
}
return [expr {$limit - $s}]
}</
It computes the identical value on the identical inputs.
=={{header|Typescript}}==
{{trans|Javascript}} with added type annotations
<
const kCounts = (str: string): Record<string, number> => {
const counts: Record<string, number> = {};
Line 1,561:
const mostFreqKSDF = ( str1: string, str2: string, k: number, maxDistance: number ): number => {
return maxDistance - mostFreqKSimilarity(str1, str2, k);
};</
=={{header|Wren}}==
Line 1,568:
{{libheader|Wren-sort}}
{{libheader|Wren-trait}}
<
import "/sort" for Sort
import "/trait" for Stepped
Line 1,629:
s1 = "abracadabra12121212121abracadabra12121212121"
s2 = s1[-1..0]
mostFreqKSDF.call(s1, s2, 2, 100)</
{{out}}
|