From fa86269bd5c1a37f0f28d528ecb278b7899b88f1 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 13 Oct 2020 21:14:59 -0400 Subject: Python code for Challenge 82 Task 2 --- challenge-082/walt-mankowski/python/ch-2.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-082/walt-mankowski/python/ch-2.py (limited to 'challenge-082/walt-mankowski/python') diff --git a/challenge-082/walt-mankowski/python/ch-2.py b/challenge-082/walt-mankowski/python/ch-2.py new file mode 100644 index 0000000000..662f0816d6 --- /dev/null +++ b/challenge-082/walt-mankowski/python/ch-2.py @@ -0,0 +1,23 @@ +from sys import argv + +def is_interleave(a, b, c): + if a == '' and b == '' and c == '': + return 1 + elif c == '': + return 0 + + found = False + if len(a) > 0 and a[0] == c[0]: + found = True + return is_interleave(a[1:], b, c[1:]) + + if len(b) > 0 and b[0] == c[0]: + found = True + return is_interleave(a, b[1:], c[1:]) + + if not found: + return 0 + +a, b, c = argv[1:] + +print(is_interleave(a, b, c)) -- cgit From 2d0c4c1678aa2caa7f0c42e90708ffa019dfc988 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 13 Oct 2020 21:18:20 -0400 Subject: replaced bounds checking with try clauses --- challenge-082/walt-mankowski/python/ch-2.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'challenge-082/walt-mankowski/python') diff --git a/challenge-082/walt-mankowski/python/ch-2.py b/challenge-082/walt-mankowski/python/ch-2.py index 662f0816d6..c9ef4de6f2 100644 --- a/challenge-082/walt-mankowski/python/ch-2.py +++ b/challenge-082/walt-mankowski/python/ch-2.py @@ -3,17 +3,21 @@ from sys import argv def is_interleave(a, b, c): if a == '' and b == '' and c == '': return 1 - elif c == '': - return 0 found = False - if len(a) > 0 and a[0] == c[0]: - found = True - return is_interleave(a[1:], b, c[1:]) + try: + if a[0] == c[0]: + found = True + return is_interleave(a[1:], b, c[1:]) + except IndexError: + pass - if len(b) > 0 and b[0] == c[0]: - found = True - return is_interleave(a, b[1:], c[1:]) + try: + if b[0] == c[0]: + found = True + return is_interleave(a, b[1:], c[1:]) + except IndexError: + pass if not found: return 0 -- cgit From e0cea5b6dcbc43a547d1401d70335c6ba74ec0c8 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 13 Oct 2020 21:18:31 -0400 Subject: Python code for Challenge 82 Task 1 --- challenge-082/walt-mankowski/python/ch-1.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 challenge-082/walt-mankowski/python/ch-1.py (limited to 'challenge-082/walt-mankowski/python') diff --git a/challenge-082/walt-mankowski/python/ch-1.py b/challenge-082/walt-mankowski/python/ch-1.py new file mode 100644 index 0000000000..d0f56b7fcb --- /dev/null +++ b/challenge-082/walt-mankowski/python/ch-1.py @@ -0,0 +1,13 @@ +from sys import argv +from math import sqrt + +def factors(n): + s = set([1]) + for i in range(2, int(sqrt(n))+1) : + if n % i == 0: + s.add(i) + s.add(n // i) + return s + +m, n = [int(x) for x in argv[1:]] +print(factors(m) & factors(n)) -- cgit From ba7a82cf5d113521a29b9f21a17c9fc9fbc5c922 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sat, 17 Oct 2020 18:27:06 -0400 Subject: realized I didn't need $found in task 2 --- challenge-082/walt-mankowski/python/ch-2.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'challenge-082/walt-mankowski/python') diff --git a/challenge-082/walt-mankowski/python/ch-2.py b/challenge-082/walt-mankowski/python/ch-2.py index c9ef4de6f2..ca2a2d551d 100644 --- a/challenge-082/walt-mankowski/python/ch-2.py +++ b/challenge-082/walt-mankowski/python/ch-2.py @@ -4,23 +4,19 @@ def is_interleave(a, b, c): if a == '' and b == '' and c == '': return 1 - found = False try: if a[0] == c[0]: - found = True return is_interleave(a[1:], b, c[1:]) except IndexError: pass try: if b[0] == c[0]: - found = True return is_interleave(a, b[1:], c[1:]) except IndexError: pass - if not found: - return 0 + return 0 a, b, c = argv[1:] -- cgit