Skip to content

Instantly share code, notes, and snippets.

@matoous
Last active June 6, 2018 16:48
Show Gist options
  • Save matoous/ed7e6957caa39ee72ffac2596949d56c to your computer and use it in GitHub Desktop.
Save matoous/ed7e6957caa39ee72ffac2596949d56c to your computer and use it in GitHub Desktop.
normalize :: [Double] -> [Double]
normalize l = map (\x -> x / (maximum normalized)) normalized
where normalized = map (\x -> (x - minimum l)) l
removeMax :: [Int] -> Int -> [Int]
removeMax ls mx = map (\x -> if x == mx then x - 1 else x) ls
increment :: [Int] -> Int -> [Int]
increment (a:b) 0 = (a + 1):b
increment (a:b) y = a:(increment b (y - 1))
count :: [Int] -> [Int] -> [Int]
count [] bins = bins
count (idx:rest) bins = count rest (increment bins idx)
indexes :: [Double] -> Int -> [Int]
indexes xs bins = map (\x -> truncate x) $ map (\x -> x / (1.0 / (fromIntegral bins))) (normalize xs)
createHistogram :: [Double] -> Int -> [Int]
createHistogram xs bins = count (removeMax (indexes xs bins) bins) (take bins $ repeat 0)
main :: IO ()
main = do xs <- getLine
bins <- getLine
putStr $ unlines $ map (\x -> show $ x) (createHistogram (map (\x -> read x :: Double) $ words xs) (read bins :: Int))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment