haskell: add more katas

This commit is contained in:
Dmitriy Pleshevskiy 2022-05-30 16:00:41 +03:00
parent 2fb615473c
commit 657f019269
3 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,6 @@
module UniqueInOrder (uniqueInOrder) where
import Data.List (head, group)
uniqueInOrder :: Eq a => [a] -> [a]
uniqueInOrder = map head . group

View file

@ -0,0 +1,14 @@
module Codewars.G964.WeightSort where
import Data.Char (digitToInt)
import Data.List (sortBy)
import Data.Ord (comparing)
orderWeight :: [Char] -> [Char]
orderWeight = unwords . sortBy cmpWeights . words
cmpWeights :: [Char] -> [Char] -> Ordering
cmpWeights = comparing weights
weights :: [Char] -> (Int, [Char])
weights s = (sum . map digitToInt $ s, s)

View file

@ -0,0 +1,54 @@
{-
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
-}