-
Notifications
You must be signed in to change notification settings - Fork 2
/
1678.py
69 lines (52 loc) · 1.81 KB
/
1678.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# [ LeetCode ] 1678. Goal Parser Interpretation
def solution(command: str) -> str:
import re
return re.sub(r"(\(\))", "o", re.sub("(\(al\))", "al", command))
def another_solution(command: str) -> str:
answer: str = ""
converted_table: dict[str, str] = { "()": "o", "(al)": "al" }
for character in command:
if character == "G":
answer += character
elif character == "(":
consecutive_word: str = "("
elif character == ")":
consecutive_word += character
answer += converted_table[consecutive_word]
consecutive_word = ""
else:
consecutive_word += character
return answer
def robust_solution(command: str) -> str:
answer: str = ""
consecutive_word: str = ""
converted_table: dict[str, str] = {"()": "o", "(al)": "al", "G": "G"}
for character in command:
consecutive_word += character
if consecutive_word in converted_table:
answer += converted_table[consecutive_word]
consecutive_word = ""
return answer
if __name__ == "__main__":
cases: list[dict[str, dict[str, str]] | str] = [
{
"input": {"command": "G()(al)"},
"output": "Goal"
},
{
"input": {"command": "G()()()()(al)"},
"output": "Gooooal"
},
{
"input": {"command": "(al)G(al)()()G"},
"output": "alGalooG"
}
]
for case in cases:
assert case["output"] == solution(command=case["input"]["command"])
assert case["output"] == another_solution(
command=case["input"]["command"]
)
assert case["output"] == robust_solution(
command=case["input"]["command"]
)