module Exercise02 where import Data.List import Data.Ord {-H2.1a)-} twoThirdsAverageWinners :: [(String, Int)] -> [String] twoThirdsAverageWinners gs = let average = floor ((2/3)*(fromIntegral(sum [snd p | p <- gs]) / fromIntegral (length gs))) distances = [(name, abs (bet - average)) | (name, bet) <- gs] in [name | (name, dist) <- distances, dist == minimum [snd p | p <- distances]] {-H2.1b)-} lowestUniqueBidder :: [(String, Int)] -> String lowestUniqueBidder bs | not (null unique) = fst (head unique) | otherwise = "Nobody" where unique = sortOn snd [(name, bet) | (name, bet) <- bs, 1 == length (findIndices (\a -> snd a == bet) bs)] {-H2-} -- returns the shortest list in a list of lists shortest :: [[Int]] -> [Int] shortest = minimumBy (comparing length) -- returns the set of all players in a tournament players :: [[Int]] -> [Int] players tournament = [1..length tournament] -- returns the dominion of player i dominion :: [[Int]] -> Int -> [Int] dominion tournament i = tournament !! (i - 1) {-H2.2a)-} dominators :: [[Int]] -> Int -> [Int] dominators tournament i = (players tournament \\ dominion tournament i) \\ [i] {-H2.2b)-} covers :: [[Int]] -> Int -> Int-> Bool covers tournament i j = null (dominion tournament j \\ dominion tournament i) {-2.2c)-} dominant :: [[Int]] -> [Int] -> Bool dominant tournament xs | null xs = False | otherwise = all (\x -> all (\y -> y `elem` dominion tournament x) dominated) xs where dominated = players tournament \\ xs {-WETT-} {-H2.2d)-} copeland :: [[Int]] -> [Int] copeland tournament = map succ $ findIndices ((maximum (map length tournament) ==) . length) tournament {-H2.2e)-} uncoveredSet :: [[Int]] -> [Int] uncoveredSet t = players t \\ [x | x <- players t, y <- delete x $ players t, covers t y x] {-H2.2f)-} topCycle :: [[Int]] -> [Int] topCycle t = last $ filter (dominant t) $ tails $ (length . dominion t) `sortOn` players t {-TTEW-}