Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Debug.Trace
- import qualified Data.Map as Map
- {-
- You are given queries. Each query is of the form two integers described below:
- - 1:x Insert x in your data structure.
- - 2:y Delete one occurence of y from your data structure, if present.
- - 3:z Check if any integer is present whose frequency is exactly z. If yes, print 1 else 0.
- -}
- -- Complete the freqQuery function below.
- freqQuery :: [[Int]] -> ([Int], Map.Map Int Int)
- freqQuery queries = foldr op ([], Map.empty) queries
- where
- op (1 : [x]) (acc, m) = (acc, Map.insertWith (+) x 1 m)
- op (2 : [y]) (acc, m) = (acc, Map.update clear y m)
- op (3 : [z]) (acc, m) = if (Map.size (Map.filter (== z) m)) > 0
- then (1 : acc, m)
- else (0 : acc, m)
- clear f = trace ("run if") (if (f - 1) == 0 then Nothing else Just $ f - 1) -- NEVER RUNS
- -- freqQuery [[1,3],[2,3]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement