aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-04 14:16:04 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-04 14:16:04 +0000
commit11599a322483a3301a1cae405883a852d2ae98f2 (patch)
tree075fdd4342b87de18c9e31813317b93c9e74b0c8 /challenge-112
parent6619f2571264d4c6c73fadbdc972e76dd5836bdb (diff)
downloadperlweeklychallenge-club-11599a322483a3301a1cae405883a852d2ae98f2.tar.gz
perlweeklychallenge-club-11599a322483a3301a1cae405883a852d2ae98f2.tar.bz2
perlweeklychallenge-club-11599a322483a3301a1cae405883a852d2ae98f2.zip
Add Python solution to challenge 112
Diffstat (limited to 'challenge-112')
-rw-r--r--challenge-112/paulo-custodio/Makefile2
-rw-r--r--challenge-112/paulo-custodio/python/ch-1.py52
-rw-r--r--challenge-112/paulo-custodio/python/ch-2.py39
3 files changed, 93 insertions, 0 deletions
diff --git a/challenge-112/paulo-custodio/Makefile b/challenge-112/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-112/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-112/paulo-custodio/python/ch-1.py b/challenge-112/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..605de4b538
--- /dev/null
+++ b/challenge-112/paulo-custodio/python/ch-1.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python3
+
+# Challenge 112
+#
+# TASK #1 - Canonical Path
+# Submitted by: Mohammad S Anwar
+# You are given a string path, starting with a slash '/'.
+#
+# Write a script to convert the given absolute path to the simplified canonical
+# path.
+#
+# In a Unix-style file system:
+#
+# - A period '.' refers to the current directory
+# - A double period '..' refers to the directory up a level
+# - Multiple consecutive slashes ('//') are treated as a single slash '/'
+# The canonical path format:
+#
+# - The path starts with a single slash '/'.
+# - Any two directories are separated by a single slash '/'.
+# - The path does not end with a trailing '/'.
+# - The path only contains the directories on the path from the root directory
+# to the target file or directory
+# Example
+# Input: "/a/"
+# Output: "/a"
+#
+# Input: "/a/b//c/"
+# Output: "/a/b/c"
+#
+# Input: "/a/b/c/../.."
+# Output: "/a"
+
+import sys
+import re
+
+def canon(path):
+ def replace(srch, rpl, s):
+ while True:
+ s, count = re.subn(srch, rpl, s)
+ if count==0:
+ return s
+
+ path = replace(r"/\./", "/", path)
+ path = replace(r"/\.$", "/", path)
+ path = replace(r"/[^\/\.]+/\.\./", "/", path)
+ path = replace(r"/[^\/\.]+/\.\.$", "/", path)
+ path = re.sub(r"/+", "/", path)
+ path = re.sub(r"/$", "", path)
+ return path
+
+print(canon(sys.argv[1]))
diff --git a/challenge-112/paulo-custodio/python/ch-2.py b/challenge-112/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..952ebe41da
--- /dev/null
+++ b/challenge-112/paulo-custodio/python/ch-2.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+# Challenge 112
+#
+# TASK #2 - Climb Stairs
+# Submitted by: Mohammad S Anwar
+# You are given $n steps to climb
+#
+# Write a script to find out the distinct ways to climb to the top. You are
+# allowed to climb either 1 or 2 steps at a time.
+#
+# Example
+# Input: $n = 3
+# Output: 3
+#
+# Option 1: 1 step + 1 step + 1 step
+# Option 2: 1 step + 2 steps
+# Option 3: 2 steps + 1 step
+#
+# Input: $n = 4
+# Output: 5
+#
+# Option 1: 1 step + 1 step + 1 step + 1 step
+# Option 2: 1 step + 1 step + 2 steps
+# Option 3: 2 steps + 1 step + 1 step
+# Option 4: 1 step + 2 steps + 1 step
+# Option 5: 2 steps + 2 steps
+
+import sys
+
+def count(n):
+ if n <= 0:
+ return 0
+ elif 1 <= n <= 2:
+ return n
+ else:
+ return count(n-1)+count(n-2)
+
+print(count(int(sys.argv[1])))