aboutsummaryrefslogtreecommitdiff
path: root/challenge-053
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-29 20:42:51 +0100
committerGitHub <noreply@github.com>2020-03-29 20:42:51 +0100
commit50fd8d1500316e53d8d57dc1b52252f43043c5ca (patch)
tree2e3403114b1c5b87116b31048eb4de3e45749cc7 /challenge-053
parentd5cce6c0ec07f089ebbff89d7c82a7b24a8c3348 (diff)
parent57d5bc206530efe100646a8d60b0fe379235b569 (diff)
downloadperlweeklychallenge-club-50fd8d1500316e53d8d57dc1b52252f43043c5ca.tar.gz
perlweeklychallenge-club-50fd8d1500316e53d8d57dc1b52252f43043c5ca.tar.bz2
perlweeklychallenge-club-50fd8d1500316e53d8d57dc1b52252f43043c5ca.zip
Merge pull request #1484 from user-person/branch-for-challenge-053
Branch for challenge 053
Diffstat (limited to 'challenge-053')
-rwxr-xr-xchallenge-053/user-person/perl/ch-1.pl142
-rwxr-xr-xchallenge-053/user-person/perl/ch-2.pl65
-rwxr-xr-xchallenge-053/user-person/python/ch-1.py118
-rwxr-xr-xchallenge-053/user-person/python/ch-2.py70
4 files changed, 395 insertions, 0 deletions
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)