Skip to content

Commit 0d38941

Browse files
committed
Solve 'Counting Duplicates Across Multiple Lists' kata
1 parent 1368b32 commit 0d38941

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Duplicates.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Duplicates where
2+
3+
-- https://www.codewars.com/kata/6113c2dc3069b1001b8fdd05/train/haskell
4+
5+
import Data.List (group, sort)
6+
7+
countDuplicates :: [String] -> [Int] -> [Int] -> Int
8+
countDuplicates names ages heights =
9+
sum
10+
. map (subtract 1)
11+
. filter (> 1)
12+
. map length
13+
. group
14+
. sort
15+
$ zip3 names ages heights

test/DuplicatesSpec.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module DuplicatesSpec where
2+
3+
import Duplicates (countDuplicates)
4+
import Test.HUnit
5+
import Test.Hspec
6+
7+
spec :: Spec
8+
spec = do
9+
describe "Fixed Tests" $ do
10+
it "Returns 0 if there are no duplicates" $ do
11+
runTest
12+
["John", "Beth", "Beth", "Bill"]
13+
[37, 23, 30, 46]
14+
[183, 170, 165, 175]
15+
0
16+
it "Counts duplicates of one entry" $ do
17+
runTest
18+
["John", "Beth", "Beth", "Beth", "Bill"]
19+
[37, 23, 23, 23, 46]
20+
[183, 170, 170, 170, 175]
21+
2
22+
it "Counts duplicates from multiple entries" $ do
23+
runTest
24+
["Jack", "Ben", "Jack", "Ben", "Jack", "Jack"]
25+
[25, 25, 34, 25, 25, 34]
26+
[180, 180, 200, 180, 180, 200]
27+
3
28+
29+
runTest :: [String] -> [Int] -> [Int] -> Int -> Expectation
30+
runTest name age height expected = assertEqual msg expected actual
31+
where
32+
actual = countDuplicates name age height
33+
msg = "Test Failed\n " ++ showInput (name, age, height)
34+
35+
showInput :: ([String], [Int], [Int]) -> String
36+
showInput (n, a, h) = unwords ["countDuplicates", show n, show a, show h]

0 commit comments

Comments
 (0)