프로그래머스 - 숫자 문자열과 영단어
내 풀이
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 = ""
tmp = ""
for char in s:
if tmp in num_alpha_dict:
answer += str(num_alpha_dict[tmp])
tmp = ""
if str(char).isdigit():
answer += str(char)
else:
tmp += char
if tmp in num_alpha_dict:
answer += str(num_alpha_dict[tmp])
return int(answer)
print(solution("one4seveneight") == 1478)
print(solution("23four5six7") == 234567)
print(solution("2three45sixseven") == 234567)
print(solution("123") == 123)
다른 사람 풀이
num_dic = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "nine":"9"}
def solution(s):
answer = s
for key, value in num_dic.items():
answer = answer.replace(key, value)
return int(answer)