From 71d3279fed60471b894f85006f0573eba5e0e66f Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 19 Oct 2021 16:40:57 +0200 Subject: Python solutions for week 135 --- challenge-135/abigail/README.md | 2 ++ challenge-135/abigail/python/ch-1.py | 24 ++++++++++++++++++++++++ challenge-135/abigail/python/ch-2.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 challenge-135/abigail/python/ch-1.py create mode 100644 challenge-135/abigail/python/ch-2.py diff --git a/challenge-135/abigail/README.md b/challenge-135/abigail/README.md index 9f6cb7b1ff..a69904f5c4 100644 --- a/challenge-135/abigail/README.md +++ b/challenge-135/abigail/README.md @@ -8,6 +8,7 @@ * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) ## Part 2 @@ -17,3 +18,4 @@ * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) diff --git a/challenge-135/abigail/python/ch-1.py b/challenge-135/abigail/python/ch-1.py new file mode 100644 index 0000000000..451fa94c9f --- /dev/null +++ b/challenge-135/abigail/python/ch-1.py @@ -0,0 +1,24 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput +import re + +for line in fileinput . input (): + line = re . sub (r'^[-+]', '', line . strip ()) + ll = len (line) + if re . search (r'[^0-9]', line): + print ("not an integer") + elif ll % 2 == 0: + print ("even number of digits") + elif ll < 3: + print ("too short") + else: + print (line [(ll - 3) // 2 : (ll + 3) // 2]) diff --git a/challenge-135/abigail/python/ch-2.py b/challenge-135/abigail/python/ch-2.py new file mode 100644 index 0000000000..93ba2921e2 --- /dev/null +++ b/challenge-135/abigail/python/ch-2.py @@ -0,0 +1,32 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput +import re + +w = [1, 3, 1, 7, 3, 9, 1] + +for line in fileinput . input (): + line = line . strip () + if re . search (r'^[0-9BCDFGHJKLMNPQRSTVWXYZ]{7}$', line): + check = 0 + for i in range (len (line)): + val = ord (line [i : i + 1]) + if val <= ord ("9"): + val = val - ord ("0") + else: + val = val - ord ("A") + 10 + check = check + w [i] * val + if check % 10 == 0: + print (1) + else: + print (0) + else: + print (0) -- cgit