aboutsummaryrefslogtreecommitdiff
path: root/challenge-067/paulo-custodio/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-20 21:40:27 +0100
committerGitHub <noreply@github.com>2024-09-20 21:40:27 +0100
commitb521af146ef7c2bd338bc5355e2fc8f6c49cbd30 (patch)
tree8d8e24fcdbb56212b91343e1306cce425826dc53 /challenge-067/paulo-custodio/python
parent962fb60ab30715e2dedc4fda8619042d22fba65e (diff)
parent4ae5477a9bc6c8b01ee984ed6a5a90dbec170833 (diff)
downloadperlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.gz
perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.bz2
perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.zip
Merge pull request #10874 from pauloscustodio/master
Add Python solution to challenge 067
Diffstat (limited to 'challenge-067/paulo-custodio/python')
-rw-r--r--challenge-067/paulo-custodio/python/ch-1.py28
-rw-r--r--challenge-067/paulo-custodio/python/ch-2.py46
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-067/paulo-custodio/python/ch-1.py b/challenge-067/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..2c0021ce8f
--- /dev/null
+++ b/challenge-067/paulo-custodio/python/ch-1.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python3
+
+# Challenge 067
+#
+# TASK #1 > Number Combinations
+# Submitted by: Mohammad S Anwar
+#
+# You are given two integers $m and $n. Write a script print all possible
+# combinations of $n numbers from the list 1 2 3 ... $m.
+#
+# Every combination should be sorted i.e. [2,3] is valid combination but [3,2] is not.
+#
+# Example:
+# Input: $m = 5, $n = 2
+#
+# Output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ]
+
+import sys
+from itertools import combinations
+
+m, n = map(int, sys.argv[1:3])
+m_list = list(range(1, m + 1))
+out = set()
+
+for combo in combinations(m_list, n):
+ out.add(str(sorted(combo)))
+
+print("[", ", ".join(sorted(out)), "]")
diff --git a/challenge-067/paulo-custodio/python/ch-2.py b/challenge-067/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..4a9e61a95c
--- /dev/null
+++ b/challenge-067/paulo-custodio/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+# Challenge 067
+#
+# TASK #2 > Letter Phone
+# Submitted by: Mohammad S Anwar
+#
+# You are given a digit string $S. Write a script to print all possible letter
+# combinations that the given digit string could represent.
+#
+# Letter Phone
+#
+#
+# Example:
+# Input: $S = '35'
+#
+# Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"].
+
+import sys
+from itertools import product
+
+digits = {
+ '1': ['_', ',', '@'],
+ '2': ['a', 'b', 'c'],
+ '3': ['d', 'e', 'f'],
+ '4': ['g', 'h', 'i'],
+ '5': ['j', 'k', 'l'],
+ '6': ['m', 'n', 'o'],
+ '7': ['p', 'q', 'r', 's'],
+ '8': ['t', 'u', 'v'],
+ '9': ['w', 'x', 'y', 'z'],
+ '*': [' ']
+}
+
+def letter_phone(s):
+ if not s:
+ return []
+
+ digit_lists = [digits[digit] for digit in s]
+ combinations = [''.join(combo) for combo in product(*digit_lists)]
+ return sorted(combinations)
+
+# Example usage
+s = sys.argv[1]
+out = letter_phone(s)
+print("[" + ", ".join(f'"{item}"' for item in out) + "]")