diff --git a/largest-series-product/example.hs b/largest-series-product/example.hs index 861dc372e..f11d05df0 100644 --- a/largest-series-product/example.hs +++ b/largest-series-product/example.hs @@ -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 diff --git a/largest-series-product/largest-series-product_test.hs b/largest-series-product/largest-series-product_test.hs index 2ca8dc9ad..88362aaf8 100644 --- a/largest-series-product/largest-series-product_test.hs +++ b/largest-series-product/largest-series-product_test.hs @@ -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 @@ -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" ]