module Exercise02 where import Data.List import Data.Ord import Data.Graph import Data.Ix {-H2.1a)-} twoThirdsAverageWinners :: [(String, Int)] -> [String] twoThirdsAverageWinners gs = [fst x | x <- gs, abs(snd x - av) == ee] where ss = 2 * (sum [snd x | x <- gs]) av = div ss (length gs * 3) ee = minimum [abs(snd x - av) | x <- gs] {-H2.1b)-} lowestUniqueBidder :: [(String, Int)] -> String lowestUniqueBidder bs | length un == 0 = "Nobody" | otherwise = head ee where un = [x | x <- bs, length [y | y <- bs, snd y == snd x] == 1] mm = minimum [snd x | x <- un] ee = [fst x | x <- un, snd x == mm] {-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 = [x | x <- players tournament, elem i (dominion tournament x)] {-H2.2b)-} covers :: [[Int]] -> Int -> Int-> Bool covers tournament i j = all over (dominion tournament j) where over ::Int -> Bool over x = elem x (dominion tournament i) {-2.2c)-} dominant :: [[Int]] -> [Int] -> Bool dominant tournament [] = False dominant tournament xs = all pos xs where pos ::Int -> Bool pos x = all rr (dominators tournament x) rr :: Int -> Bool rr y = elem y xs {-WETT-} {-H2.2d)-} copeland :: [[Int]] -> [Int] copeland tournament = [x | x <- players tournament, length (dominion tournament x) == mx] where mx = maximum [length (dominion tournament x) | x <- players tournament] {-H2.2e)-} uncoveredSet :: [[Int]] -> [Int] uncoveredSet tournament = [x | x <- players tournament, length [y | y <- dominators tournament x, covers tournament y x] == 0] {-H2.2f)-} topCycle :: [[Int]] -> [Int] topCycle tournament = flattenSCC (last (stronglyConnComp [(x,x,dominion tournament x) | x <- players tournament])) {-TTEW-}