diff options
| author | user-person <60802990+user-person@users.noreply.github.com> | 2020-03-25 14:18:55 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-25 14:18:55 -0400 |
| commit | 5dd949bd8a2d00b66f7e59dcd00efe69101b78c4 (patch) | |
| tree | 82ad9cd36625f5fa0cbbc83142d4b741e67df462 | |
| parent | 8f823189169c5955f86a3dff577580d0022e9758 (diff) | |
| download | perlweeklychallenge-club-5dd949bd8a2d00b66f7e59dcd00efe69101b78c4.tar.gz perlweeklychallenge-club-5dd949bd8a2d00b66f7e59dcd00efe69101b78c4.tar.bz2 perlweeklychallenge-club-5dd949bd8a2d00b66f7e59dcd00efe69101b78c4.zip | |
Create ch-1-CORRECTED.pl
| -rw-r--r-- | challenge-052/user-person/perl/ch-1-CORRECTED.pl | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/challenge-052/user-person/perl/ch-1-CORRECTED.pl b/challenge-052/user-person/perl/ch-1-CORRECTED.pl new file mode 100644 index 0000000000..0f5ebee9d0 --- /dev/null +++ b/challenge-052/user-person/perl/ch-1-CORRECTED.pl @@ -0,0 +1,86 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1-CORRECTED.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/ # +# # +# Stepping Numbers # +# # +# Write a script to accept two numbers between 100 and 999. It should # +# then print all Stepping Numbers between them. # +# # +# A number is called a stepping number if the adjacent digits have a # +# difference of 1. For example, 456 is a stepping number but 129 is not. # +# # +########################################################################### + +use strict; +use warnings; +use FindBin; + +my @step = (); +my $UPPER_LIMIT = 1000; +my $LOWER_LIMIT = 99; + +for (my $i = 1; $i < 10; ++$i) { + if ($i < 8) { # UP UP + push @step, ($i * 100) + ( $i + 1 ) * 10 + ($i + 2); + } + if ($i > 1) { # DOWN DOWN + push @step, ($i * 100) + ( $i - 1 ) * 10 + ($i - 2); + } + if ($i < 9) { # UP DOWN + push @step, ($i * 100) + ( $i + 1 ) * 10 + $i; + } + push @step, ($i * 100) + ( $i - 1 ) * 10 + $i; # DOWN UP +} + +@step = sort { $a <=> $b } @step; + +if ( + scalar @ARGV != 2 + + or $ARGV[0] !~ m|\A\d{3}\Z| + or $ARGV[0] < $LOWER_LIMIT + or $ARGV[0] > $UPPER_LIMIT + + or $ARGV[1] !~ m|\A\d{3}\Z| + or $ARGV[1] < $LOWER_LIMIT + or $ARGV[1] > $UPPER_LIMIT + + ) { + print STDERR "$FindBin::Script requires 2 arguments between 100 and 999.\n"; + exit(1); +} + +my ($min, $max) = (0, 0); + +if ($ARGV[0] < $ARGV[1]) { + $min = $ARGV[0]; + $max = $ARGV[1]; +} elsif ($ARGV[1] < $ARGV[0]) { + $min = $ARGV[1]; + $max = $ARGV[0]; +} else { + $max = $min = $ARGV[0]; +} + +my $commaFlag = 0; + +exit if $step[0] > $max or $step[-1] < $min; + +LOOP: +foreach (@step) { + if ($_ >= $min and $_ <= $max) { + print ", " if $commaFlag; + print "$_"; + $commaFlag = 1; + } elsif ($commaFlag) { + last LOOP; + } +} + +print "\n" if $commaFlag; |
