문제 링크
https://programmers.co.kr/learn/courses/30/lessons/81301
from collections import deque
def solution(s):
answer = []
num_alpha_dict = {
"zero":"0",
"one":"1",
"two":"2",
"three":"3",
"four":"4",
"five":"5",
"six":"6",
"seven":"7",
"eight":"8",
"nine":"9"
}
if s.isdigit():
return int(s)
else:
s_deq = deque(s)
check = []
for a in s:
if a.isalpha():
check.append(s_deq.popleft())
if "".join(check) in num_alpha_dict.keys():
answer.append(num_alpha_dict["".join(check)])
check = []
else:
answer.append(s_deq.popleft())
return int("".join(answer))
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
count : 변경할 횟수를 지정할 수 있다. default는 -1으로, 모든 old를 변경한다.
replace 함수를 사용하면 이렇게 간단하게 작성할 수 있다...!!
def solution(s):
num_alpha_dict = {
"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "nine":"9"
}
answer = s
for key, value in num_alpha_dict.items():
answer = answer.replace(key, value)
return int(answer)