aboutsummaryrefslogtreecommitdiff
path: root/challenge-182
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-10-25 13:44:39 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-10-25 13:44:39 +0100
commit94b806ad0938e2e15e270fd39bb6b354697a8c37 (patch)
tree22c44a393c6bb2840dbc541867670511899fab14 /challenge-182
parent66bdb3cf94cdba75181600a1da46db0e6a21c546 (diff)
downloadperlweeklychallenge-club-94b806ad0938e2e15e270fd39bb6b354697a8c37.tar.gz
perlweeklychallenge-club-94b806ad0938e2e15e270fd39bb6b354697a8c37.tar.bz2
perlweeklychallenge-club-94b806ad0938e2e15e270fd39bb6b354697a8c37.zip
Add Python solutions
Diffstat (limited to 'challenge-182')
-rw-r--r--challenge-182/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-182/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-182/paulo-custodio/python/ch-1.py24
-rw-r--r--challenge-182/paulo-custodio/python/ch-2.py57
4 files changed, 83 insertions, 2 deletions
diff --git a/challenge-182/paulo-custodio/perl/ch-1.pl b/challenge-182/paulo-custodio/perl/ch-1.pl
index edbd2e53fd..dfc949e8f2 100644
--- a/challenge-182/paulo-custodio/perl/ch-1.pl
+++ b/challenge-182/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 182
#
diff --git a/challenge-182/paulo-custodio/perl/ch-2.pl b/challenge-182/paulo-custodio/perl/ch-2.pl
index 24a734dfc7..bba31b5b77 100644
--- a/challenge-182/paulo-custodio/perl/ch-2.pl
+++ b/challenge-182/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 182
#
diff --git a/challenge-182/paulo-custodio/python/ch-1.py b/challenge-182/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..a92ca0bd4a
--- /dev/null
+++ b/challenge-182/paulo-custodio/python/ch-1.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+
+# Challenge 182
+#
+# Task 1: Max Index
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of integers.
+#
+# Write a script to find the index of the first biggest number in the list.
+# Example
+#
+# Input: @n = (5, 2, 9, 1, 7, 6)
+# Output: 2 (as 3rd element in the list is the biggest number)
+#
+#
+# Input: @n = (4, 2, 3, 1, 5, 0)
+# Output: 4 (as 5th element in the list is the biggest number)
+
+import sys
+
+in_list = sys.argv[1:]
+result = sorted(((value, index) for index, value in enumerate(in_list)), key=lambda x: x[0], reverse=True)[0][1]
+print(result)
diff --git a/challenge-182/paulo-custodio/python/ch-2.py b/challenge-182/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..ec9469f316
--- /dev/null
+++ b/challenge-182/paulo-custodio/python/ch-2.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+
+# Challenge 182
+#
+# Task 2: Common Path
+# Submitted by: Julien Fiegehenn
+#
+# Given a list of absolute Linux file paths, determine the deepest path to the
+# directory that contains all of them.
+# Example
+#
+# Input:
+# /a/b/c/1/x.pl
+# /a/b/c/d/e/2/x.pl
+# /a/b/c/d/3/x.pl
+# /a/b/c/4/x.pl
+# /a/b/c/d/5/x.pl
+#
+# Ouput:
+# /a/b/c
+
+import sys
+
+def extract_common_prefix(paths):
+ # check if all paths have the same prefix
+ dir_prefix = None
+ for i in range(len(paths)):
+ if not paths[i]: # path empty
+ return None
+ if i == 0:
+ dir_prefix = paths[i][0] # first path
+ else:
+ if dir_prefix != paths[i][0]: # not same prefix
+ return None
+
+ # all have dir_prefix, shift if out
+ for path in paths:
+ path.pop(0)
+
+ return dir_prefix
+
+
+def common_prefix(sep, *paths):
+ # split paths by separator
+ paths = [path.split(sep) for path in paths]
+
+ # find common prefix
+ prefix = []
+ while (dir_prefix := extract_common_prefix(paths)) is not None:
+ prefix.append(dir_prefix)
+
+ return sep.join(prefix)
+
+
+sep = sys.argv[1]
+paths = sys.argv[2:]
+print(common_prefix('/', *paths))