aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-07-13 22:23:45 +0100
committerGitHub <noreply@github.com>2020-07-13 22:23:45 +0100
commit48d20199448870ac1bb707bce41b734c9c79373d (patch)
tree04b623c95a3474daf47f4e82f24386a955113831
parent3276171ffb6706446a89886ed34d0ab13599c720 (diff)
parent516bfdc2cff19ed6f24fc963ae3a898421ff1aba (diff)
downloadperlweeklychallenge-club-48d20199448870ac1bb707bce41b734c9c79373d.tar.gz
perlweeklychallenge-club-48d20199448870ac1bb707bce41b734c9c79373d.tar.bz2
perlweeklychallenge-club-48d20199448870ac1bb707bce41b734c9c79373d.zip
Merge pull request #1943 from jacoby/master
Task #1
-rwxr-xr-xchallenge-069/dave-jacoby/perl/ch-1.pl44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-069/dave-jacoby/perl/ch-1.pl b/challenge-069/dave-jacoby/perl/ch-1.pl
new file mode 100755
index 0000000000..409824df77
--- /dev/null
+++ b/challenge-069/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+use List::Util qw{ min max };
+
+# we want positive integers, and will block anything but
+@ARGV = map { abs int $_ } @ARGV;
+
+croak 'Need more numbers' if scalar @ARGV < 2;
+
+my $min = int min @ARGV;
+my $max = int max @ARGV;
+
+# zero not allowed. googol not allowed.
+croak 'Min too small' if $min < 1;
+croak 'Max too large' if $max > 10**15;
+
+for my $n ( $min .. $max ) {
+ say $n if is_strobo($n);
+}
+
+exit;
+
+# there are digits that will not be part of a strobogrammatic
+# number, and we nope those away immeditately.
+# then we use the transliteration operator, tr, to swap sixes with
+# nines and vice versa, reverse the order of the numbers -- scratch
+# those operations, reverse them -- and compare with the original
+# number.
+
+# I thought this would be difficult, and was very prepared to not
+# turn this one in, until I remembered the magic that is tr///
+sub is_strobo ( $n ) {
+ return 0 if $n =~ /2|3|4|5|7/;
+ my $alt = join '' , reverse split //, $n;
+ $alt =~ tr/69/96/;
+ return 0 unless $n == $alt;
+ return 1;
+}