exercism-perl5

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

commit 79e02b630ffe9463f526e06f33a1e687354bece6
parent bf3ab33b3afde9fc29965b16431f1d5b5c232ace
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Tue, 21 Dec 2021 22:02:27 +0000

initial commit for sublist

Diffstat:
Asublist/HELP.md | 36++++++++++++++++++++++++++++++++++++
Asublist/README.md | 36++++++++++++++++++++++++++++++++++++
Asublist/Sublist.pm | 34++++++++++++++++++++++++++++++++++
Asublist/sublist.t | 320+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 426 insertions(+), 0 deletions(-)

diff --git a/sublist/HELP.md b/sublist/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 Sublist.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/sublist/README.md b/sublist/README.md @@ -0,0 +1,35 @@ +# Sublist + +Welcome to Sublist on Exercism's Perl 5 Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given two lists determine if the first list is contained within the second +list, if the second list is contained within the first list, if both lists are +contained within each other or if none of these are true. + +Specifically, a list A is a sublist of list B if by dropping 0 or more elements +from the front of B and 0 or more elements from the back of B you get a list +that's completely equal to A. + +Examples: + + * A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B + * A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B + * A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B + * A = [1, 2, 3], B = [1, 2, 3], A is equal to B + * A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B + * A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B + +## Source + +### Created by + +- @autark + +### Contributed to by + +- @kytrinyx +- @m-dango +- @rfilipo +\ No newline at end of file diff --git a/sublist/Sublist.pm b/sublist/Sublist.pm @@ -0,0 +1,34 @@ +package Sublist; +use strict; +use warnings; +use v5.22; +use Exporter qw<import>; +our @EXPORT_OK = qw<compare_lists>; + +sub a_within_b { + my ($a, $b) = @_; + my @a = @{$a}; + my @b = @{$b}; + foreach my $element (@a) { + #print "checking $_ within @b ... "; + #my $found = grep { /$_/ } @b; + #say "found $found"; + return 0 unless (grep /^$element$/, @b); # grep needs expr that evanlates + # to true or false + } + return 1; +} + +sub compare_lists { + my ($args) = @_; + my @a = @{$args->{listOne}}; + my @b = @{$args->{listTwo}}; + return "equal" if (@a == 0 && @b == 0); # empty lists + return "sublist" if (@a == 0 && @b > 0); # empty list within non empty list + return "superlist" if (@a > 0 && @b == 0); # non empty list contains empty list + return "equal" if (@a == @b && a_within_b(\@a, \@b)); + return "sublist" if (a_within_b(\@a, \@b)); + return "unequal"; +} + +1; diff --git a/sublist/sublist.t b/sublist/sublist.t @@ -0,0 +1,320 @@ +#!/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 Sublist qw<compare_lists>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; + +imported_ok qw<compare_lists> or bail_out; + +for my $case (@test_cases) { + is( compare_lists( $case->{input} ), + $case->{expected}, $case->{description}, ); +} + +done_testing; + +__DATA__ +[ + { + "description": "empty lists", + "expected": "equal", + "input": { + "listOne": [], + "listTwo": [] + }, + "property": "sublist" + }, + { + "description": "empty list within non empty list", + "expected": "sublist", + "input": { + "listOne": [], + "listTwo": [ + 1, + 2, + 3 + ] + }, + "property": "sublist" + }, + { + "description": "non empty list contains empty list", + "expected": "superlist", + "input": { + "listOne": [ + 1, + 2, + 3 + ], + "listTwo": [] + }, + "property": "sublist" + }, + { + "description": "list equals itself", + "expected": "equal", + "input": { + "listOne": [ + 1, + 2, + 3 + ], + "listTwo": [ + 1, + 2, + 3 + ] + }, + "property": "sublist" + }, + { + "description": "different lists", + "expected": "unequal", + "input": { + "listOne": [ + 1, + 2, + 3 + ], + "listTwo": [ + 2, + 3, + 4 + ] + }, + "property": "sublist" + }, + { + "description": "false start", + "expected": "sublist", + "input": { + "listOne": [ + 1, + 2, + 5 + ], + "listTwo": [ + 0, + 1, + 2, + 3, + 1, + 2, + 5, + 6 + ] + }, + "property": "sublist" + }, + { + "description": "consecutive", + "expected": "sublist", + "input": { + "listOne": [ + 1, + 1, + 2 + ], + "listTwo": [ + 0, + 1, + 1, + 1, + 2, + 1, + 2 + ] + }, + "property": "sublist" + }, + { + "description": "sublist at start", + "expected": "sublist", + "input": { + "listOne": [ + 0, + 1, + 2 + ], + "listTwo": [ + 0, + 1, + 2, + 3, + 4, + 5 + ] + }, + "property": "sublist" + }, + { + "description": "sublist in middle", + "expected": "sublist", + "input": { + "listOne": [ + 2, + 3, + 4 + ], + "listTwo": [ + 0, + 1, + 2, + 3, + 4, + 5 + ] + }, + "property": "sublist" + }, + { + "description": "sublist at end", + "expected": "sublist", + "input": { + "listOne": [ + 3, + 4, + 5 + ], + "listTwo": [ + 0, + 1, + 2, + 3, + 4, + 5 + ] + }, + "property": "sublist" + }, + { + "description": "at start of superlist", + "expected": "superlist", + "input": { + "listOne": [ + 0, + 1, + 2, + 3, + 4, + 5 + ], + "listTwo": [ + 0, + 1, + 2 + ] + }, + "property": "sublist" + }, + { + "description": "in middle of superlist", + "expected": "superlist", + "input": { + "listOne": [ + 0, + 1, + 2, + 3, + 4, + 5 + ], + "listTwo": [ + 2, + 3 + ] + }, + "property": "sublist" + }, + { + "description": "at end of superlist", + "expected": "superlist", + "input": { + "listOne": [ + 0, + 1, + 2, + 3, + 4, + 5 + ], + "listTwo": [ + 3, + 4, + 5 + ] + }, + "property": "sublist" + }, + { + "description": "first list missing element from second list", + "expected": "unequal", + "input": { + "listOne": [ + 1, + 3 + ], + "listTwo": [ + 1, + 2, + 3 + ] + }, + "property": "sublist" + }, + { + "description": "second list missing element from first list", + "expected": "unequal", + "input": { + "listOne": [ + 1, + 2, + 3 + ], + "listTwo": [ + 1, + 3 + ] + }, + "property": "sublist" + }, + { + "description": "order matters to a list", + "expected": "unequal", + "input": { + "listOne": [ + 1, + 2, + 3 + ], + "listTwo": [ + 3, + 2, + 1 + ] + }, + "property": "sublist" + }, + { + "description": "same digits but different numbers", + "expected": "unequal", + "input": { + "listOne": [ + 1, + 0, + 1 + ], + "listTwo": [ + 10, + 1 + ] + }, + "property": "sublist" + } +]