09009
[프로그래머스 lv1] 숫자 문자열과 영단어 본문
문제 보기
https://school.programmers.co.kr/learn/courses/30/lessons/81301
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 해결
replace()를 이용하여 해결한다.
.replace("찾을 문자열", "변경할 문자열")
소스 코드
- 직접 해결한 풀이
def solution(s):
word = {'zero':0, 'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8,'nine':9}
tmp = ''
answer = ''
for i in s:
if i.isalpha():
tmp += i
if tmp in word:
answer += str(word[tmp])
tmp = ''
else:
answer += str(i)
return int(answer)
def solution(s):
answer = 0
word = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5",
"six":"6", "seven":"7", "eight":"8", "nine":"9"}
tmp = ''
answer = ''
for i in s:
if i.isdigit():
answer += i
else:
tmp += i
if tmp in word:
answer += word[tmp]
tmp = ''
return int(answer)
- 참고 풀이
def solution(s):
word = {'zero':'0', 'one':'1', 'two':'2', 'three':'3', 'four':'4', 'five':'5', 'six':'6', 'seven':'7', 'eight':'8','nine':'9'}
for k,v in word.items():
s = s.replace(k, v)
return int(s)
'Algorithm > 문자열' 카테고리의 다른 글
[프로그래머스 lv2] [3차] 방금그곡 (0) | 2023.10.13 |
---|---|
[프로그래머스 lv1] 가장 가까운 같은 글자 (0) | 2023.10.08 |
[프로그래머스 lv1] 가장 가까운 같은 글자 (0) | 2023.09.24 |
[프로그래머스 lv1] 숫자 문자열과 영단어 (0) | 2023.09.23 |
[프로그래머스 lv1] 문자열 다루기 기본 (0) | 2023.09.23 |