-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
29 lines (22 loc) · 761 Bytes
/
example.py
File metadata and controls
29 lines (22 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sys
from itertools import product
def solve():
# Fast Read
val = sys.stdin.read().strip()
if not val:
print("[]")
return
mapping = {
"2": "abc", "3": "def", "4": "ghi", "5": "jkl",
"6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"
}
# 1. Collect the letter groups corresponding to digits
# If val is "23", groups will be ["abc", "def"]
groups = [mapping[d] for d in val if d in mapping]
# 2. Use Cartesian Product (The "C-speed" optimization)
# *groups unpacks the list so product receives ("abc", "def")
result = [''.join(combo) for combo in product(*groups)]
# The image shows printing the list format
print(result)
if __name__ == "__main__":
solve()