Skip to content

Commit

Permalink
phone-number 1.2.0.3: enforce area/exchange not starting with 1
Browse files Browse the repository at this point in the history
1.0.1 and 1.0.2: description clarifications
exercism/problem-specifications#719
exercism/problem-specifications#724

1.1.0: enforce area/exchange not starting with 1
exercism/problem-specifications#745

1.2.0: 1a2b3c4d5e6f7g8h9i0j removed
exercism/problem-specifications#772

Closes #518
  • Loading branch information
petertseng committed May 9, 2017
1 parent 381d339 commit 0ed4ac7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
15 changes: 12 additions & 3 deletions exercises/phone-number/examples/success-standard/src/Phone.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
module Phone (number) where
import Data.Char (isDigit, isLetter)
import Data.Char (isDigit)

number :: String -> Maybe String
number input
| any isLetter input = Nothing
number input = clean input >>= check

check :: String -> Maybe String
check ('0':_) = Nothing
check ('1':_) = Nothing
check (_:_:_:'0':_) = Nothing
check (_:_:_:'1':_) = Nothing
check s = Just s

clean :: String -> Maybe String
clean input
| len == 10 = Just digits
| len == 11 && head digits == '1' = Just $ tail digits
| otherwise = Nothing
Expand Down
2 changes: 1 addition & 1 deletion exercises/phone-number/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: phone-number
version: 1.0.0.2
version: 1.2.0.3

dependencies:
- base
Expand Down
38 changes: 23 additions & 15 deletions exercises/phone-number/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,34 @@ data Case = Case { description :: String
cases :: [Case]
cases =
[ Case { description = "cleans the number"
, input = "(123) 456-7890"
, expected = Just "1234567890"
, input = "(223) 456-7890"
, expected = Just "2234567890"
}
, Case { description = "cleans number with dots"
, input = "123.456.7890"
, expected = Just "1234567890"
, Case { description = "cleans numbers with dots"
, input = "223.456.7890"
, expected = Just "2234567890"
}
, Case { description = "cleans numbers with multiple spaces"
, input = "123 456 7890 "
, expected = Just "1234567890"
, input = "223 456 7890 "
, expected = Just "2234567890"
}
, Case { description = "invalid when 9 digits"
, input = "123456789"
, expected = Nothing
}
, Case { description = "invalid when 11 digits"
, input = "21234567890"
, Case { description = "invalid when 11 digits does not start with a 1"
, input = "22234567890"
, expected = Nothing
}
, Case { description = "valid when 11 digits and first is 1"
, input = "11234567890"
, expected = Just "1234567890"
, Case { description = "valid when 11 digits and starting with 1"
, input = "12234567890"
, expected = Just "2234567890"
}
, Case { description = "valid when 11 digits and starting with 1 even with punctuation"
, input = "+1 (223) 456-7890"
, expected = Just "2234567890"
}
, Case { description = "invalid when 12 digits"
, Case { description = "invalid when more than 11 digits"
, input = "321234567890"
, expected = Nothing
}
Expand All @@ -58,8 +62,12 @@ cases =
, input = "123-@:!-7890"
, expected = Nothing
}
, Case { description = "invalid with right number of digits but letters mixed in"
, input = "1a2b3c4d5e6f7g8h9i0j"
, Case { description = "invalid if area code does not start with 2-9"
, input = "(123) 456-7890"
, expected = Nothing
}
, Case { description = "invalid if exchange code does not start with 2-9"
, input = "(223) 056-7890"
, expected = Nothing
}
]

0 comments on commit 0ed4ac7

Please sign in to comment.