Leading characters that are not letters should be ignored.
Added relevant test cases.
module ToUpperFirst where import Data.Char (toUpper, isLetter) toUpperFirst :: String -> String toUpperFirst str = case break isLetter str of (xs, y:ys) -> xs ++ toUpper y : ys _ -> str
- module ToUpperFirst where
import Data.Char (toUpper, isSpace)toUpperFirst (x : xs) = if isSpace x then x : toUpperFirst xs else toUpper x : xstoUpperFirst _ = ""- import Data.Char (toUpper, isLetter)
- toUpperFirst :: String -> String
- toUpperFirst str =
- case break isLetter str of
- (xs, y:ys) -> xs ++ toUpper y : ys
- _ -> str
module ExampleSpec where import Test.Hspec import ToUpperFirst spec :: Spec spec = do describe "toUpperFirst" $ do it "should capitalize first letter of a string" $ do toUpperFirst "" `shouldBe` "" toUpperFirst "finn the human" `shouldBe` "Finn the human" toUpperFirst "alice" `shouldBe` "Alice" toUpperFirst "Joe" `shouldBe` "Joe" toUpperFirst " bob" `shouldBe` " Bob" toUpperFirst "\nsteven" `shouldBe` "\nSteven" it "should work if the first letter occurs after digits" $ do toUpperFirst "3fish" `shouldBe` "3Fish" toUpperFirst "383\n 38boB" `shouldBe` "383\n 38BoB"
- module ExampleSpec where
- --- Tests can be written using Hspec http://hspec.github.io/
- --- Replace this with your own tests.
- import Test.Hspec
- import ToUpperFirst
- --- `spec` of type `Spec` must exist
- spec :: Spec
- spec = do
describe "toUpperFirst" $ doit "should capitalize first letter of a string" $ dotoUpperFirst "" `shouldBe` ""toUpperFirst "finn the human" `shouldBe` "Finn the human"toUpperFirst "alice" `shouldBe` "Alice"toUpperFirst "Joe" `shouldBe` "Joe"toUpperFirst " bob" `shouldBe` " Bob"toUpperFirst "steven" `shouldBe` "Steven"-- the following line is optional for 8.2main = hspec spec- describe "toUpperFirst" $ do
- it "should capitalize first letter of a string" $ do
- toUpperFirst "" `shouldBe` ""
- toUpperFirst "finn the human" `shouldBe` "Finn the human"
- toUpperFirst "alice" `shouldBe` "Alice"
- toUpperFirst "Joe" `shouldBe` "Joe"
- toUpperFirst " bob" `shouldBe` " Bob"
- toUpperFirst "
- steven" `shouldBe` "
- Steven"
- it "should work if the first letter occurs after digits" $ do
- toUpperFirst "3fish" `shouldBe` "3Fish"
- toUpperFirst "383\n 38boB" `shouldBe` "383\n 38BoB"
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