๐ฏ CodingTest/Programmers
[Programmers] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด
S.Honey
2022. 4. 9. 23:51
def solution(s):
words = {'zero': '0', 'one':'1', 'two':'2', 'three':'3','four':'4', 'five':'5', 'six':'6', 'seven':'7','eight':'8','nine':'9'}
temp = ''
answer = ''
for c in s:
if c in words.values() :
answer += c
else:
temp += c
if temp in words.keys() :
answer += words[temp]
temp = ''
return int(answer)
- ์ด๊ธฐ
words
๋์ ๋๋ฆฌ์ ์ซ์์ ์๋ฌธํ๊ธฐ๋ฅผ ํค๋กํ๊ณ ์ซ์(๋ฌธ์์ด)๋ฅผ ๊ฐ์ผ๋ก ๊ฐ์ง๋ ๋์ ๋๋ฆฌ๋ก ์ด๊ธฐํ temp
๋ฌธ์์ด๊ณผanswer
๋ฌธ์์ด ์ ์ธ ๋ฐ ๊ฐ๊ฐ ๋น ๋ฌธ์์ด๋ก ์ด๊ธฐํ- ๋งค๊ฐ๋ณ์๋ก ๋ค์ด์จ
s
(๋ฌธ์์ด) ๋ด๋ถ์c
(ํ๊ธ์)๋ฅผ ๊ฐ์ ธ์words.values()
์ ์๋ ์ซ์ํํ์ ๋ฌธ์๋ค๊ณผ ๋น๊ตํด ์๋์ง ์ฒดํฌ - ๋ง์ผ ์๋ค๋ฉด ๋ฐ๋ก
answer
๋ฌธ์์ด์ ์ถ๊ฐํ๊ณ ์๋ค๋ฉดtemp
๋ฌธ์์ด์ ์ถ๊ฐ - ์ดํ
words.keys()
๋ฅผ ํตํดkey
๋ค์ ๊ฐ์ ธ์ ํด๋นkey
๋ค ์คtemp
์ ๋์ ๋ ๋ฌธ์๊ฐ ์๋์ง ๋น๊ต - ๋ง์ผ
temp
์ ๋์ ๋ ๋ฌธ์์ ๋์ผํ ๋ฌธ์๊ฐ ์๋ค๋ฉดanswer
์words[temp]
๋ก ๊ฐ์ ๋ฌธ์์ด์ ์ถ๊ฐํด์ฃผ๊ณ , ์๋ค๋ฉด ๋ฐ๋ณต๋ฌธ์ ๊ณ์ ์งํ - ๋์ ๋
answer
๋ฌธ์์ด์int
ํ์ผ๋ก ํ๋ณํ ํ ์ต์ข ๊ฒฐ๊ณผ ๋ฐํ