Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. module FrequencyQueries where
  2.  
  3. import Debug.Trace
  4. import qualified Data.Map as Map
  5. {-
  6. You are given queries. Each query is of the form two integers described below:
  7. - 1:x Insert x in your data structure.
  8. - 2:y Delete one occurence of y from your data structure, if present.
  9. - 3:z Check if any integer is present whose frequency is exactly z. If yes, print 1 else 0.
  10. -}
  11. -- Complete the freqQuery function below.
  12. freqQuery :: [[Int]] -> ([Int], Map.Map Int Int)
  13. freqQuery queries = foldr op ([], Map.empty) queries
  14. where
  15. op (1 : [x]) (acc, m) = (acc, Map.insertWith (+) x 1 m)
  16. op (2 : [y]) (acc, m) = (acc, Map.update clear y m)
  17. op (3 : [z]) (acc, m) = if (Map.size (Map.filter (== z) m)) > 0
  18. then (1 : acc, m)
  19. else (0 : acc, m)
  20. clear f = if (f - 1) == 0 then Nothing else Just $ f - 1 -- never seems to run
  21.  
  22. -- run with freqQuery [[1,3],[2,3]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement