aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2020-07-13 17:12:25 -0400
committerDave Jacoby <jacoby.david@gmail.com>2020-07-13 17:12:25 -0400
commit516bfdc2cff19ed6f24fc963ae3a898421ff1aba (patch)
treeb7b517f01025e67e9946436878cb2b0bf06443d0
parent3abb2d4aa46c6d2627a8a4d7a4557f0d4835b2e3 (diff)
downloadperlweeklychallenge-club-516bfdc2cff19ed6f24fc963ae3a898421ff1aba.tar.gz
perlweeklychallenge-club-516bfdc2cff19ed6f24fc963ae3a898421ff1aba.tar.bz2
perlweeklychallenge-club-516bfdc2cff19ed6f24fc963ae3a898421ff1aba.zip
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;
+}