Algorithm
숫자 문자열과 영단어 / 프로그래머스
김예나
2025. 2. 1. 20:59
정답 (replace 사용 버전)
- dictionary.items(): 딕셔너리의 키벨류 값을 동시에 가져옴
print(num_dict.items())
# dict_items([('zero', '0'), ('one', '1'), ('two', '2'), ..., ('nine', '9')])
- s.replace(key, value) : key를 value값으로 변경해줌
def solution(s):
dictionary = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5",
"six":"6", "seven":"7", "eight":"8", "nine":"9"}
for key, value in dictionary.items():
s = s.replace(key, value)
return int(s)
정답 (replace 사용 안한 버전)
isdigit() : 문자열이 숫자인지 확인하는 함수
def solution(s):
dictionary = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5",
"six":"6", "seven":"7", "eight":"8", "nine":"9"}
temp = ""
ans = ""
for i in s:
if i.isdigit():
ans += i
else:
temp += i
if temp in dictionary:
ans += dictionary[temp]
temp = ""
return int(ans)