aboutsummaryrefslogtreecommitdiff
path: root/challenge-067/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-067/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-067/paulo-custodio/python/ch-2.py46
1 files changed, 46 insertions, 0 deletions
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) + "]")