diff options
Diffstat (limited to 'challenge-053/user-person/python')
| -rwxr-xr-x | challenge-053/user-person/python/ch-1.py | 119 | ||||
| -rwxr-xr-x | challenge-053/user-person/python/ch-2.py | 81 |
2 files changed, 200 insertions, 0 deletions
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..29cdf05747 --- /dev/null +++ b/challenge-053/user-person/python/ch-1.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# Wed Mar 25 17:33:56 2020 | 1585172036 # +# # +# 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..73544afc1b --- /dev/null +++ b/challenge-053/user-person/python/ch-2.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# Thu Mar 26 00:42:17 2020 | 1585197737 # +# # +# 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) +# if num == 1: +# j = '{num:01d}'.format(num=j) +# elif num == 2: +# j = '{num:02d}'.format(num=j) +# elif num == 3: +# j = '{num:03d}'.format(num=j) +# elif num == 4: +# j = '{num:04d}'.format(num=j) +# elif num == 5: +# j = '{num:05d}'.format(num=j) + + 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) |
