From c4dd5345cb5ec4b75485af5e1dccde179fca4333 Mon Sep 17 00:00:00 2001 From: icyrockcom Date: Mon, 24 Apr 2017 22:37:53 -0400 Subject: [PATCH] Added Largest Series Product exercise --- config.json | 7 ++ exercises/largest-series-product/bower.json | 16 ++++ .../examples/src/LargestSeriesProduct.purs | 31 ++++++++ .../src/LargestSeriesProduct.purs | 3 + .../largest-series-product/test/Main.purs | 73 +++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 exercises/largest-series-product/bower.json create mode 100644 exercises/largest-series-product/examples/src/LargestSeriesProduct.purs create mode 100644 exercises/largest-series-product/src/LargestSeriesProduct.purs create mode 100644 exercises/largest-series-product/test/Main.purs diff --git a/config.json b/config.json index b5ceeb9..03ad0bf 100644 --- a/config.json +++ b/config.json @@ -159,6 +159,13 @@ "topics": [ "maps" ] + }, + { + "slug": "largest-series-product", + "difficulty": 1, + "topics": [ + "strings" + ] } ], "deprecated": [ diff --git a/exercises/largest-series-product/bower.json b/exercises/largest-series-product/bower.json new file mode 100644 index 0000000..57c09ac --- /dev/null +++ b/exercises/largest-series-product/bower.json @@ -0,0 +1,16 @@ +{ + "name": "largest-series-product", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "output" + ], + "dependencies": { + "purescript-prelude": "^3.0.0" + }, + "devDependencies": { + "purescript-psci-support": "^3.0.0", + "purescript-test-unit": "^11.0.0" + } +} diff --git a/exercises/largest-series-product/examples/src/LargestSeriesProduct.purs b/exercises/largest-series-product/examples/src/LargestSeriesProduct.purs new file mode 100644 index 0000000..f6fca03 --- /dev/null +++ b/exercises/largest-series-product/examples/src/LargestSeriesProduct.purs @@ -0,0 +1,31 @@ +module LargestSeriesProduct + ( largestProduct + ) where + +import Prelude +import Data.Array (drop, length, take, (:)) +import Data.Foldable (maximum, product) +import Data.Int (fromString) +import Data.Maybe (Maybe(..)) +import Data.String (singleton, toCharArray) +import Data.Traversable (sequence) + +spans :: forall a. Int -> Array a -> Array (Array a) +spans n a + | length a < n = [] + | otherwise = take n a : spans n (drop 1 a) + +toIntArray :: String -> Maybe (Array Int) +toIntArray = toCharArray + >>> map (singleton >>> fromString) + >>> sequence + +largestProduct :: String -> Int -> Maybe Int +largestProduct digits span + | span == 0 = Just 1 + | span < 0 = Nothing + | otherwise = do + is <- toIntArray digits + spans span is + # map product + # maximum diff --git a/exercises/largest-series-product/src/LargestSeriesProduct.purs b/exercises/largest-series-product/src/LargestSeriesProduct.purs new file mode 100644 index 0000000..6ed66d9 --- /dev/null +++ b/exercises/largest-series-product/src/LargestSeriesProduct.purs @@ -0,0 +1,3 @@ +module LargestSeriesProduct + ( largestProduct + ) where diff --git a/exercises/largest-series-product/test/Main.purs b/exercises/largest-series-product/test/Main.purs new file mode 100644 index 0000000..8c05472 --- /dev/null +++ b/exercises/largest-series-product/test/Main.purs @@ -0,0 +1,73 @@ +module Test.Main where + +import Prelude +import Control.Monad.Eff (Eff) +import Data.Maybe (Maybe(..)) +import Test.Unit.Assert as Assert +import Test.Unit (suite, test) +import Test.Unit.Main (runTest) +import LargestSeriesProduct (largestProduct) + +main :: Eff _ Unit +main = runTest do + suite "LargestSeriesProduct.largestProduct" do + + test "finds the largest product if span equals length" $ + Assert.equal (Just 18) + (largestProduct "29" 2) + + test "can find the largest product of 2 with numbers in order" $ + Assert.equal (Just 72) + (largestProduct "0123456789" 2) + + test "can find the largest product of 2" $ + Assert.equal (Just 48) + (largestProduct "576802143" 2) + + test "can find the largest product of 3 with numbers in order" $ + Assert.equal (Just 504) + (largestProduct "0123456789" 3) + + test "can find the largest product of 3" $ + Assert.equal (Just 270) + (largestProduct "1027839564" 3) + + test "can find the largest product of 5 with numbers in order" $ + Assert.equal (Just 15120) + (largestProduct "0123456789" 5) + + test "can get the largest product of a big number" $ + Assert.equal (Just 23520) + (largestProduct "73167176531330624919225119674426574742355349194934" 6) + + test "reports zero if the only digits are zero" $ + Assert.equal (Just 0) + (largestProduct "0000" 2) + + test "reports zero if all spans include zero" $ + Assert.equal (Just 0) + (largestProduct "99099" 3) + + test "rejects span longer than string length" $ + Assert.equal Nothing + (largestProduct "123" 4) + + test "reports 1 for empty string and empty product (0 span)" $ + Assert.equal (Just 1) + (largestProduct "" 0) + + test "reports 1 for nonempty string and empty product (0 span)" $ + Assert.equal (Just 1) + (largestProduct "123" 0) + + test "rejects empty string and nonzero span" $ + Assert.equal Nothing + (largestProduct "" 1) + + test "rejects invalid character in digits" $ + Assert.equal Nothing + (largestProduct "1234a5" 2) + + test "rejects negative span" $ + Assert.equal Nothing + (largestProduct "12345" (-1))