정답 (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)
'Algorithm' 카테고리의 다른 글
K번째수 / 프로그래머스 (0) | 2025.02.06 |
---|---|
문자열 내맘대로 정렬하기 / 프로그래머스 (0) | 2025.02.05 |
시저 암호 / 프로그래머스 (0) | 2025.01.27 |
최소직사각형 / 프로그래머스 (0) | 2025.01.24 |
2606 바이러스 / DFS,BFS (0) | 2025.01.22 |