diff options
| author | brtastic <brtastic.dev@gmail.com> | 2020-07-16 22:03:48 +0200 |
|---|---|---|
| committer | brtastic <brtastic.dev@gmail.com> | 2020-07-16 22:03:48 +0200 |
| commit | be5e8c6dcc05940c46763f55e6ff9e1cf042c367 (patch) | |
| tree | d15c02cd40d68c3fcc1856f59c86aff42a8441f3 /challenge-069 | |
| parent | b47755559632b4aec5ad178123e3d6e32bcd9598 (diff) | |
| download | perlweeklychallenge-club-be5e8c6dcc05940c46763f55e6ff9e1cf042c367.tar.gz perlweeklychallenge-club-be5e8c6dcc05940c46763f55e6ff9e1cf042c367.tar.bz2 perlweeklychallenge-club-be5e8c6dcc05940c46763f55e6ff9e1cf042c367.zip | |
Solution to challenge #1
Diffstat (limited to 'challenge-069')
| -rw-r--r-- | challenge-069/brtastic/perl/ch-1.pl | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-069/brtastic/perl/ch-1.pl b/challenge-069/brtastic/perl/ch-1.pl new file mode 100644 index 0000000000..f892b6ad7c --- /dev/null +++ b/challenge-069/brtastic/perl/ch-1.pl @@ -0,0 +1,43 @@ +use v5.32; +use warnings; + +use feature qw(signatures); +no warnings qw(experimental::signatures); + +use constant MIRRORED => {qw( + 6 9 + 9 6 + 0 0 + 8 8 +)}; + +my $tr_search = join "", keys MIRRORED->%*; +my $tr_replace = join "", values MIRRORED->%*; + +sub is_strobogrammatic($number = $_) { + my sub flip($string) { + my $count; + eval "\$count = \$string =~ tr/$tr_search/$tr_replace/"; + return $count == length $string ? scalar reverse $string : undef; + } + + my $flipped = flip($number); + return !!0 unless defined $flipped; + return $number eq $flipped; +} + +sub find_strobogrammatic_in_range($from, $to) { + return [ + grep is_strobogrammatic, + grep { /^[$tr_search]+$/ } + $from .. $to + ]; +} + +use Test::More; + +is_deeply find_strobogrammatic_in_range(50, 100), [69, 88, 96]; +is_deeply find_strobogrammatic_in_range(0, 10), [0, 8]; +is_deeply find_strobogrammatic_in_range(100, 1000), [609, 689, 808, 888, 906, 986]; + +done_testing; |
