Most frequent k chars distance: Difference between revisions

no edit summary
No edit summary
Line 143:
</pre>
=Implementations=
 
=={{header|Haskell}}==
<lang Haskell>module MostFrequentK
where
import Data.List ( nubBy , sortBy , groupBy )
import qualified Data.Set as S
 
count :: Eq a => [a] -> a -> Int
count [] x = 0
count ( x:xs ) k
|x == k = 1 + count xs k
|otherwise = count xs k
 
--frequency of letters
letterFrequency :: String -> [( Char , Int )]
letterFrequency s = nubBy (\e f -> fst e == fst f ) letterfrequencies
where
letterfrequencies = zip s ( map (\c -> count s c ) s )
 
letterStatistics :: String -> [(Char , Int)]
letterStatistics myWord = sortBy (\c d -> compare ( snd c ) ( snd d ) ) $ letterFrequency myWord
 
--frequency of letters , if identical, ordered by first occurrence in string
--function mostFrequentKHashing starts at the last elements, therefore the sublists have to be reversed
orderStatistics :: String -> [(Char , Int)]
orderStatistics s = concat $ map ( reverse . ( sortBy myCriterion ) ) orderedStatistics
where
orderedStatistics = groupBy (\g h -> snd g == snd h ) $ letterStatistics s
found :: Char -> String -> Int
found c str = ( length $ takeWhile ( /= c ) str ) + 1
myCriterion :: (Char , Int) -> (Char , Int ) -> Ordering
myCriterion d e = compare ( found ( fst d ) s ) ( found ( fst e ) s )
mostFrequentKHashing :: String -> Int -> String
mostFrequentKHashing s n = toString lastElement ++ toString secondFromLast
where
statistics = orderStatistics s
lastElement = last statistics
secondFromLast = last $ init statistics
toString :: (Char , Int ) -> String
toString ( c , i ) = c : show i
 
mostFreqKSimilarity :: String -> String -> Int
mostFreqKSimilarity s t = fromEnum ( last $ head list ) - 48
where
toPairs :: String -> [String]
toPairs st = [take 2 $ drop start st | start <- [0,2 ..length st - 2]]
list = S.toList ( S.fromList ( toPairs s ) `S.intersection` S.fromList ( toPairs t ) )
 
mostFreqKSDF :: String -> String -> Int -> Int -> Int
mostFreqKSDF s1 s2 k dist = dist - mostFreqKSimilarity ( mostFrequentKHashing s1 k ) ( mostFrequentKHashing s2 k )
</lang>
{{out}}
<pre>mostFrequentKHashing "LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV" 2
"L9T8"
*MostFrequentK> mostFrequentKHashing "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG" 2
"F9L8"
</pre>
 
=={{header|J}}==
258

edits