From 5f7b533e8cf34449a3a92a91dadc67856a883660 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Jun 2021 20:10:04 +0200 Subject: AWK, Bash, C, Lua, Node.js, Perl, Python, Ruby solutions for week 116, part 2. --- challenge-116/abigail/python/ch-2.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-116/abigail/python/ch-2.py (limited to 'challenge-116/abigail/python') diff --git a/challenge-116/abigail/python/ch-2.py b/challenge-116/abigail/python/ch-2.py new file mode 100644 index 0000000000..0876b16b64 --- /dev/null +++ b/challenge-116/abigail/python/ch-2.py @@ -0,0 +1,23 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput +from math import sqrt + +for line in fileinput . input (): + sum_of_squares = 0 + for char in line: + if "1" <= char and char <= "9": + sum_of_squares = sum_of_squares + int (char) * int (char) + root = int (.5 + sqrt (sum_of_squares)) + if sum_of_squares == root * root: + print (1) + else: + print (0) -- cgit From 3fe9fba9c9d2ec298020988b59ddf16dade7285f Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 9 Jun 2021 18:03:02 +0200 Subject: AWK, Bash, Lua, Node.js, Perl, Python, Ruby solutions for week 116, part 1 --- challenge-116/abigail/python/ch-1.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 challenge-116/abigail/python/ch-1.py (limited to 'challenge-116/abigail/python') diff --git a/challenge-116/abigail/python/ch-1.py b/challenge-116/abigail/python/ch-1.py new file mode 100644 index 0000000000..795d4ecabd --- /dev/null +++ b/challenge-116/abigail/python/ch-1.py @@ -0,0 +1,33 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +def make_chain (string, start): + if string == start: + return [start] + + if 0 == string . find (start): + tail = string [len (start) :] + result = make_chain (tail, str (int (start) + 1)) or \ + make_chain (tail, str (int (start) - 1)) + if result: + return [start] + result + + return None + + +for line in fileinput . input (): + line = line . strip () + for i in range (0, len (line)): + result = make_chain (line, line [0 : i + 1]) + if result: + print ("," . join (result)) + break -- cgit From 7e7b2cab61dcef029b71bd066bf6b5e3a71011af Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 13 Jun 2021 21:14:34 +0200 Subject: Rename make_chain to make_sequence --- challenge-116/abigail/python/ch-1.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'challenge-116/abigail/python') diff --git a/challenge-116/abigail/python/ch-1.py b/challenge-116/abigail/python/ch-1.py index 795d4ecabd..b988189564 100644 --- a/challenge-116/abigail/python/ch-1.py +++ b/challenge-116/abigail/python/ch-1.py @@ -10,14 +10,14 @@ import fileinput -def make_chain (string, start): +def make_sequence (string, start): if string == start: return [start] if 0 == string . find (start): tail = string [len (start) :] - result = make_chain (tail, str (int (start) + 1)) or \ - make_chain (tail, str (int (start) - 1)) + result = make_sequence (tail, str (int (start) + 1)) or \ + make_sequence (tail, str (int (start) - 1)) if result: return [start] + result @@ -27,7 +27,7 @@ def make_chain (string, start): for line in fileinput . input (): line = line . strip () for i in range (0, len (line)): - result = make_chain (line, line [0 : i + 1]) + result = make_sequence (line, line [0 : i + 1]) if result: print ("," . join (result)) break -- cgit