aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-11 00:32:12 +0200
committerAbigail <abigail@abigail.be>2021-05-11 00:32:12 +0200
commitbfb3640986af5c780b19d4165f36892336cdbcc9 (patch)
tree756ea1b3816b0417f373693b8772530eaaf0633c
parent2292cff0b3bd0bc5e2dadbecee8a4f50cb9fb840 (diff)
downloadperlweeklychallenge-club-bfb3640986af5c780b19d4165f36892336cdbcc9.tar.gz
perlweeklychallenge-club-bfb3640986af5c780b19d4165f36892336cdbcc9.tar.bz2
perlweeklychallenge-club-bfb3640986af5c780b19d4165f36892336cdbcc9.zip
Python solutions for week 112
-rw-r--r--challenge-112/abigail/README.md2
-rw-r--r--challenge-112/abigail/python/ch-1.py24
-rw-r--r--challenge-112/abigail/python/ch-2.py21
3 files changed, 47 insertions, 0 deletions
diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md
index f3d497b249..4b619db213 100644
--- a/challenge-112/abigail/README.md
+++ b/challenge-112/abigail/README.md
@@ -39,6 +39,7 @@ Output: "/a"
* [Lua](lua/ch-1.lua)
* [Node.js](node/ch-1.js)
* [Perl](perl/ch-1.pl)
+* [Python](python/ch-1.py)
### Blog
@@ -60,5 +61,6 @@ This is just finding the `$n + 1` Fibonacci number.
* [Lua](lua/ch-2.lua)
* [Node.js](node/ch-1.js)
* [Perl](perl/ch-2.pl)
+* [Python](python/ch-2.py)
### Blog
diff --git a/challenge-112/abigail/python/ch-1.py b/challenge-112/abigail/python/ch-1.py
new file mode 100644
index 0000000000..bf52e6d83c
--- /dev/null
+++ b/challenge-112/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
+
+for line in fileinput . input ():
+ parts = line . rstrip () . split ("/") # Split input on /
+ parts2 = []
+ for part in parts:
+ if part == "" or part == ".": # Skip empty parts,
+ continue # and current directory.
+ if part == "..": # Pop parent directory
+ if len (parts2):
+ parts2 . pop ()
+ continue
+ parts2 . append (part) # Else, append.
+ print ("/" + "/" . join (parts2)) # Print result.
diff --git a/challenge-112/abigail/python/ch-2.py b/challenge-112/abigail/python/ch-2.py
new file mode 100644
index 0000000000..3e2a55750d
--- /dev/null
+++ b/challenge-112/abigail/python/ch-2.py
@@ -0,0 +1,21 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as: python ch-1.py < input-file
+#
+
+import fileinput
+
+cache = {0: 1, 1: 1}
+
+def fib (n):
+ if not n in cache:
+ cache [n] = fib (n - 1) + fib (n - 2)
+ return (cache [n])
+
+for line in fileinput . input ():
+ print (fib (int (line)))