module Exercise02 where import Data.List import Data.Ord {-H2.1a)-} {-bids: only the bids from the tuples a: average of the bids bids2: how far from average each bid is -} twoThirdsAverageWinners :: [(String, Int)] -> [String] twoThirdsAverageWinners gs = [n | (n,b) <- zip (map fst gs) bids2, b == minimum bids2] where bids = map snd gs bids2 = map (\b -> abs (b-((2*sum bids) `div` (3*length bids)))) bids {-H2.1b)-} lowestUniqueBidder :: [(String, Int)] -> String lowestUniqueBidder bs = if null noDuplicates then "Nobody" else fst (minimumBy comp noDuplicates) where duplicateBets = concat [xs | xs <- group ( sort(map snd bs)), length xs /= 1] noDuplicates = filter (\x -> snd x `notElem` duplicateBets) bs comp e1 e2 = compare (snd e1) (snd e2) {-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 = filter (`notElem` notDominators) (players tournament) where notDominators = dominion tournament i ++ [i] {-H2.2b)-} covers :: [[Int]] -> Int -> Int-> Bool covers tournament i j = all (`elem` domI) domJ where domI = dominion tournament i domJ = dominion tournament j {-2.2c)-} dominant :: [[Int]] -> [Int] -> Bool dominant _ [] = False dominant tournament xs = all checkIfDominant xs where notInSet = filter (`notElem` xs) (players tournament) checkIfDominant i = not (any (`notElem` dominion tournament i) notInSet) {-WETT-} {-H2.2d)-} copeland :: [[Int]] -> [Int] copeland xs = succ <$> maximum ls `elemIndices` ls where ls = map length xs {-H2.2e)-} uncoveredSet :: [[Int]] -> [Int] uncoveredSet tournament = pl \\ allCovered `concatMap` pl where allCovered x = covers tournament x `filter` delete x pl pl = players tournament {-H2.2f)-} topCycle :: [[Int]] -> [Int] topCycle tournament = until (dominant tournament) (nub . (dominators tournament `concatMap`)) $uncoveredSet tournament {-TTEW-}