diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-24 17:36:23 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-24 17:36:23 +0100 |
| commit | b85a297a74e530757380adf05f8404c2d3b483a6 (patch) | |
| tree | e8d87bb12f2331e76f7ba7d14d74cdce0dadca64 /challenge-170/sgreen/python | |
| parent | a285527aa83f1ff9659cc3090b74aa832f4d79dd (diff) | |
| parent | 5d25c6e2b681143209bb64c08fb04a822e962133 (diff) | |
| download | perlweeklychallenge-club-b85a297a74e530757380adf05f8404c2d3b483a6.tar.gz perlweeklychallenge-club-b85a297a74e530757380adf05f8404c2d3b483a6.tar.bz2 perlweeklychallenge-club-b85a297a74e530757380adf05f8404c2d3b483a6.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-170/sgreen/python')
| -rwxr-xr-x | challenge-170/sgreen/python/ch-1.py | 36 | ||||
| -rwxr-xr-x | challenge-170/sgreen/python/ch-2.py | 41 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-170/sgreen/python/ch-1.py b/challenge-170/sgreen/python/ch-1.py new file mode 100755 index 0000000000..356393c2b4 --- /dev/null +++ b/challenge-170/sgreen/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import math + + +def is_prime(number): + '''Return true or false if the number is a prime''' + if number < 2: + return False + + for i in range(2, int(math.sqrt(number)) + 1): + if number % i == 0: + return False + + # It's a prime + return True + + +def main(): + solutions = [1] + value = 1 + counter = 0 + + # We need 10 solutions + while len(solutions) < 10: + counter += 1 + if is_prime(counter): + # Multiple the solution by this number + value *= counter + solutions.append(value) + + print(*solutions, sep=', ') + + +if __name__ == '__main__': + main() diff --git a/challenge-170/sgreen/python/ch-2.py b/challenge-170/sgreen/python/ch-2.py new file mode 100755 index 0000000000..fc0700a967 --- /dev/null +++ b/challenge-170/sgreen/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +import sys +import yaml + + +def check_array(a): + ''' Check that all rows in the array are the same length''' + for i in range(1, len(a)): + if (len(a[0]) != len(a[i])): + raise ValueError('mismatched array width') + + return a + + +def main(file_name): + # Read the supplied file in YAML format + with open(file_name, 'r') as file: + arrays = yaml.safe_load(file) + + if len(arrays) != 2: + raise ValueError('Expecting two arrays') + + array1 = check_array(arrays[0]) + array2 = check_array(arrays[1]) + + solution = [] + for row in array1: + for second_row in array2: + solution.append(list( str(c1 * c2) for c1 in row for c2 in second_row)) + + # Format each row uniformly. + max_length = max(max(len(col) for col in row) for row in solution) + fmt = '[' + (' {:>' + str(max_length) + 's}') * len(solution[0]) + ' ]' + + for row in solution: + print(fmt.format(*row)) + + +if __name__ == '__main__': + main(sys.argv[1]) |
