Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด

๊ฐ์ž ๐Ÿฅ” 2021. 9. 1. 18:49
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

 

๋‚˜์˜ ํ’€์ด

- ํ’€์ด์‹œ๊ฐ„ : 25๋ถ„

  1. ๋ฌธ์ž์—ด 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)

์–ด๋ฉ”์ด์ง•.... ์—ฐ์Šต๋งŽ์ดํ•ด์•ผ๊ฒ ๋”ฐ ใ… ใ…  

๋ฐ˜์‘ํ˜•