diff options
| -rw-r--r-- | challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm | 21 | ||||
| -rw-r--r-- | challenge-118/lance-wicks/perl/t/01-bin.t | 13 |
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm b/challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm new file mode 100644 index 0000000000..f81bd238ff --- /dev/null +++ b/challenge-118/lance-wicks/perl/lib/Binary/Palindrome.pm @@ -0,0 +1,21 @@ +package Binary::Palindrome; + +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; + + if ( $rev eq $bin ) { return 1 } + else { return 0 } +} + +sub represent_as_binary { + my ( $self, $n ) = @_; + return sprintf( "%b", $n ); +} + +1; diff --git a/challenge-118/lance-wicks/perl/t/01-bin.t b/challenge-118/lance-wicks/perl/t/01-bin.t new file mode 100644 index 0000000000..7fe6374184 --- /dev/null +++ b/challenge-118/lance-wicks/perl/t/01-bin.t @@ -0,0 +1,13 @@ +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'; +}; + +subtest 'Binary representation' => sub { + is $CLASS->represent_as_binary(5), '101', '5 --> 101'; + is $CLASS->represent_as_binary(4), '100', '4 --> 100'; +}; + +done_testing; |
