Added tests for negative integers, modified solution so it doesn't rely on Show
instance of Int
.
module AreThereThree where solution :: Int -> Bool solution = go . abs where go 0 = False go x = let (x', y) = x `divMod` 10 in y == 3 || go x'
- module AreThereThree where
- solution :: Int -> Bool
solution = elem '3' . show- solution = go . abs
- where
- go 0 = False
- go x = let (x', y) = x `divMod` 10 in y == 3 || go x'
module AreThereThreeSpec where import Test.Hspec import AreThereThree spec :: Spec spec = do describe "Fixed Tests" $ do it "should return True if contains 3" $ do solution 354523 `shouldBe` True solution 35 `shouldBe` True it "should return False if doesn't contain 3" $ do solution 1 `shouldBe` False solution 264568 `shouldBe` False it "works with negative numbers" $ do solution (-49283) `shouldBe` True solution (-2849829) `shouldBe` False main = hspec spec
- module AreThereThreeSpec where
- import Test.Hspec
- import AreThereThree
- spec :: Spec
- spec = do
describe "Tests" $ do- describe "Fixed Tests" $ do
- it "should return True if contains 3" $ do
- solution 354523 `shouldBe` True
- solution 35 `shouldBe` True
- it "should return False if doesn't contain 3" $ do
- solution 1 `shouldBe` False
- solution 264568 `shouldBe` False
- it "works with negative numbers" $ do
- solution (-49283) `shouldBe` True
- solution (-2849829) `shouldBe` False
- main = hspec spec