Skip to content

Instantly share code, notes, and snippets.

@ehamberg
Created May 21, 2011 17:40
Show Gist options
  • Save ehamberg/984711 to your computer and use it in GitHub Desktop.
Save ehamberg/984711 to your computer and use it in GitHub Desktop.
import qualified Data.ByteString.Char8 as B
import qualified Data.MemoCombinators as Memo
findRPI :: Int -> [B.ByteString] -> String
findRPI games matches = unlines $ map (\t -> show $ rpi t matches) [0..(games-1)]
rpi :: Int -> [B.ByteString] -> Double
rpi team matches = 0.25 * wp' + 0.50 * owp' team matches + 0.25 * oowp' team matches
where wp' = wp (matches !! team) Nothing
owp' = Memo.integral owp
oowp' = Memo.integral oowp
wp :: B.ByteString -> Maybe Int -> Double
wp matches exclude = fromIntegral won/fromIntegral total
where games = B.filter (/= '.') matches'
total = B.length games
won = (B.length . B.filter (=='1')) games
matches' = case exclude of
Nothing -> matches
Just t -> B.take t matches `B.append` B.drop (t+1) matches
owp :: Int -> [B.ByteString] -> Double
owp team matches = sum owps/nOpponents
where opponents = map fst $ filter (\e -> snd e /= '.') $ zip [0..] (B.unpack $ matches !! team)
nOpponents = (fromIntegral . length) opponents
owps = map (flip wp (Just team) . (\ i -> matches !! i)) opponents
oowp :: Int -> [B.ByteString] -> Double
oowp team matches = sum oowps/nOpponents
where opponents = map fst $ filter (\e -> snd e /= '.') $ zip [0..] (B.unpack $ matches !! team)
nOpponents = (fromIntegral . length) opponents
oowps = map (`owp` matches) opponents
printCase n = do
nGames <- fmap read getLine
ts <- mapM (\_ -> fmap B.pack getLine) [1..nGames]
putStr $ "Case #" ++ show n ++ ":\n" ++ findRPI nGames ts
main = do
t <- fmap read getLine
mapM_ printCase [1..t]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment