Skip to content
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
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@
"topics": [
"maps"
]
},
{
"slug": "largest-series-product",
"difficulty": 1,
"topics": [
"strings"
]
}
],
"deprecated": [
Expand Down
16 changes: 16 additions & 0 deletions exercises/largest-series-product/bower.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map f >>> sequence is just traverse f

Copy link
Contributor Author

@icyrockcom icyrockcom Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @paf31, missed that one, I'll try to remember :) I've pushed an update icyrockcom@25711b0 if you prefer to merge that one.

>>> 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module LargestSeriesProduct
( largestProduct
) where
73 changes: 73 additions & 0 deletions exercises/largest-series-product/test/Main.purs
Original file line number Diff line number Diff line change
@@ -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))