๋ฐ์ํ
๋ฌธ์ ๋งํฌ
๋์ ํ์ด
- ํ์ด์๊ฐ : 25๋ถ
- ๋ฌธ์์ด s์์กด์ฌํ๋ ๋จ์ด ํ๋ํ๋๋ฅผ ๋ฝ์์ text ์ num ๋ฆฌ์คํธ์ ์กด์ฌํ๋์ง ํ์ธํ๋ ๊ณผ์ ์ ๊ฑฐ์ณ ์ ๋ต ์ถ๋ ฅ
def solution(s):
text = ['zero','one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight','nine','ten']
num = ['1', '2', '3','4','5','6','7','8','9','0']
word = ''
result = ''
s = list(s)
while len(s) > 0:
word += s.pop(0)
if word in num:
result += word
word = ''
elif word in text:
result += str(text.index(word))
word = ''
else:
pass
return int(result)
๋๋ฌด๋๋ฌด ๋ถ๋๋ฌ์ด ์ฝ๋.... ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํจ์ผ๋ก์จ ์๊ฐ๋ณต์ก๋๊ฐ ๋์ด๋ฌ๊ณ , ํจ์จ์ด ๋จ์ด์ง๋ค.
๋์
๋๋ฆฌ๋ฅผ ํ์ฉํ๊ณ , replace ๋ฅผ ํ์ฉํ๋ฉด ์ ๋ง 5์ค๋ ์๋๊ฒ ๋ฐ๋ก ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค.
์์ โผโผ
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)
์ด๋ฉ์ด์ง.... ์ฐ์ต๋ง์ดํด์ผ๊ฒ ๋ฐ ใ ใ
๋ฐ์ํ
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ๊ท ์์ด๋ ์ถ์ฒ (level 1, ์นด์นด์ค) (0) | 2021.09.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ๋ํ๊ธฐ (level.1) (0) | 2021.09.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ตฌํ - ๋ฌธ์์ด ์์ถ (0) | 2021.09.01 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (level 1) (0) | 2021.08.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํฐ์ผ๋ชฌ (level 1) (0) | 2021.08.18 |