exercism-perl5

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

commit f5b3878cfb3ba3674bdbace7cf8364c2ae2d4a7a
parent 31bcba98257f27e4d2e4fdc5680ccf79e35631c4
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Sun, 23 Jan 2022 15:08:38 +0000

initial commit for palindrome-products

Diffstat:
Apalindrome-products/HELP.md | 36++++++++++++++++++++++++++++++++++++
Apalindrome-products/Palindrome.pm | 46++++++++++++++++++++++++++++++++++++++++++++++
Apalindrome-products/README.md | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apalindrome-products/palindrome.t | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apalindrome-products/ptest.pl | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apalindrome-products/ptest2.pl | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 382 insertions(+), 0 deletions(-)

diff --git a/palindrome-products/HELP.md b/palindrome-products/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 Palindrome.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/palindrome-products/Palindrome.pm b/palindrome-products/Palindrome.pm @@ -0,0 +1,46 @@ +package Palindrome; + +use strict; +use warnings; +use Math::Prime::Util 'factor'; + +sub new { + my ( $class, $min_max ) = @_; + my $self = bless ( {}, $class ); + $self->_init( $min_max ); + #print "min_factor is ", $self->{min_factor}, " and max_factor is ", $self->{max_factor}, "\n"; + return $self; +} + +sub _init { + my ( $self, $arg ) = @_; +# my $min_factor = $arg->{min_factor}; +# unless (defined $min_factor ) { +# $self->{min_factor} = 1; +# } + $self->{min_factor} = defined $arg->{min_factor} ? + $arg->{min_factor} : + 1; + $self->{max_factor} = $arg->{max_factor}; +} + +sub largest { + my $self = shift; + print "my min factor is ", $self->{min_factor}, "\n"; + print "my max factor is ", $self->{max_factor}, "\n"; + #my @factors = factor(9); + #print "my factors are @factors\n"; + my %answer = ( + value => 9, + factors => [ [1, 9], [3, 3] ], + ); + return \%answer; +} + +sub smallest { + my $self = shift; + + return undef; +} + +1; diff --git a/palindrome-products/README.md b/palindrome-products/README.md @@ -0,0 +1,54 @@ +# Palindrome Products + +Welcome to Palindrome Products on Exercism's Perl 5 Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Detect palindrome products in a given range. + +A palindromic number is a number that remains the same when its digits are +reversed. For example, `121` is a palindromic number but `112` is not. + +Given a range of numbers, find the largest and smallest palindromes which +are products of two numbers within that range. + +Your solution should return the largest and smallest palindromes, along with the +factors of each within the range. If the largest or smallest palindrome has more +than one pair of factors within the range, then return all the pairs. + +## Example 1 + +Given the range `[1, 9]` (both inclusive)... + +And given the list of all possible products within this range: +`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]` + +The palindrome products are all single digit numbers (in this case): +`[1, 2, 3, 4, 5, 6, 7, 8, 9]` + +The smallest palindrome product is `1`. Its factors are `(1, 1)`. +The largest palindrome product is `9`. Its factors are `(1, 9)` and `(3, 3)`. + +## Example 2 + +Given the range `[10, 99]` (both inclusive)... + +The smallest palindrome product is `121`. Its factors are `(11, 11)`. +The largest palindrome product is `9009`. Its factors are `(91, 99)`. + +## Source + +### Created by + +- @bistik + +### Contributed to by + +- @kytrinyx +- @m-dango +- @rfilipo + +### Based on + +Problem 4 at Project Euler - http://projecteuler.net/problem=4 +\ No newline at end of file diff --git a/palindrome-products/palindrome.t b/palindrome-products/palindrome.t @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Test2::Bundle::More; +use FindBin qw($Bin); +use lib $Bin, "$Bin/local/lib/perl5"; + +my $module = 'Palindrome'; + +plan 15; + +ok -e "$Bin/$module.pm", "Missing $module.pm" + or BAIL_OUT "You need to create file: $module.pm"; + +eval "use $module"; +ok !$@, "Cannot load $module" + or BAIL_OUT + "Cannot load $module. Does it compile? Does it end with 1;?"; + +can_ok $module, "new" + or BAIL_OUT "Missing package $module or missing sub new()"; +can_ok $module, "largest" + or BAIL_OUT "Missing package $module or missing sub largest()"; +can_ok $module, "smallest" + or BAIL_OUT "Missing package $module or missing sub smallest()"; + +my $palindrome; +my ( $largest, $smallest ); + +$palindrome = $module->new( { max_factor => 9 } ); +$largest = $palindrome->largest; +is $largest->{value}, 9, + "largest palindrome value from single digit factors"; +is_deeply( + $largest->{factors}, + [ [ 1, 9 ], [ 3, 3 ] ], + "largest single digit palindrome factors" +) or diag explain $largest->{factors}; + +$palindrome = $module->new( { max_factor => 99, min_factor => 10 } ); +$largest = $palindrome->largest; +is $largest->{value}, 9009, + "largest palindrome value from double digit factors"; +is_deeply( + [ sort @{ $largest->{factors} } ], + [ [ 91, 99 ] ], + "largest double digit palindrome factors" +) or diag explain $largest->{factors}; + +$palindrome + = $module->new( { max_factor => 999, min_factor => 100 } ); +$largest = $palindrome->largest; +is $largest->{value}, 906609, + "largest palindrome value from triple digit factors"; +is_deeply( + $largest->{factors}, + [ [ 913, 993 ] ], + "largest triple digit palindrome factors" +) or diag explain $largest->{factors}; + +$palindrome = $module->new( { max_factor => 99, min_factor => 10 } ); +$smallest = $palindrome->smallest; +is $smallest->{value}, 121, + "smallest palindrome value from double digit factors"; +is_deeply( + $smallest->{factors}, + [ [ 11, 11 ] ], + "smallest double digit palindrome factors" +) or diag explain $smallest->{factors}; + +$palindrome + = $module->new( { max_factor => 999, min_factor => 100 } ); +$smallest = $palindrome->smallest; +is $smallest->{value}, 10201, + "smallest palindrome value from triple digit factors"; +is_deeply( + $smallest->{factors}, + [ [ 101, 101 ] ], + "smallest triple digit palindrome factors" +) or diag explain $smallest->{factors}; diff --git a/palindrome-products/ptest.pl b/palindrome-products/ptest.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Test2::Bundle::More; +use FindBin qw($Bin); +use lib $Bin, "$Bin/local/lib/perl5"; + +my $module = 'Palindrome'; +use Palindrome; + +#plan 15; +# +#ok -e "$Bin/$module.pm", "Missing $module.pm" +# or BAIL_OUT "You need to create file: $module.pm"; +# +#eval "use $module"; +#ok !$@, "Cannot load $module" +# or BAIL_OUT +# "Cannot load $module. Does it compile? Does it end with 1;?"; +# +#can_ok $module, "new" +# or BAIL_OUT "Missing package $module or missing sub new()"; +#can_ok $module, "largest" +# or BAIL_OUT "Missing package $module or missing sub largest()"; +#can_ok $module, "smallest" +# or BAIL_OUT "Missing package $module or missing sub smallest()"; + +my $palindrome; +my ( $largest, $smallest ); + +#$palindrome = $module->new( { max_factor => 9 } ); +#$largest = $palindrome->largest; +#is $largest->{value}, 9, +# "largest palindrome value from single digit factors"; +#is_deeply( +# $largest->{factors}, +# [ [ 1, 9 ], [ 3, 3 ] ], +# "largest single digit palindrome factors" +#) or diag explain $largest->{factors}; + +$palindrome = $module->new( { max_factor => 9, min_factor => 1 } ); +$largest = $palindrome->largest; +print "largest palindrome product, which should be 9, is '$largest->{value}'\n"; +#is $largest->{value}, 9009, +# "largest palindrome value from double digit factors"; +#is_deeply( +# [ sort @{ $largest->{factors} } ], +# [ [ 91, 99 ] ], +# "largest double digit palindrome factors" +#) or diag explain $largest->{factors}; + +#$palindrome +# = $module->new( { max_factor => 999, min_factor => 100 } ); +#$largest = $palindrome->largest; +#is $largest->{value}, 906609, +# "largest palindrome value from triple digit factors"; +#is_deeply( +# $largest->{factors}, +# [ [ 913, 993 ] ], +# "largest triple digit palindrome factors" +#) or diag explain $largest->{factors}; + +$palindrome = $module->new( { max_factor => 99, min_factor => 10 } ); +$smallest = $palindrome->smallest; +is $smallest->{value}, 121, + "smallest palindrome value from double digit factors"; +is_deeply( + $smallest->{factors}, + [ [ 11, 11 ] ], + "smallest double digit palindrome factors" +) or diag explain $smallest->{factors}; + +#$palindrome +# = $module->new( { max_factor => 999, min_factor => 100 } ); +#$smallest = $palindrome->smallest; +#is $smallest->{value}, 10201, +# "smallest palindrome value from triple digit factors"; +#is_deeply( +# $smallest->{factors}, +# [ [ 101, 101 ] ], +# "smallest triple digit palindrome factors" +#) or diag explain $smallest->{factors}; diff --git a/palindrome-products/ptest2.pl b/palindrome-products/ptest2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Test2::Bundle::More; +use FindBin qw($Bin); +use lib $Bin, "$Bin/local/lib/perl5"; + +my $module = 'Palindrome'; + +plan 9; + +ok -e "$Bin/$module.pm", "Missing $module.pm" + or BAIL_OUT "You need to create file: $module.pm"; + +eval "use $module"; +ok !$@, "Cannot load $module" + or BAIL_OUT + "Cannot load $module. Does it compile? Does it end with 1;?"; + +can_ok $module, "new" + or BAIL_OUT "Missing package $module or missing sub new()"; +can_ok $module, "largest" + or BAIL_OUT "Missing package $module or missing sub largest()"; +can_ok $module, "smallest" + or BAIL_OUT "Missing package $module or missing sub smallest()"; + +my $palindrome; +my ( $largest, $smallest ); + +$palindrome = $module->new( { max_factor => 9 } ); +$largest = $palindrome->largest; +is $largest->{value}, 9, + "largest palindrome value from single digit factors"; +is_deeply( + $largest->{factors}, + [ [ 1, 9 ], [ 3, 3 ] ], + "largest single digit palindrome factors" +) or diag explain $largest->{factors}; + +$palindrome = $module->new( { max_factor => 99, min_factor => 10 } ); +$largest = $palindrome->largest; +is $largest->{value}, 9009, + "largest palindrome value from double digit factors"; +is_deeply( + [ sort @{ $largest->{factors} } ], + [ [ 91, 99 ] ], + "largest double digit palindrome factors" +) or diag explain $largest->{factors}; + +#$palindrome +# = $module->new( { max_factor => 999, min_factor => 100 } ); +#$largest = $palindrome->largest; +#is $largest->{value}, 906609, +# "largest palindrome value from triple digit factors"; +#is_deeply( +# $largest->{factors}, +# [ [ 913, 993 ] ], +# "largest triple digit palindrome factors" +#) or diag explain $largest->{factors}; +# +#$palindrome = $module->new( { max_factor => 99, min_factor => 10 } ); +#$smallest = $palindrome->smallest; +#is $smallest->{value}, 121, +# "smallest palindrome value from double digit factors"; +#is_deeply( +# $smallest->{factors}, +# [ [ 11, 11 ] ], +# "smallest double digit palindrome factors" +#) or diag explain $smallest->{factors}; +# +#$palindrome +# = $module->new( { max_factor => 999, min_factor => 100 } ); +#$smallest = $palindrome->smallest; +#is $smallest->{value}, 10201, +# "smallest palindrome value from triple digit factors"; +#is_deeply( +# $smallest->{factors}, +# [ [ 101, 101 ] ], +# "smallest triple digit palindrome factors" +#) or diag explain $smallest->{factors};