From 657f019269a735ffd941f65ffd9d68ee8d730ff9 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 30 May 2022 16:00:41 +0300 Subject: [PATCH] haskell: add more katas --- haskell/12_unique_in_order.hs | 6 +++ haskell/13_weight_for_weight.hs | 14 +++++ .../fundamentals/1_total_amount_of_points.hs | 54 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 haskell/12_unique_in_order.hs create mode 100644 haskell/13_weight_for_weight.hs create mode 100644 haskell/fundamentals/1_total_amount_of_points.hs diff --git a/haskell/12_unique_in_order.hs b/haskell/12_unique_in_order.hs new file mode 100644 index 0000000..d29209d --- /dev/null +++ b/haskell/12_unique_in_order.hs @@ -0,0 +1,6 @@ +module UniqueInOrder (uniqueInOrder) where + +import Data.List (head, group) + +uniqueInOrder :: Eq a => [a] -> [a] +uniqueInOrder = map head . group diff --git a/haskell/13_weight_for_weight.hs b/haskell/13_weight_for_weight.hs new file mode 100644 index 0000000..2864103 --- /dev/null +++ b/haskell/13_weight_for_weight.hs @@ -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) diff --git a/haskell/fundamentals/1_total_amount_of_points.hs b/haskell/fundamentals/1_total_amount_of_points.hs new file mode 100644 index 0000000..14f43e6 --- /dev/null +++ b/haskell/fundamentals/1_total_amount_of_points.hs @@ -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 +-}