exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

commit e2cc8a0a2b9f1fb04ca6499043b5fbb4637a7796
parent 3e5ea0ec702d4ddf8e854ff5d6ad224145529efa
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Fri,  1 Jul 2022 18:00:39 +0000

initial commit for series

Diffstat:
Aseries/HELP.md | 36++++++++++++++++++++++++++++++++++++
Aseries/README.md | 43+++++++++++++++++++++++++++++++++++++++++++
Aseries/Series.pm | 15+++++++++++++++
Aseries/series.t | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 247 insertions(+), 0 deletions(-)

diff --git a/series/HELP.md b/series/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 Series.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/series/README.md b/series/README.md @@ -0,0 +1,42 @@ +# Series + +Welcome to Series 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, output all the contiguous substrings of length `n` in +that string in the order that they appear. + +For example, the string "49142" has the following 3-digit series: + +- "491" +- "914" +- "142" + +And the following 4-digit series: + +- "4914" +- "9142" + +And if you ask for a 6-digit series from a 5-digit string, you deserve +whatever you get. + +Note that these series are only required to occupy *adjacent positions* +in the input; the digits need not be *numerically consecutive*. + +## Source + +### Created by + +- @autark + +### Contributed to by + +- @kytrinyx +- @m-dango +- @rfilipo + +### Based on + +A subset of the Problem 8 at Project Euler - http://projecteuler.net/problem=8 +\ No newline at end of file diff --git a/series/Series.pm b/series/Series.pm @@ -0,0 +1,15 @@ +package Series; + +use strict; +use warnings; +use feature qw<say>; + +use Exporter qw<import>; +our @EXPORT_OK = qw<slices>; + +sub slices { + my ($args) = @_; + return undef; +} + +1; diff --git a/series/series.t b/series/series.t @@ -0,0 +1,153 @@ +#!/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 Series qw<slices>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; + +imported_ok qw<slices> or bail_out; + +for my $case (@test_cases) { + if ( ref $case->{expected} ne 'HASH' ) { + is( slices( $case->{input} ), + $case->{expected}, $case->{description}, ); + } + else { + like dies( sub { slices( $case->{input} ) } ), + qr/$case->{expected}{error}/, $case->{description}; + } +} + +done_testing; + +__DATA__ +[ + { + "description": "slices of one from one", + "expected": [ + "1" + ], + "input": { + "series": "1", + "sliceLength": 1 + }, + "property": "slices" + }, + { + "description": "slices of one from two", + "expected": [ + "1", + "2" + ], + "input": { + "series": "12", + "sliceLength": 1 + }, + "property": "slices" + }, + { + "description": "slices of two", + "expected": [ + "35" + ], + "input": { + "series": "35", + "sliceLength": 2 + }, + "property": "slices" + }, + { + "description": "slices of two overlap", + "expected": [ + "91", + "14", + "42" + ], + "input": { + "series": "9142", + "sliceLength": 2 + }, + "property": "slices" + }, + { + "description": "slices can include duplicates", + "expected": [ + "777", + "777", + "777", + "777" + ], + "input": { + "series": "777777", + "sliceLength": 3 + }, + "property": "slices" + }, + { + "description": "slices of a long series", + "expected": [ + "91849", + "18493", + "84939", + "49390", + "93904", + "39042", + "90424", + "04243" + ], + "input": { + "series": "918493904243", + "sliceLength": 5 + }, + "property": "slices" + }, + { + "description": "slice length is too large", + "expected": { + "error": "slice length cannot be greater than series length" + }, + "input": { + "series": "12345", + "sliceLength": 6 + }, + "property": "slices" + }, + { + "description": "slice length cannot be zero", + "expected": { + "error": "slice length cannot be zero" + }, + "input": { + "series": "12345", + "sliceLength": 0 + }, + "property": "slices" + }, + { + "description": "slice length cannot be negative", + "expected": { + "error": "slice length cannot be negative" + }, + "input": { + "series": "123", + "sliceLength": -1 + }, + "property": "slices" + }, + { + "description": "empty series is invalid", + "expected": { + "error": "series cannot be empty" + }, + "input": { + "series": "", + "sliceLength": 1 + }, + "property": "slices" + } +]