-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions-or-not.hs
More file actions
31 lines (28 loc) · 1017 Bytes
/
functions-or-not.hs
File metadata and controls
31 lines (28 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Author: btjanaka (Bryon Tjanaka)
-- Problem: (HackerRank) functions-or-not
-- Title: Functions or Not?
-- Link: https://www.hackerrank.com/challenges/functions-or-not/problem
-- Idea: Functions map each value to only one other value.
-- Difficulty: easy
-- Tags: fp
import Control.Monad
import qualified Data.Map as Map
readLines :: Int -> IO [String]
readLines n
| n == 0 = return []
| otherwise = do
s <- getLine
otherLines <- readLines (n - 1)
return $ s : otherLines
functionChecker :: (Bool, Map.Map Int Int) -> [Int] -> (Bool, Map.Map Int Int)
functionChecker (fails, m) [k, v] =
(fails || (Map.member k m && (Map.!) m k /= v), Map.insert k v m)
main = do
t <- readLn :: IO Int
forM_ [1 .. t] $ \ca -> do
n <- readLn :: IO Int
funcText <- readLines n
let mappings = map (map (read :: String -> Int) . words) funcText
putStrLn $ if fst $ foldl functionChecker (False, Map.empty) mappings
then "NO"
else "YES"