Skip to content

Commit a27f1af

Browse files
committed
Solution for largest-series-product
1 parent f5c7d09 commit a27f1af

File tree

6 files changed

+247
-0
lines changed

6 files changed

+247
-0
lines changed

largest-series-product/README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Largest Series Product
2+
3+
Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.
4+
5+
For example, for the input `'1027839564'`, the largest product for a
6+
series of 3 digits is 270 (9 * 5 * 6), and the largest product for a
7+
series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5).
8+
9+
Note that these series are only required to occupy *adjacent positions*
10+
in the input; the digits need not be *numerically consecutive*.
11+
12+
For the input `'73167176531330624919225119674426574742355349194934'`,
13+
the largest product for a series of 6 digits is 23520.
14+
15+
16+
## Getting Started
17+
18+
For installation and learning resources, refer to the
19+
[exercism help page](http://exercism.io/languages/haskell).
20+
21+
## Running the tests
22+
23+
To run the test suite, execute the following command:
24+
25+
```bash
26+
stack test
27+
```
28+
29+
#### If you get an error message like this...
30+
31+
```
32+
No .cabal file found in directory
33+
```
34+
35+
You are probably running an old stack version and need
36+
to upgrade it.
37+
38+
#### Otherwise, if you get an error message like this...
39+
40+
```
41+
No compiler found, expected minor version match with...
42+
Try running "stack setup" to install the correct GHC...
43+
```
44+
45+
Just do as it says and it will download and install
46+
the correct compiler version:
47+
48+
```bash
49+
stack setup
50+
```
51+
52+
## Running *GHCi*
53+
54+
If you want to play with your solution in GHCi, just run the command:
55+
56+
```bash
57+
stack ghci
58+
```
59+
60+
## Feedback, Issues, Pull Requests
61+
62+
The [exercism/xhaskell](https://github.com/exercism/xhaskell) repository on
63+
GitHub is the home for all of the Haskell exercises.
64+
65+
If you have feedback about an exercise, or want to help implementing a new
66+
one, head over there and create an issue. We'll do our best to help you!
67+
68+
## Source
69+
70+
A variation on Problem 8 at Project Euler [http://projecteuler.net/problem=8](http://projecteuler.net/problem=8)
71+
72+
## Submitting Incomplete Problems
73+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
74+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- This file has been generated from package.yaml by hpack version 0.15.0.
2+
--
3+
-- see: https://github.com/sol/hpack
4+
5+
name: largest-series-product
6+
version: 0.0.0
7+
build-type: Simple
8+
cabal-version: >= 1.10
9+
10+
library
11+
hs-source-dirs:
12+
src
13+
build-depends:
14+
base
15+
exposed-modules:
16+
Series
17+
other-modules:
18+
Paths_largest_series_product
19+
default-language: Haskell2010
20+
21+
test-suite test
22+
type: exitcode-stdio-1.0
23+
main-is: Tests.hs
24+
hs-source-dirs:
25+
test
26+
build-depends:
27+
base
28+
, largest-series-product
29+
, hspec
30+
default-language: Haskell2010

largest-series-product/package.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: largest-series-product
2+
3+
dependencies:
4+
- base
5+
6+
library:
7+
exposed-modules: Series
8+
source-dirs: src
9+
dependencies:
10+
# - foo # List here the packages you
11+
# - bar # want to use in your solution.
12+
13+
tests:
14+
test:
15+
main: Tests.hs
16+
source-dirs: test
17+
dependencies:
18+
- largest-series-product
19+
- hspec

largest-series-product/src/Series.hs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Series (largestProduct) where
2+
3+
import Data.Char
4+
import Data.Maybe
5+
6+
largestProduct :: Int -> String -> Maybe Integer
7+
largestProduct size xs | length xs >= size && onlyDigit && size >= 0 = reccur its size xs
8+
| otherwise = Nothing
9+
where
10+
onlyDigit = all isDigit xs
11+
l = length xs
12+
its = l - size + 1
13+
14+
reccur :: Int -> Int -> String -> Maybe Integer
15+
reccur 0 _ _ = Just 0
16+
reccur its size xs = if p > next then p else next
17+
where
18+
p = Just . fromIntegral . product $ map digitToInt (take size xs)
19+
next = reccur (its - 1 ) size $ tail xs

largest-series-product/stack.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
resolver: lts-8.2

largest-series-product/test/Tests.hs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
2+
3+
import Test.Hspec (Spec, describe, it, shouldBe)
4+
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
5+
6+
import Series (largestProduct)
7+
8+
main :: IO ()
9+
main = hspecWith defaultConfig {configFastFail = True} specs
10+
11+
specs :: Spec
12+
specs = describe "largest-series-product" $
13+
14+
-- Test cases adapted from `exercism/x-common/largest-series-product.json`
15+
-- on 2016-07-27.
16+
17+
describe "largestProduct" $ do
18+
19+
it "can find the largest product of 2 with numbers in order" $
20+
largestProduct 2 "0123456789"
21+
`shouldBe` Just 72
22+
23+
it "can find the largest product of 2" $
24+
largestProduct 2 "576802143"
25+
`shouldBe` Just 48
26+
27+
it "finds the largest product if span equals length" $
28+
largestProduct 2 "29"
29+
`shouldBe` Just 18
30+
31+
it "can find the largest product of 3 with numbers in order" $
32+
largestProduct 3 "0123456789"
33+
`shouldBe` Just 504
34+
35+
it "can find the largest product of 3" $
36+
largestProduct 3 "1027839564"
37+
`shouldBe` Just 270
38+
39+
it "can find the largest product of 5 with numbers in order" $
40+
largestProduct 5 "0123456789"
41+
`shouldBe` Just 15120
42+
43+
it "can get the largest product of a big number" $
44+
largestProduct 6 "73167176531330624919225119674426574742355349194934"
45+
`shouldBe` Just 23520
46+
47+
it "can get the largest product of a big number II" $
48+
largestProduct 6 "52677741234314237566414902593461595376319419139427"
49+
`shouldBe` Just 28350
50+
51+
it "can get the largest product of a big number (Project Euler)" $
52+
largestProduct 13 "73167176531330624919225119674426574742355349194934\
53+
\96983520312774506326239578318016984801869478851843\
54+
\85861560789112949495459501737958331952853208805511\
55+
\12540698747158523863050715693290963295227443043557\
56+
\66896648950445244523161731856403098711121722383113\
57+
\62229893423380308135336276614282806444486645238749\
58+
\30358907296290491560440772390713810515859307960866\
59+
\70172427121883998797908792274921901699720888093776\
60+
\65727333001053367881220235421809751254540594752243\
61+
\52584907711670556013604839586446706324415722155397\
62+
\53697817977846174064955149290862569321978468622482\
63+
\83972241375657056057490261407972968652414535100474\
64+
\82166370484403199890008895243450658541227588666881\
65+
\16427171479924442928230863465674813919123162824586\
66+
\17866458359124566529476545682848912883142607690042\
67+
\24219022671055626321111109370544217506941658960408\
68+
\07198403850962455444362981230987879927244284909188\
69+
\84580156166097919133875499200524063689912560717606\
70+
\05886116467109405077541002256983155200055935729725\
71+
\71636269561882670428252483600823257530420752963450"
72+
`shouldBe` Just 23514624000
73+
74+
it "reports zero if the only digits are zero" $
75+
largestProduct 2 "0000"
76+
`shouldBe` Just 0
77+
78+
it "reports zero if all spans include zero" $
79+
largestProduct 3 "99099"
80+
`shouldBe` Just 0
81+
82+
it "rejects span longer than string length" $
83+
largestProduct 4 "123"
84+
`shouldBe` Nothing
85+
86+
it "reports 1 for empty string and empty product (0 span)" $
87+
largestProduct 0 ""
88+
`shouldBe` Just 1
89+
90+
it "reports 1 for nonempty string and empty product (0 span)" $
91+
largestProduct 0 "123"
92+
`shouldBe` Just 1
93+
94+
it "rejects empty string and nonzero span" $
95+
largestProduct 1 ""
96+
`shouldBe` Nothing
97+
98+
it "rejects invalid character in digits" $
99+
largestProduct 2 "1234a5"
100+
`shouldBe` Nothing
101+
102+
it "rejects negative span" $
103+
largestProduct (-1) "12345"
104+
`shouldBe` Nothing

0 commit comments

Comments
 (0)