Advertisement
Guest User

Untitled

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