Skip to content

Instantly share code, notes, and snippets.

@kisielk
Created March 15, 2011 07:10
Show Gist options
  • Select an option

  • Save kisielk/870412 to your computer and use it in GitHub Desktop.

Select an option

Save kisielk/870412 to your computer and use it in GitHub Desktop.
Our candidate programming problem, implemented in Haskell
import Data.List
normalizedScore :: String -> String -> Float
normalizedScore a b = fromIntegral (numSimilar a b) / fromIntegral (length a)
numSimilar :: String -> String -> Int
numSimilar a b = sum $ map fromEnum $ zipWith (==) a b
slice :: Int -> Int -> [a] -> [a]
slice from to xs = take (to - from + 1) (drop from xs)
slices :: Int -> [a] -> [[a]]
slices l xs = [slice a (a+l-1) xs | a <- [0 .. length xs - l]]
nbest :: Int -> String -> String -> [(Float, String)]
nbest n a b = take n $ reverse $ sort $ map (\x -> (normalizedScore a x, x)) $ slices (length a) b
@kisielk

kisielk commented Mar 15, 2011

Copy link
Copy Markdown
Author

was able to remove the similarity function and replace it with fromEnum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment