Last active
August 29, 2015 14:03
-
-
Save tmiller/5871d98225341c5dec59 to your computer and use it in GitHub Desktop.
Round robin tournament algorthim for generating coding pairs in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.List | |
data Coder = Coder String | |
| Nobody | |
deriving (Show) | |
data Pairings = Pair Coder Coder | |
| Pairs [Pairings] | |
deriving (Show) | |
main = putStrLn . intercalate "\n" . map renderPairings . pair . coders $ names | |
names :: [String] | |
names = ["Andrew","Anthony","Ben","Matt", "Tom","Zac"] | |
coders :: [String] -> [Coder] | |
coders cs | even (length cs) = map Coder cs | |
| otherwise = Nobody : map Coder cs | |
split :: [a] -> ([a],[a]) | |
split [] = ([],[]) | |
split xs = splitAt midpoint xs | |
where midpoint = (length xs) `div` 2 | |
reverseSnd :: ([a],[a]) -> ([a],[a]) | |
reverseSnd (top,bot) = (top, reverse bot) | |
rotate :: [a] -> [a] | |
rotate xs = last xs : init xs | |
pair :: [Coder] -> [Pairings] | |
pair (piv:cs) = map buildPairsings orderdDevRotations | |
where buildPairsings = Pairs . (uncurry $ zipWith Pair) . reverseSnd . split | |
orderdDevRotations = take (length cs) rotations | |
rotations = map (piv:) (iterate rotate cs) | |
renderCoder :: Coder -> String | |
renderCoder (Coder c) = filter (/= '\"') $ show c | |
renderCoder (Nobody) = "Nobody" | |
renderPairings :: Pairings -> String | |
renderPairings (Pair a b) = (renderCoder a) ++ " & " ++ (renderCoder b) | |
renderPairings (Pairs ps) = (show $ map renderPairings ps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment