Skip to content

largest-series-product corner cases #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions largest-series-product/example.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ slices :: Integral a => Int -> String -> [[a]]
slices n = go . digits
where go xs = map (take n) $ take (length xs - pred n) (tails xs)

largestProduct :: Integral a => Int -> String -> a
largestProduct :: Integral a => Int -> String -> Maybe a
largestProduct n text = case map product (slices n text) of
[] -> 1
products -> maximum products
[] -> Nothing
products -> Just $ maximum products
15 changes: 10 additions & 5 deletions largest-series-product/largest-series-product_test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ main = exitProperly $ runTestTT $ TestList
ints :: [Int] -> [Int]
ints = id

int :: Int -> Int
int = id
int :: Int -> Maybe Int
int = Just

intNothing :: Maybe Int
intNothing = Nothing

seriesTests :: [Test]
seriesTests = map TestCase
Expand All @@ -43,8 +46,10 @@ seriesTests = map TestCase
, int 28350 @=?
largestProduct 6 "52677741234314237566414902593461595376319419139427"
, int 1 @=? largestProduct 0 ""
-- unlike the Ruby implementation, no error is expected for too small input
, int 1 @=? largestProduct 4 "123"
-- edge case :)
, int 1 @=? largestProduct 0 "123"
, intNothing @=? largestProduct 1 ""
, intNothing @=? largestProduct 4 "123"
-- if all spans contain zero, result is zero.
, int 0 @=? largestProduct 3 "99099"
, int 0 @=? largestProduct 2 "00"
]