module Exercise02 where import Data.List import Data.Ord import GHC.Float (double2Int) import Data.Maybe ownTournament :: [[Int]] ownTournament = [[2,3,4, 5], [3,4,5], [5], [], [4]] given :: [[Int]] given = [[4], [1], [1,2,4,5], [2], [1,2,4,6], [1,2,3,4]] {-H2.1a)-} twoThirdsAverageWinners :: [(String, Int)] -> [String] twoThirdsAverageWinners gs = [fst x | x <- gs, abs (a - snd x) == m] where a = double2Int(fromIntegral (sum [snd x | x <- gs]) / fromIntegral (length gs) * 2 / 3) m = minimum [abs $ a - snd z| z <- gs] {-H2.1b)-} lowestUniqueBidder :: [(String, Int)] -> String lowestUniqueBidder bs = if null onlyUnique then "Nobody" else head [fst x | x <- bs, snd x == minUniqueBid] where allNums = [snd x | x <- bs] allNums1Time = nub allNums onlyDuplicates = nub $ allNums \\ allNums1Time onlyUnique = filter (`notElem` onlyDuplicates) allNums minUniqueBid = minimum onlyUnique {-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 = [p | p <- players tournament, i `elem` dominion tournament p] {-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) && and [sort xs == filter (`elem` xs) (dominators tournament x)| x <- nonDominant] where nonDominant = players tournament \\ xs {-WETT-} {-H2.2d)-} copeland :: [[Int]] -> [Int] copeland tournament = map succ $ findIndices ((==maximum (map length tournament)) . length) tournament --21 {-H2.2e)-} uncoveredSet :: [[Int]] -> [Int] uncoveredSet tournament = filter (\x -> not . any (flip (covers tournament) x) $ delete x $ players tournament) $ players tournament -- 29 {-H2.2f)-} topCycle :: [[Int]] -> [Int] topCycle tournament = helper tournament $ uncoveredSet tournament --47 helper :: [[Int]] -> [Int] -> [Int] helper tournament set | null r = set | otherwise = helper tournament $ union r set where r = (players tournament \\ foldr (intersect . dominion tournament) (players tournament) set) \\ set {-TTEW-}