exercism-perl5

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

commit abf3c6c1a114819c32972c4bf501471018310714
parent d12e409401189202c94559f57e072a237a831e42
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 25 Nov 2021 15:38:10 +0000

add matrix

Diffstat:
Amatrix/.exercism/config.json | 24++++++++++++++++++++++++
Amatrix/.exercism/metadata.json | 2++
Amatrix/HELP.md | 36++++++++++++++++++++++++++++++++++++
Amatrix/Matrix.pm | 26++++++++++++++++++++++++++
Amatrix/README.md | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amatrix/matrix-test.t | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amatrix/matrix.t | 130+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 361 insertions(+), 0 deletions(-)

diff --git a/matrix/.exercism/config.json b/matrix/.exercism/config.json @@ -0,0 +1,24 @@ +{ + "blurb": "Given a string representing a matrix of numbers, return the rows and columns of that matrix.", + "authors": [ + "bistik" + ], + "contributors": [ + "kytrinyx", + "m-dango", + "rfilipo" + ], + "files": { + "solution": [ + "Matrix.pm" + ], + "test": [ + "matrix.t" + ], + "example": [ + ".meta/solutions/Matrix.pm" + ] + }, + "source": "Warmup to the `saddle-points` warmup.", + "source_url": "http://jumpstartlab.com" +} diff --git a/matrix/.exercism/metadata.json b/matrix/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"perl5","exercise":"matrix","id":"00510e2ee65643928883a45fb6cd5058","url":"https://exercism.org/tracks/perl5/exercises/matrix","handle":"confidentidiot","is_requester":true,"auto_approve":false} +\ No newline at end of file diff --git a/matrix/HELP.md b/matrix/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 Matrix.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/matrix/Matrix.pm b/matrix/Matrix.pm @@ -0,0 +1,26 @@ +package Matrix; +use strict; +use warnings; +use Exporter qw<import>; +our @EXPORT_OK = qw<row column>; + +sub define_matrix { + my @matrix; + my @lines = split /\n/, shift; + push @matrix, [split / /] foreach (@lines); + return @matrix; +} + +sub row { + my ($input) = @_; + my $index = $input->{'index'}; + my @matrix = define_matrix($input->{'string'}); + return \@{$matrix[$index - 1]}; +} + +sub column { + my ($input) = @_; + return undef; +} + +1; diff --git a/matrix/README.md b/matrix/README.md @@ -0,0 +1,62 @@ +# Matrix + +Welcome to Matrix 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 representing a matrix of numbers, return the rows and columns of +that matrix. + +So given a string with embedded newlines like: + +```text +9 8 7 +5 3 2 +6 6 7 +``` + +representing this matrix: + +```text + 1 2 3 + |--------- +1 | 9 8 7 +2 | 5 3 2 +3 | 6 6 7 +``` + +your code should be able to spit out: + +- A list of the rows, reading each row left-to-right while moving + top-to-bottom across the rows, +- A list of the columns, reading each column top-to-bottom while moving + from left-to-right. + +The rows for our example matrix: + +- 9, 8, 7 +- 5, 3, 2 +- 6, 6, 7 + +And its columns: + +- 9, 5, 6 +- 8, 3, 6 +- 7, 2, 7 + +## Source + +### Created by + +- @bistik + +### Contributed to by + +- @kytrinyx +- @m-dango +- @rfilipo + +### Based on + +Warmup to the `saddle-points` warmup. - http://jumpstartlab.com +\ No newline at end of file diff --git a/matrix/matrix-test.t b/matrix/matrix-test.t @@ -0,0 +1,80 @@ +#!/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 Matrix qw<row column>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; + +imported_ok qw<row column> or bail_out; + +for my $case (@test_cases) { + my $func; + if ( $case->{property} eq 'row' ) { + $func = \&row; + } + elsif ( $case->{property} eq 'column' ) { + $func = \&column; + } + + is( $func->( $case->{input} ), + $case->{expected}, $case->{description}, ); +} + +done_testing; + +__DATA__ +[ + { + "description": "extract row from one number matrix", + "expected": [ + 1 + ], + "input": { + "index": 1, + "string": "1" + }, + "property": "row" + }, + { + "description": "can extract row", + "expected": [ + 3, + 4 + ], + "input": { + "index": 2, + "string": "1 2\n3 4" + }, + "property": "row" + }, + { + "description": "extract row where numbers have different widths", + "expected": [ + 10, + 20 + ], + "input": { + "index": 2, + "string": "1 2\n10 20" + }, + "property": "row" + }, + { + "description": "can extract row from non-square matrix with no corresponding column", + "expected": [ + 8, + 7, + 6 + ], + "input": { + "index": 4, + "string": "1 2 3\n4 5 6\n7 8 9\n8 7 6" + }, + "property": "row" + } +] diff --git a/matrix/matrix.t b/matrix/matrix.t @@ -0,0 +1,130 @@ +#!/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 Matrix qw<row column>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; + +imported_ok qw<row column> or bail_out; + +for my $case (@test_cases) { + my $func; + if ( $case->{property} eq 'row' ) { + $func = \&row; + } + elsif ( $case->{property} eq 'column' ) { + $func = \&column; + } + + is( $func->( $case->{input} ), + $case->{expected}, $case->{description}, ); +} + +done_testing; + +__DATA__ +[ + { + "description": "extract row from one number matrix", + "expected": [ + 1 + ], + "input": { + "index": 1, + "string": "1" + }, + "property": "row" + }, + { + "description": "can extract row", + "expected": [ + 3, + 4 + ], + "input": { + "index": 2, + "string": "1 2\n3 4" + }, + "property": "row" + }, + { + "description": "extract row where numbers have different widths", + "expected": [ + 10, + 20 + ], + "input": { + "index": 2, + "string": "1 2\n10 20" + }, + "property": "row" + }, + { + "description": "can extract row from non-square matrix with no corresponding column", + "expected": [ + 8, + 7, + 6 + ], + "input": { + "index": 4, + "string": "1 2 3\n4 5 6\n7 8 9\n8 7 6" + }, + "property": "row" + }, + { + "description": "extract column from one number matrix", + "expected": [ + 1 + ], + "input": { + "index": 1, + "string": "1" + }, + "property": "column" + }, + { + "description": "can extract column", + "expected": [ + 3, + 6, + 9 + ], + "input": { + "index": 3, + "string": "1 2 3\n4 5 6\n7 8 9" + }, + "property": "column" + }, + { + "description": "can extract column from non-square matrix with no corresponding row", + "expected": [ + 4, + 8, + 6 + ], + "input": { + "index": 4, + "string": "1 2 3 4\n5 6 7 8\n9 8 7 6" + }, + "property": "column" + }, + { + "description": "extract column where numbers have different widths", + "expected": [ + 1903, + 3, + 4 + ], + "input": { + "index": 2, + "string": "89 1903 3\n18 3 1\n9 4 800" + }, + "property": "column" + } +]