mykatas/haskell/fundamentals/1_total_amount_of_points.hs

55 lines
1.4 KiB
Haskell

{-
Our football team finished the championship. The result of each match look like "x:y". Results of all matches are recorded in the collection.
For example: ["3:1", "2:2", "0:1", ...]
Write a function that takes such collection and counts the points of our team in the championship. Rules for counting points for each match:
if x > y: 3 points
if x < y: 0 point
if x = y: 1 point
Notes:
there are 10 matches in the championship
0 <= x <= 4
0 <= y <= 4
-}
module TotalPoints where
import Data.Char (isDigit)
points :: [String] -> Int
points = sum . map (counting . nums)
counting :: (Int, Int) -> Int
counting t
| uncurry (>) t = 3
| uncurry (<) t = 0
| otherwise = 1
nums :: String -> (Int, Int)
nums chars = (read c1, read c2)
where
c1, c2 :: String
c2 = drop ((+1) $ length c1) chars
c1 = takeWhile isDigit chars
{-
module TotalPointsSpec where
import TotalPoints
import Test.Hspec
spec = do
describe "Total amount of points" $ do
it "Basic tests" $ do
points ["1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"] `shouldBe` 30
points ["1:1","2:2","3:3","4:4","2:2","3:3","4:4","3:3","4:4","4:4"] `shouldBe` 10
points ["0:1","0:2","0:3","0:4","1:2","1:3","1:4","2:3","2:4","3:4"] `shouldBe` 0
points ["1:0","2:0","3:0","4:0","2:1","1:3","1:4","2:3","2:4","3:4"] `shouldBe` 15
points ["1:0","2:0","3:0","4:4","2:2","3:3","1:4","2:3","2:4","3:4"] `shouldBe` 12
-}