๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

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

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ : "one4seveneight"
  • ์ถœ๋ ฅ : 1478

2. ์ฝ”๋“œ

solution1.py

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):
    value_list = num_dic.values()
    result = []
    temp = ''
    for i,s in enumerate(S):
        #๋งŒ์•ฝ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด
        if s in value_list:
            result.append(s) #์ˆซ์ž๋ฅผ append
        #๋ฌธ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด
        else:
            temp +=s #๋ฌธ์ž๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ž„์‹œ ๋ฌธ์ž์—ด์— ๊ณ„์† ๋ถ™์ด๋‹ค๊ฐ€
            if temp in num_dic: #์ž„์‹œ๋ฌธ์ž์—ด ์ค‘ num_dic์˜ key์— ๊ฐ™์€๊ฒŒ ์žˆ์œผ๋ฉด
                result.append(num_dic[temp]) #value๋ฅผ append
                temp = '' #temp๋ฅผ ์ดˆ๊ธฐํ™”

    return int(''.join(result)) #๋ฆฌ์ŠคํŠธ -> ๋ฌธ์ž์—ด -> ์ˆซ์ž

solution2.py

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)

solution3.py

def solution(S):
    num_dic = {'zero':'0','one':'1', 'two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}
    value_list = num_dic.values()
    result = ''
    temp = ''
    for i,s in enumerate(S):
        #๋งŒ์•ฝ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด ์ˆซ์ž๋ฅผ append
        if s in value_list:
            result+=s
        #๋ฌธ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด
        else:
            temp +=s #key์— ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ž„์‹œ ๋ฌธ์ž์—ด์— ๊ณ„์† ๋ถ™์ด๋‹ค๊ฐ€
            if temp in num_dic: #num_dic์˜ key์— ๋งŒ๋“ค์–ด์ง„ ์ž„์‹œ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์€๊ฒŒ ์žˆ์œผ๋ฉด
                result+=num_dic[temp]#value๋ฅผ result์— ๋ถ™์ž„
                temp = '' #temp๋ฅผ ์ดˆ๊ธฐํ™”

    return int(result)

3. ํšŒ๊ณ 

  • ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๋ถˆํ•„์š”ํ•œ ๋ณ€์ˆ˜๋ฅผ ๋งŽ์ด ์“ด ๊ฒƒ ๊ฐ™๋‹ค.
  • ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ''.join(๋ฆฌ์ŠคํŠธ) ๋Š” ๊ธฐ์–ตํ•ด๋‘์ž
  • ๋”•์…”๋„ˆ๋ฆฌ์— ํŠน์ • ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•  ๋•Œ๋Š” 'for '๋ฌธ์ž์—ด' in ๋”•์…”๋„ˆ๋ฆฌ:'๋ฅผ ์“ฐ๋ฉด๋˜๋Š”๋ฐ, ๋ฌธ์ž์—ด์ด key์— ์กด์žฌํ•˜๋Š”์ง€๋งŒ ์•Œ๋ ค์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋”•์…”๋„ˆ๋ฆฌ์˜ key์™€ value๋ฅผ ์ž˜ ์ •ํ•˜๋Š”๊ฒŒ ์ค‘์š”ํ–ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ์ด ๋ชจ๋“  ๊ฑด replace ๋ฅผ ์“ฐ๋ฉด ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€๋ฆฐ๋‹ค... (solution2.py)
  • num_dic.items()๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด Key์™€ Value์˜ ์Œ์„ ํŠœํ”Œ๋กœ ๋ฌถ์€ ๊ฐ’์„ ๋ฆฌ์ŠคํŠธํ˜•ํƒœ๋กœ ๋Œ๋ ค์ค€๋‹ค. key, value ๊ฐ’์ด ๋‘˜ ๋‹ค ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅ
    dict_items([('zero', '0'), ('one', '1'), ('two', '2'), ('three', '3'), ('four', '4'), ('five', '5'), ('six', '6'), ('seven', '7'), ('eight', '8'), ('nine', '9')])
  • ๋”•์…”๋„ˆ๋ฆฌ.keys(), .values(), .items() ๋“ค์€ ๋ชจ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ ค์ค€๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž
  • solution3.py: ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ’€๋‹ค๊ฐ€ solution1.py์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค. reulst = [] ์ฒ˜๋Ÿผ ๊ตณ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์“ฐ์ง€์•Š๊ณ , reulst = '' ์ด๋ ‡๊ฒŒ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋” ํŽธํ•˜๋‹ค.