diff options
| author | Lance Wicks <lw@judocoach.com> | 2021-06-21 21:01:02 +0100 |
|---|---|---|
| committer | Lance Wicks <lw@judocoach.com> | 2021-06-21 21:01:02 +0100 |
| commit | ef7f425931c7632c6134eb10eff0768ed5db7bf4 (patch) | |
| tree | 68298d0706ba5dd596eeb2e83213fb94df5975fb | |
| parent | 1dab153d0833a3548dbedef4d757c8ed60841de4 (diff) | |
| download | perlweeklychallenge-club-ef7f425931c7632c6134eb10eff0768ed5db7bf4.tar.gz perlweeklychallenge-club-ef7f425931c7632c6134eb10eff0768ed5db7bf4.tar.bz2 perlweeklychallenge-club-ef7f425931c7632c6134eb10eff0768ed5db7bf4.zip | |
Naive verbose solution
| -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; |
