module Exercise02 where import Data.Ord ( comparing ) import Data.List {-H2.1a)-} twoThirdsAverageWinners :: [(String, Int)] -> [String] twoThirdsAverageWinners gs = [fst x | x <- gs, abs (snd x - avg) == bst] where num = [snd x | x <- gs] avg = (2 * sum num) `div` (length num * 3) bst = minimum [abs (x-avg) | x <- num] {-H2.1b)-} lowestUniqueBidder :: [(String, Int)] -> String lowestUniqueBidder bs = fst (lowest (-1)) where numbers = [snd x | x <- bs] border n = [x | x <- numbers, x > n] lowest n | length [x | x <- numbers, x == minimum (border n)] == 1 = head [x | x <- bs, snd x == minimum (border n)] | minimum (border n) == maximum (border n) = ("Nobody", 101) | otherwise = lowest (minimum (border n)) {-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, i `elem` dominion tournament x] {-H2.2b)-} covers :: [[Int]] -> Int -> Int-> Bool covers tournament i j = not (null (dominion tournament i)) && length [x | x <- dominion tournament i, x `elem` dominion tournament j] == length (dominion tournament j) {-2.2c)-} dominant :: [[Int]] -> [Int] -> Bool dominant tournament xs = not (null xs) && length [x | x <- xs, dominatesAll x] == length xs where left = [x | x <- players tournament, x `notElem` xs] dominatesAll i = length [x | x <- dominion tournament i, x `elem` left] == length left {-WETT-} {-H2.2d)-} copeland :: [[Int]] -> [Int] copeland tournament = elemIndices (maximum l) l where l = -1 : map length tournament {-H2.2e)-} uncoveredSet :: [[Int]] -> [Int] uncoveredSet tournament = [x | x <- players tournament, not $ or [covers tournament y x | y <- players tournament, y /= x]] {-H2.2f)-} topCycle :: [[Int]] -> [Int] topCycle tournament = topCycleRec $ copeland tournament where topCycleRec cur = if dominant tournament cur then cur else topCycleRec $ cur ++ (players tournament \\ foldr1 intersect (map (dominion tournament) cur)) {-TTEW-} {- Experiment 2.2e (mehr Tokens leider): uncoveredSet tournament = map succ $ elemIndices False $ map (or . uncoveredSetF) $ players tournament where uncoveredSetF x = map (\y -> y /= x && covers tournament y x) $ players tournament -} {- alte 2.2f: topCycle tournament = shortest [x ++ copeland tournament | x <- subsequences [y | y <- players tournament \\ copeland tournament], dominant tournament $ x ++ copeland tournament] -}