From 5a1730a8eeba1975756d2a68cf9f61e18e949cb4 Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Sat, 23 Jan 2016 10:34:01 -0800 Subject: [PATCH 1/3] largest-series-product: Nothing for too small input The reasoning behind this is that there are zero ways to make a 4-character string out of "123", so to find the max product is to take the max of an empty list, which is an error. (Compare with the case of taking a 0-character string, of which there is one, so that case is not an error) These are consistent with the values defined in https://github.com/exercism/x-common/blob/master/largest-series-product.json --- largest-series-product/example.hs | 6 +++--- largest-series-product/largest-series-product_test.hs | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) 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..bd2081527 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,8 @@ 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" + , intNothing @=? largestProduct 1 "" + , intNothing @=? largestProduct 4 "123" -- edge case :) , int 0 @=? largestProduct 2 "00" ] From 545af938c2ba2c8c217c9a09fa8b06903deb4583 Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Sat, 23 Jan 2016 10:36:33 -0800 Subject: [PATCH 2/3] largest-series-product: Test for zero span, nonempty string It is prudent to ensure that every combination of edge/non-edge are tested for the two inputs. --- largest-series-product/largest-series-product_test.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/largest-series-product/largest-series-product_test.hs b/largest-series-product/largest-series-product_test.hs index bd2081527..f0c84d000 100644 --- a/largest-series-product/largest-series-product_test.hs +++ b/largest-series-product/largest-series-product_test.hs @@ -46,6 +46,7 @@ seriesTests = map TestCase , int 28350 @=? largestProduct 6 "52677741234314237566414902593461595376319419139427" , int 1 @=? largestProduct 0 "" + , int 1 @=? largestProduct 0 "123" , intNothing @=? largestProduct 1 "" , intNothing @=? largestProduct 4 "123" -- edge case :) From cee0dde7c81b6e80736c20fdd568d9324670c19e Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Sat, 23 Jan 2016 10:37:58 -0800 Subject: [PATCH 3/3] largest-series-product: Add a case where all spans contain 0 This is distinct from the existing case because there are nonzero digits in this string as well. --- largest-series-product/largest-series-product_test.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/largest-series-product/largest-series-product_test.hs b/largest-series-product/largest-series-product_test.hs index f0c84d000..88362aaf8 100644 --- a/largest-series-product/largest-series-product_test.hs +++ b/largest-series-product/largest-series-product_test.hs @@ -49,6 +49,7 @@ seriesTests = map TestCase , int 1 @=? largestProduct 0 "123" , intNothing @=? largestProduct 1 "" , intNothing @=? largestProduct 4 "123" - -- edge case :) + -- if all spans contain zero, result is zero. + , int 0 @=? largestProduct 3 "99099" , int 0 @=? largestProduct 2 "00" ]