Skip to content

Commit d161463

Browse files
committed
Solve 526989a41034285187000de4
1 parent e9647ae commit d161463

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

src/Kyu5/Bananas.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ bananas xs = go xs banana
88
where
99
go xs [] = ['-' <$ xs]
1010
go [] _ = []
11-
go (x : xs) (b : bs) =
12-
[x : rest | x == b, rest <- go xs bs]
11+
go (x : xs) (b : bs) =
12+
[x : rest | x == b, rest <- go xs bs]
1313
++ ['-' : rest | rest <- go xs (b : bs)]
14-

src/Kyu5/IpsBetween.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Kyu5.IpsBetween where
2+
3+
import Data.List.Split (splitOn)
4+
5+
type Ipv4 = String
6+
7+
ipsBetween :: Ipv4 -> Ipv4 -> Int
8+
ipsBetween from to = to32Bit to - to32Bit from
9+
10+
to32Bit :: Ipv4 -> Int
11+
to32Bit ip = foldl (\acc x -> acc * 256 + x) 0 $ map read $ splitOn "." ip

test/Kyu5/BananasSpec.hs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
module Kyu5.BananasSpec (spec) where
22

3-
import Kyu5.Bananas (bananas)
4-
import Test.Hspec
53
import Control.Monad (when)
64
import Data.List (sort)
5+
import Kyu5.Bananas (bananas)
6+
import Test.Hspec
77

88
spec :: Spec
99
spec = do
1010
describe "Example tests" $ do
1111
it "example 0" $ test "banann" []
1212
it "example 1" $ test "banana" ["banana"]
13-
it "example 2" $ test "bbananana" ["-b--anana","-ba--nana","-ban--ana","-bana--na"
14-
,"-banan--a","-banana--","b---anana","b-a--nana"
15-
,"b-an--ana","b-ana--na","b-anan--a","b-anana--"
16-
]
17-
it "example 3" $ test "bananaaa" ["banan--a","banan-a-","banana--"]
18-
it "example 4" $ test "bananana" ["b--anana","ba--nana","ban--ana","bana--na","banan--a","banana--"]
13+
it "example 2" $
14+
test
15+
"bbananana"
16+
[ "-b--anana",
17+
"-ba--nana",
18+
"-ban--ana",
19+
"-bana--na",
20+
"-banan--a",
21+
"-banana--",
22+
"b---anana",
23+
"b-a--nana",
24+
"b-an--ana",
25+
"b-ana--na",
26+
"b-anan--a",
27+
"b-anana--"
28+
]
29+
it "example 3" $ test "bananaaa" ["banan--a", "banan-a-", "banana--"]
30+
it "example 4" $ test "bananana" ["b--anana", "ba--nana", "ban--ana", "bana--na", "banan--a", "banana--"]
1931

2032
test :: String -> [String] -> Expectation
2133
test xs expected = do

test/Kyu5/IpsBetweenSpec.hs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Kyu5.IpsBetweenSpec where
2+
3+
import Data.List
4+
import Data.List.Split
5+
import Kyu5.IpsBetween
6+
import Test.Hspec
7+
import Test.QuickCheck
8+
9+
spec :: Spec
10+
spec = do
11+
describe "ipsBetween" $ do
12+
it "basic" $ do
13+
ipsBetween "10.0.0.0" "10.0.0.50" `shouldBe` 50
14+
ipsBetween "20.0.0.10" "20.0.1.0" `shouldBe` 246
15+
ipsBetween "150.0.0.0" "150.0.0.1" `shouldBe` 1
16+
ipsBetween "10.0.0.0" "10.0.0.50" `shouldBe` 50
17+
ipsBetween "20.0.0.10" "20.0.1.0" `shouldBe` 246
18+
ipsBetween "10.11.12.13" "10.11.13.0" `shouldBe` 243
19+
ipsBetween "160.0.0.0" "160.0.1.0" `shouldBe` 256
20+
ipsBetween "170.0.0.0" "170.1.0.0" `shouldBe` 65536
21+
ipsBetween "50.0.0.0" "50.1.1.1" `shouldBe` 65793
22+
ipsBetween "180.0.0.0" "181.0.0.0" `shouldBe` 16777216
23+
ipsBetween "1.2.3.4" "5.6.7.8" `shouldBe` 67372036
24+
25+
it "random" $ do
26+
forAll (vectorOf 4 (choose (0, 255))) $ \x ->
27+
forAll (vectorOf 4 (choose (0, 255))) $ \y ->
28+
let from = asIP x
29+
to = asIP y
30+
in to32Bit' to <= to32Bit' from ==> collect (from, to) $ ipsBetween from to === solution from to
31+
32+
asIP :: [Int] -> String
33+
asIP = intercalate "." . map show
34+
35+
solution :: String -> String -> Int
36+
solution from to = to32Bit' to - to32Bit' from
37+
38+
to32Bit' :: String -> Int
39+
to32Bit' ip = foldl (\acc x -> acc * 256 + x) 0 $ map read $ splitOn "." ip

0 commit comments

Comments
 (0)