aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-25 18:27:34 +0000
committerGitHub <noreply@github.com>2020-03-25 18:27:34 +0000
commitd4b7675ea2e5d93cd5cd8a4a2fd3fa2e8c4b5899 (patch)
tree80b0b6006bf0422f2486e690f932cb27431c43e4
parent76149df6dc425f24fecdb8679bed53fedc00c29b (diff)
parentd5fa188fc2daf2f251673046c1616945ef9c3cb5 (diff)
downloadperlweeklychallenge-club-d4b7675ea2e5d93cd5cd8a4a2fd3fa2e8c4b5899.tar.gz
perlweeklychallenge-club-d4b7675ea2e5d93cd5cd8a4a2fd3fa2e8c4b5899.tar.bz2
perlweeklychallenge-club-d4b7675ea2e5d93cd5cd8a4a2fd3fa2e8c4b5899.zip
Merge pull request #1467 from user-person/branch-for-challenge-052
Branch for challenge 052
-rw-r--r--challenge-052/user-person/perl/ch-1-CORRECTED.pl86
-rw-r--r--challenge-052/user-person/python/ch-1-CORRECTED.py81
2 files changed, 167 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;
diff --git a/challenge-052/user-person/python/ch-1-CORRECTED.py b/challenge-052/user-person/python/ch-1-CORRECTED.py
new file mode 100644
index 0000000000..0fa2fa5bdd
--- /dev/null
+++ b/challenge-052/user-person/python/ch-1-CORRECTED.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+
+###########################################################################
+# script name: ch-1-CORRECTED.py #
+# #
+# 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. #
+# #
+###########################################################################
+
+import os
+import re
+import sys
+
+step = []
+UPPER_LIMIT = 1000
+LOWER_LIMIT = 99
+
+for i in range(1,10):
+ if i < 8: # UP UP
+ step.append( (i * 100) + ((i+1) * 10) + (i+2) )
+
+ if i > 1: # DOWN DOWN
+ step.append( (i * 100) + ((i-1) * 10) + (i-2) )
+
+ if i < 9: # UP DOWN
+ step.append( (i * 100) + ((i+1) * 10) + i )
+
+ step.append( (i * 100) + ((i-1) * 10) + i ) # DOWN UP
+
+step.sort()
+
+errorString = os.path.basename(sys.argv[0]) + ' requires 2 arguments between 100 and 999.'
+
+if len(sys.argv) != 3:
+ print(errorString)
+ exit(1)
+
+args = [int(arg) for arg in sys.argv[1:]]
+
+for arg in args:
+ if arg < LOWER_LIMIT or arg > UPPER_LIMIT:
+ print(errorString)
+ exit(1)
+
+max = min = 0
+
+if args[0] < args[1]:
+ min = args[0]
+ max = args[1]
+elif args[1] < args[0]:
+ min = args[1]
+ max = args[0]
+else:
+ max = min = args[0]
+
+commaFlag = False
+
+if step[0] > max or step[-1] < min:
+ exit(0)
+
+for num in step:
+ if num >= min and num <= max:
+ if commaFlag:
+ print(', ', end='')
+ print(num, end='')
+ commaFlag = True
+ elif commaFlag:
+ break
+
+if commaFlag:
+ print()