commit 052415c68e901c47819e113ac6d0ecef30969193
parent cac7dabefda10a1f578b2bce6ddf9edaac003b1b
Author: Samir Parikh <noreply@samirparikh.com>
Date: Wed, 20 Apr 2022 14:42:23 +0000
initial commit for largest series product
Diffstat:
4 files changed, 275 insertions(+), 0 deletions(-)
diff --git a/largest-series-product/HELP.md b/largest-series-product/HELP.md
@@ -0,0 +1,35 @@
+# Help
+
+## Running the tests
+
+There is a Perl 5 script with the extension `.t`, which will be used to test
+your solution. You can run through the tests by using the command:
+
+```bash
+`prove .`
+```
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit LargestSeriesProduct.pm` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Perl 5 track's documentation](https://exercism.org/docs/tracks/perl5)
+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+To get help if you're having trouble, you can use one of the following resources:
+
+- [/r/perl5](https://www.reddit.com/r/perl5) is the perl5 subreddit.
+- [StackOverflow](http://stackoverflow.com/questions/tagged/perl5) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
+\ No newline at end of file
diff --git a/largest-series-product/LargestSeriesProduct.pm b/largest-series-product/LargestSeriesProduct.pm
@@ -0,0 +1,24 @@
+package LargestSeriesProduct;
+use strict;
+use warnings;
+use List::Util qw( product );
+use Exporter qw<import>;
+our @EXPORT_OK = qw<largest_product>;
+
+sub largest_product {
+ my ($input) = @_;
+ my $max = 0;
+ #print "$input->{digits} and $input->{span}\n";
+ my @digits = split //, $input->{digits};
+ my $length = scalar @digits;
+ #print "length = $length\n";
+ #print "$_\n" foreach @digits;
+ foreach my $i (0 .. $length - $input->{span}) {
+ my @substring = @digits[$i .. ($i + $input->{span} - 1)];
+ my $product = product @substring;
+ $max = $product if ($product > $max);
+ }
+ return $max;
+}
+
+1;
diff --git a/largest-series-product/README.md b/largest-series-product/README.md
@@ -0,0 +1,41 @@
+# Largest Series Product
+
+Welcome to Largest Series Product on Exercism's Perl 5 Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+Given a string of digits, calculate the largest product for a contiguous
+substring of digits of length n.
+
+For example, for the input `'1027839564'`, the largest product for a
+series of 3 digits is 270 (9 \* 5 \* 6), and the largest product for a
+series of 5 digits is 7560 (7 \* 8 \* 3 \* 9 \* 5).
+
+Note that these series are only required to occupy *adjacent positions*
+in the input; the digits need not be *numerically consecutive*.
+
+For the input `'73167176531330624919225119674426574742355349194934'`,
+the largest product for a series of 6 digits is 23520.
+
+For a series of zero digits, the largest product is 1 because 1 is the multiplicative identity.
+(You don't need to know what a multiplicative identity is to solve this problem;
+it just means that multiplying a number by 1 gives you the same number.)
+
+## Source
+
+### Created by
+
+- @bistik
+
+### Contributed to by
+
+- @dogsleg
+- @kytrinyx
+- @m-dango
+- @petertseng
+- @rfilipo
+
+### Based on
+
+A variation on Problem 8 at Project Euler - http://projecteuler.net/problem=8
+\ No newline at end of file
diff --git a/largest-series-product/largest-series-product.t b/largest-series-product/largest-series-product.t
@@ -0,0 +1,173 @@
+#!/usr/bin/env perl
+use Test2::V0;
+use JSON::PP;
+use constant JSON => JSON::PP->new;
+
+use FindBin qw<$Bin>;
+use lib $Bin, "$Bin/local/lib/perl5";
+
+use LargestSeriesProduct qw<largest_product>;
+
+my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
+
+imported_ok qw<largest_product> or bail_out;
+
+for my $case (@test_cases) {
+ if ( !ref $case->{expected} ) {
+ is( largest_product( $case->{input} ),
+ $case->{expected}, $case->{description}, );
+ }
+ else {
+ like dies( sub { largest_product( $case->{input} ) } ),
+ qr/$case->{expected}{error}/, $case->{description};
+ }
+}
+
+done_testing;
+
+__DATA__
+[
+ {
+ "description": "finds the largest product if span equals length",
+ "expected": 18,
+ "input": {
+ "digits": "29",
+ "span": 2
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "can find the largest product of 2 with numbers in order",
+ "expected": 72,
+ "input": {
+ "digits": "0123456789",
+ "span": 2
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "can find the largest product of 2",
+ "expected": 48,
+ "input": {
+ "digits": "576802143",
+ "span": 2
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "can find the largest product of 3 with numbers in order",
+ "expected": 504,
+ "input": {
+ "digits": "0123456789",
+ "span": 3
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "can find the largest product of 3",
+ "expected": 270,
+ "input": {
+ "digits": "1027839564",
+ "span": 3
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "can find the largest product of 5 with numbers in order",
+ "expected": 15120,
+ "input": {
+ "digits": "0123456789",
+ "span": 5
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "can get the largest product of a big number",
+ "expected": 23520,
+ "input": {
+ "digits": "73167176531330624919225119674426574742355349194934",
+ "span": 6
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "reports zero if the only digits are zero",
+ "expected": 0,
+ "input": {
+ "digits": "0000",
+ "span": 2
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "reports zero if all spans include zero",
+ "expected": 0,
+ "input": {
+ "digits": "99099",
+ "span": 3
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "rejects span longer than string length",
+ "expected": {
+ "error": "span must be smaller than string length"
+ },
+ "input": {
+ "digits": "123",
+ "span": 4
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "reports 1 for empty string and empty product (0 span)",
+ "expected": 1,
+ "input": {
+ "digits": "",
+ "span": 0
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "reports 1 for nonempty string and empty product (0 span)",
+ "expected": 1,
+ "input": {
+ "digits": "123",
+ "span": 0
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "rejects empty string and nonzero span",
+ "expected": {
+ "error": "span must be smaller than string length"
+ },
+ "input": {
+ "digits": "",
+ "span": 1
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "rejects invalid character in digits",
+ "expected": {
+ "error": "digits input must only contain digits"
+ },
+ "input": {
+ "digits": "1234a5",
+ "span": 2
+ },
+ "property": "largestProduct"
+ },
+ {
+ "description": "rejects negative span",
+ "expected": {
+ "error": "span must be greater than zero"
+ },
+ "input": {
+ "digits": "12345",
+ "span": -1
+ },
+ "property": "largestProduct"
+ }
+]