diff options
| -rwxr-xr-x | challenge-052/user-person/perl/ch-1-CORRECTED.pl | 86 | ||||
| -rwxr-xr-x | challenge-052/user-person/python/ch-1-CORRECTED.py | 81 | ||||
| -rwxr-xr-x | challenge-053/user-person/perl/ch-1.pl | 142 | ||||
| -rwxr-xr-x | challenge-053/user-person/perl/ch-2.pl | 65 | ||||
| -rwxr-xr-x | challenge-053/user-person/python/ch-1.py | 118 | ||||
| -rwxr-xr-x | challenge-053/user-person/python/ch-2.py | 70 |
6 files changed, 562 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 100755 index 0000000000..9817a5e7ca --- /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 && $i < 10) { # 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 100755 index 0000000000..2cb2de3814 --- /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() diff --git a/challenge-053/user-person/perl/ch-1.pl b/challenge-053/user-person/perl/ch-1.pl new file mode 100755 index 0000000000..2ffc8964e4 --- /dev/null +++ b/challenge-053/user-person/perl/ch-1.pl @@ -0,0 +1,142 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-053/ # +# # +# Rotate Matrix # +# Write a script to rotate the followin matrix by given 90/180/270 # +# degrees clockwise. # +# # +# [ 1, 2, 3 ] # +# [ 4, 5, 6 ] # +# [ 7, 8, 9 ] # +# # +# For example, if you rotate by 90 degrees then expected result should # +# be like below # +# # +# [ 7, 4, 1 ] # +# [ 8, 5, 2 ] # +# [ 9, 6, 3 ] # +# # +########################################################################### + +use strict; +use warnings; +use diagnostics; + +use FindBin; + +sub flat { + return map { ref eq 'ARRAY' ? flat(@$_) : $_ } @_; +} + +my @matrix = [[1,2,3], + [4,5,6], + [7,8,9]]; + +#______________________________________ +# 0 start | 90 | 180 | 270 | +# 1 [0][0] | [0][2] | [2][2] | [2][0] | +# 2 [0][1] | [1][2] | [2][1] | [1][0] | +# 3 [0][2] | [2][2] | [2][0] | [0][0] | +# 4 [1][0] | [0][1] | [1][2] | [2][1] | +# 5 [1][1] | [1][1] | [1][1] | [1][1] | +# 6 [1][2] | [2][1] | [1][0] | [0][1] | +# 7 [2][0] | [0][0] | [0][2] | [2][2] | +# 8 [2][1] | [1][0] | [0][1] | [1][2] | +# 9 [2][2] | [2][0] | [0][0] | [0][2] | +#__________|________|________|________| + +# inner outer +# 0 row 0 + col 0 + +# 90 col 2 - row 0 + +# 180 row 2 - col 2 - +# 270 col 2 - row 0 + + +my $width = 0; +my @rawNums = (); + +@rawNums = flat @matrix; + +for (my $w=1; $w**2 <= scalar @rawNums; ++$w) { + if ($w**2 == scalar @rawNums) { + $width = $w; + } +} + +my $deg; +my $errorString = "Invalid choice. There are modes for 0, 90, 180, 270 degree rotations.\n"; + +if (@ARGV) { + $deg = $ARGV[0]; +} else { + print STDERR $FindBin::Script, " requires a command-line argument. There are modes for 0, 90, 180, and 270 degree rotations.\n"; + exit(1); +} + +if ($deg !~ m{\A[012789]+\Z}) { + print STDERR $errorString; + exit(1); +} + +if ( not ($deg == 0 or $deg == 90 or $deg == 180 or $deg == 270)) { + print STDERR $errorString; + exit(1); +} + +my @newMatrix = (); +my $i = 0; + +if ($deg == 0) { + + for (my $j = 0; $j < $width; ++$j) { + for (my $k = 0; $k < $width; ++$k) { + $newMatrix[$j][$k] = $rawNums[$i++]; + } + } + +} elsif ( $deg == 90 ) { + + for (my $j = $width-1; $j >= 0; --$j) { + for (my $k = 0; $k < $width; ++$k) { + $newMatrix[$k][$j] = $rawNums[$i++]; + } + } + +} elsif ( $deg == 180 ) { + + for (my $j = $width-1; $j >= 0; --$j) { + for (my $k = $width-1; $k >= 0; --$k) { + $newMatrix[$j][$k] = $rawNums[$i++]; + } + } + +} elsif ( $deg == 270 ) { + + for (my $j = 0; $j < $width; ++$j) { + for (my $k = $width-1; $k >= 0; --$k) { + $newMatrix[$k][$j] = $rawNums[$i++]; + } + } + +} + +foreach my $row (@newMatrix) { + my $format = 0; + foreach my $element (@$row) { + if ($format == 0) { + print "[", $element, ","; + } elsif ($format == 1) { + print " ", $element, ","; + } elsif ($format == 2) { + print " ", $element, "]"; + } + ++$format; + } + + print "\n"; +} diff --git a/challenge-053/user-person/perl/ch-2.pl b/challenge-053/user-person/perl/ch-2.pl new file mode 100755 index 0000000000..ddc744aec8 --- /dev/null +++ b/challenge-053/user-person/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-053/ # +# # +# Vowel Strings # +# # +# Write a script to accept an integer 1 <= N <= 5 that would print all # +# possible strings of size N formed by using only vowels (a, e, i, o, u). # +# The string should follow the following rules: # +# # +# 1. 'a' can only be followed by 'e' and 'i'. # +# 2. 'e' can only be followed by 'i'. # +# 3. 'i' can only be followed by 'a', 'e', 'o', and 'u'. # +# 4. 'o' can only be followed by 'a' and 'u'. # +# 5. 'u' can only be followed by 'o' and 'e'. # +# # +########################################################################### + +use strict; +use warnings; +use diagnostics; + +use FindBin; +use Math::Int2Base qw( int2base base2int ); + +my $num = 0; + +if (! @ARGV) { + print STDERR $FindBin::Script, " requires an argument. Argument should be 1 <= n <= 5\n"; + exit(1); +} else { + $num = $ARGV[0]; +} + +if ($num < 1 or $num > 5) { + print STDERR "Invalid choice. Argument should be 1 <= n <= 5\n"; + exit(1); +} + +my @vowels = qw{ a e i o u }; +my $MAX = '4' x $num; + +my $j = 0; +for (my $i = 0; $j <= $MAX; ++$i) { + $j = int2base($i,5); + $j = sprintf "%0${num}d", $j; + my $string; + foreach (split(//,$j)){ + $string .= $vowels[$_]; + } + if ( $string =~ m{a[^ei]} + or $string =~ m{e[^i]} + or $string =~ m{i[^aeou]} + or $string =~ m{o[^au]} + or $string =~ m{u[^oe]} ){ + next; + } else { + print "$string\n"; + } +} diff --git a/challenge-053/user-person/python/ch-1.py b/challenge-053/user-person/python/ch-1.py new file mode 100755 index 0000000000..687bf81cab --- /dev/null +++ b/challenge-053/user-person/python/ch-1.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-053/ # +# # +# Rotate Matrix # +# Write a script to rotate the followin matrix by given 90/180/270 # +# degrees clockwise. # +# # +# [ 1, 2, 3 ] # +# [ 4, 5, 6 ] # +# [ 7, 8, 9 ] # +# # +# For example, if you rotate by 90 degrees then expected result should # +# be like below # +# # +# [ 7, 4, 1 ] # +# [ 8, 5, 2 ] # +# [ 9, 6, 3 ] # +# # +########################################################################### + +from itertools import chain +import os +import re +import sys + +matrix = [[1,2,3], + [4,5,6], + [7,8,9]]; + +#______________________________________ +# 0 start | 90 | 180 | 270 | +# 1 [0][0] | [0][2] | [2][2] | [2][0] | +# 2 [0][1] | [1][2] | [2][1] | [1][0] | +# 3 [0][2] | [2][2] | [2][0] | [0][0] | +# 4 [1][0] | [0][1] | [1][2] | [2][1] | +# 5 [1][1] | [1][1] | [1][1] | [1][1] | +# 6 [1][2] | [2][1] | [1][0] | [0][1] | +# 7 [2][0] | [0][0] | [0][2] | [2][2] | +# 8 [2][1] | [1][0] | [0][1] | [1][2] | +# 9 [2][2] | [2][0] | [0][0] | [0][2] | +#__________|________|________|________| + +# inner outer +# 0 row 0 + col 0 + +# 90 col 2 - row 0 + +# 180 row 2 - col 2 - +# 270 col 2 - row 0 + + +width = 0 +rawNums = [] + +rawNums = list(chain.from_iterable(matrix)) + +w = 1 + +while w**2 <= len(rawNums): + if w**2 == len(rawNums): + width = w + loop = False + w += 1 + +#scriptName = os.path.basename(sys.argv[0]) +errorString = os.path.basename(sys.argv[0]) + ' requires a command-line argument. There are modes for 0, 90, 180, and 270 degree rotations.\n' + +if not len(sys.argv[1:]): + sys.stderr.write(errorString) + exit(1) + +deg = sys.argv[1] + +if not re.search(r'\A[012789]+\Z',deg): + sys.stderr.write(errorString) + exit(1) + +deg = int(deg) + +if not (deg == 0 or deg == 90 or deg == 180 or deg == 270): + sys.stderr.write('Invalid choice. There are modes for 0, 90, 180, and 270 degrees.\n') + exit(1) + +newMatrix = arr = [[0 for i in range(width)] for j in range(width)] +i = 0 + +if deg == 0: + for j in range(width): + for k in range(width): + # print('j: ', j,'k: ', k,'i: ', i,'rawNums[i]: ', rawNums[i]) + newMatrix[j][k] = rawNums[i] + i += 1 + +elif deg == 90: + for j in range(width-1, -1, -1): + for k in range(width): + newMatrix[k][j] = rawNums[i] + i += 1 + +elif deg == 180: + for j in range(width-1, -1, -1): + for k in range(width-1, -1, -1): + newMatrix[j][k] = rawNums[i] + i += 1 + +elif deg == 270: + for j in range(width): + for k in range(width-1, -1, -1): + newMatrix[k][j] = rawNums[i] + i += 1 + +[print(whoa) for whoa in newMatrix] + + + diff --git a/challenge-053/user-person/python/ch-2.py b/challenge-053/user-person/python/ch-2.py new file mode 100755 index 0000000000..ff5262ffbf --- /dev/null +++ b/challenge-053/user-person/python/ch-2.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-053/ # +# # +# Vowel Strings # +# # +# Write a script to accept an integer 1 <= N <= 5 that would print all # +# possible strings of size N formed by using only vowels (a, e, i, o, u). # +# The string should follow the following rules: # +# # +# 1. 'a' can only be followed by 'e' and 'i'. # +# 2. 'e' can only be followed by 'i'. # +# 3. 'i' can only be followed by 'a', 'e', 'o', and 'u'. # +# 4. 'o' can only be followed by 'a' and 'u'. # +# 5. 'u' can only be followed by 'o' and 'e'. # +# # +########################################################################### + +import os +import re +import sys + +# https://is.gd/lOmciv +def toStr(n,base): + convertString = "01234" + if n < base: + return convertString[n] + else: + return toStr(n//base,base) + convertString[n%base] + +scriptName = os.path.basename(sys.argv[0]) + +num = 0 + +try: + num = int(sys.argv[1]) +except IndexError: + sys.stderr.write(scriptName + ' requires an argument. Argument should be 1 <= n <= 5\n') + exit(1) + +if num < 1 or num > 5: + sys.stderr.write('Invalid choice. Argument should be 1 <= n <= 5\n') + exit(1) + +vowels = ( 'a', 'e', 'i', 'o', 'u') +MAX = int('4' * num,5) + +j = 0 +for i in range(0,MAX): + j = int(toStr(i,5)) + + j = '{number:0{size}d}'.format(number=j,size=num) + + string = '' + for k in str(j): + string += vowels[int(k)] + + if re.search(r'a[^ei]',string) \ + or re.search(r'e[^i]',string) \ + or re.search(r'o[^au]',string) \ + or re.search(r'i[^aeou]',string) \ + or re.search(r'u[^oe]',string): + continue + else: + print(string) |
