module Exercise02 where import Data.List import Data.Ord {-H2.1a)-} twoThirdsAverageWinners :: [(String, Int)] -> [String] twoThirdsAverageWinners gs = [name | (name, guess) <- gs, abs (guess-correct) == minimum [ abs ( g - correct) | (_,g) <- gs]] where correct = div (2 * sum [i | (_, i) <- gs]) (3*length gs) {-H2.1b)-} lowestUniqueBidder :: [(String, Int)] -> String lowestUniqueBidder bs = head ( [name | (name, guess) <- bs, guess == minimum ([g | (n, g) <- bs, null [(s, i) | (s, i) <- bs, i == g, n /= s] ] ++ [101])] ++ ["Nobody"] ) {-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 (\j -> i `elem` dominion tournament j) $ players tournament {-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 = not (null xs) && null ( (players tournament \\ xs) \\ foldr1 intersect [dominion tournament i | i <- xs]) -- 63 tokens {-WETT-} {-H2.2d)-} copeland :: [[Int]] -> [Int] copeland tournament = map succ $ maximum (map length tournament) `elemIndices` map length tournament {-H2.2e)-} uncoveredSet :: [[Int]] -> [Int] uncoveredSet tournament = players tournament \\ [i | i <- players tournament, p <- dominators tournament i, covers tournament p i] {-H2.2f)-} topCycle :: [[Int]] -> [Int] topCycle tournament = head $ dominant tournament `filter` inits ((Down . length . dominion tournament) `sortOn` players tournament) {-TTEW-} {- MCCOMMENT an even shorter, but much slower version of topCycle: topCycle tournament = shortest $ dominant tournament `filter` subsequences (players tournament) -}