-
-
Notifications
You must be signed in to change notification settings - Fork 35
Added Largest Series Product exercise #44
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
exercises/largest-series-product/examples/src/LargestSeriesProduct.purs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| >>> 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 | ||
3 changes: 3 additions & 0 deletions
3
exercises/largest-series-product/src/LargestSeriesProduct.purs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module LargestSeriesProduct | ||
| ( largestProduct | ||
| ) where |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map f >>> sequenceis justtraverse fUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.