aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLance Wicks <lw@judocoach.com>2021-06-21 21:58:53 +0100
committerLance Wicks <lw@judocoach.com>2021-06-21 21:58:53 +0100
commit22ca24ae06f87871bda2643e9e838e392f0932da (patch)
tree85549af7b7a9943ed94bb401c308aa45f93e0454
parentef7f425931c7632c6134eb10eff0768ed5db7bf4 (diff)
downloadperlweeklychallenge-club-22ca24ae06f87871bda2643e9e838e392f0932da.tar.gz
perlweeklychallenge-club-22ca24ae06f87871bda2643e9e838e392f0932da.tar.bz2
perlweeklychallenge-club-22ca24ae06f87871bda2643e9e838e392f0932da.zip
With a script as per instructions, which has tests also
-rw-r--r--challenge-118/lance-wicks/perl/ch-1.pl20
-rw-r--r--challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm5
-rw-r--r--challenge-118/lance-wicks/perl/t/01-bin.t6
-rw-r--r--challenge-118/lance-wicks/perl/t/01-script.t14
4 files changed, 42 insertions, 3 deletions
diff --git a/challenge-118/lance-wicks/perl/ch-1.pl b/challenge-118/lance-wicks/perl/ch-1.pl
new file mode 100644
index 0000000000..5528159fb0
--- /dev/null
+++ b/challenge-118/lance-wicks/perl/ch-1.pl
@@ -0,0 +1,20 @@
+__PACKAGE__->run() unless caller;
+
+use lib './lib';
+use Binary::Palindrome;
+
+sub run {
+ my $n = $ARGV[0] || shift;
+
+ my $bp = Binary::Palindrome->new;
+ if ( my $res = $bp->is_palindrome($n) ) {
+ print "$res as binary representation of $n is ",
+ $bp->represent_as_binary($n), " which is Palindrome.";
+ }
+ else {
+ print "$res as binary representation of $n is ",
+ $bp->represent_as_binary($n), " which is NOT Palindrome.";
+ }
+}
+
+1;
diff --git a/challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm b/challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm
index f81bd238ff..1b752cf9e2 100644
--- a/challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm
+++ b/challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm
@@ -1,10 +1,9 @@
package Binary::Palindrome;
+use Moo;
+
sub is_palindrome {
my ( $self, $n ) = @_;
- # 1. get a binary representation of the integer
- # 2. reverse it
- # 3. is that the same ar step 1?
my $bin = $self->represent_as_binary($n);
my $rev = reverse $bin;
diff --git a/challenge-118/lance-wicks/perl/t/01-bin.t b/challenge-118/lance-wicks/perl/t/01-bin.t
index 7fe6374184..50113a946b 100644
--- a/challenge-118/lance-wicks/perl/t/01-bin.t
+++ b/challenge-118/lance-wicks/perl/t/01-bin.t
@@ -3,11 +3,17 @@ use Test2::V0 -target => 'Binary::Palindrome';
subtest 'Examples' => sub {
is $CLASS->is_palindrome(5), 1, '5 is a binary palindrome';
is $CLASS->is_palindrome(4), 0, '4 is NOT a binary palindrome';
+
+ is $CLASS->is_palindrome(99), 1, '99 is a binary palindrome';
+ is $CLASS->is_palindrome(98), 0, '98 is NOT a binary palindrome';
};
subtest 'Binary representation' => sub {
is $CLASS->represent_as_binary(5), '101', '5 --> 101';
is $CLASS->represent_as_binary(4), '100', '4 --> 100';
+
+ is $CLASS->represent_as_binary(99), '1100011', '99 --> 1100011';
+ is $CLASS->represent_as_binary(98), '1100010', '98 --> 1100010';
};
done_testing;
diff --git a/challenge-118/lance-wicks/perl/t/01-script.t b/challenge-118/lance-wicks/perl/t/01-script.t
new file mode 100644
index 0000000000..1cf4ac930b
--- /dev/null
+++ b/challenge-118/lance-wicks/perl/t/01-script.t
@@ -0,0 +1,14 @@
+use Test::More;
+use Test::Output;
+
+require_ok('./ch-1.pl');
+
+stdout_is { &run(5) }
+'1 as binary representation of 5 is 101 which is Palindrome.',
+ '5 is a palindrome';
+
+stdout_is { &run(4) }
+'0 as binary representation of 4 is 100 which is NOT Palindrome.',
+ '4 is NOT a palindrome';
+
+done_testing;