๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿฆ„ | ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

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

by KASSID 2021. 12. 15.

๋ชฉ์ฐจ

    728x90

    ์‚ฌ์šฉ์–ธ์–ด)

    python3

     

    ๋ฌธ์ œ)

     

    ๋„ค์˜ค์™€ ํ”„๋กœ๋„๊ฐ€ ์ˆซ์ž๋†€์ด๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์˜ค๊ฐ€ ํ”„๋กœ๋„์—๊ฒŒ ์ˆซ์ž๋ฅผ ๊ฑด๋„ฌ ๋•Œ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๋ฅผ ์˜๋‹จ์–ด๋กœ ๋ฐ”๊พผ ์นด๋“œ๋ฅผ ๊ฑด๋„ค์ฃผ๋ฉด ํ”„๋กœ๋„๋Š” ์›๋ž˜ ์ˆซ์ž๋ฅผ ์ฐพ๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค.

    ๋‹ค์Œ์€ ์ˆซ์ž์˜ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๋ฅผ ์˜๋‹จ์–ด๋กœ ๋ฐ”๊พธ๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

    • 1478 → "one4seveneight"
    • 234567 → "23four5six7"
    • 10203 → "1zerotwozero3"

    ์ด๋ ‡๊ฒŒ ์ˆซ์ž์˜ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๊ฐ€ ์˜๋‹จ์–ด๋กœ ๋ฐ”๋€Œ์–ด์กŒ๊ฑฐ๋‚˜, ํ˜น์€ ๋ฐ”๋€Œ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ์ธ ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. s๊ฐ€ ์˜๋ฏธํ•˜๋Š” ์›๋ž˜ ์ˆซ์ž๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

    ์ฐธ๊ณ ๋กœ ๊ฐ ์ˆซ์ž์— ๋Œ€์‘๋˜๋Š” ์˜๋‹จ์–ด๋Š” ๋‹ค์Œ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์ˆซ์ž์˜๋‹จ์–ด

    0 zero
    1 one
    2 two
    3 three
    4 four
    5 five
    6 six
    7 seven
    8 eight
    9 nine

    ์ œํ•œ์‚ฌํ•ญ

    • 1 ≤ s์˜ ๊ธธ์ด ≤ 50
    • s๊ฐ€ "zero" ๋˜๋Š” "0"์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • return ๊ฐ’์ด 1 ์ด์ƒ 2,000,000,000 ์ดํ•˜์˜ ์ •์ˆ˜๊ฐ€ ๋˜๋Š” ์˜ฌ๋ฐ”๋ฅธ ์ž…๋ ฅ๋งŒ s๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ

    sresult

    "one4seveneight" 1478
    "23four5six7" 234567
    "2three45sixseven" 234567
    "123" 123

    ์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

    ์ž…์ถœ๋ ฅ ์˜ˆ #1

    • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ #2

    • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ #3

    • "three"๋Š” 3, "six"๋Š” 6, "seven"์€ 7์— ๋Œ€์‘๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋‹ต์€ ์ž…์ถœ๋ ฅ ์˜ˆ #2์™€ ๊ฐ™์€ 234567์ด ๋ฉ๋‹ˆ๋‹ค.
    • ์ž…์ถœ๋ ฅ ์˜ˆ #2์™€ #3๊ณผ ๊ฐ™์ด ๊ฐ™์€ ์ •๋‹ต์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ #4

    • s์—๋Š” ์˜๋‹จ์–ด๋กœ ๋ฐ”๋€ ๋ถ€๋ถ„์ด ์—†์Šต๋‹ˆ๋‹ค.

    ์ œํ•œ์‹œ๊ฐ„ ์•ˆ๋‚ด

    • ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ : 10์ดˆ

     


    ๋‚˜์˜ ๋‹ต)

    def solution(s):
        sArr = ['zero','one','two','three','four','five','six','seven','eight','nine']
        nArr = ['0','1','2','3','4','5','6','7','8','9']
        
        for i in range(len(sArr)):
            if (sArr[i] in s):
                s = s.replace(sArr[i],nArr[i])
        answer = int(s)
            
        return answer

     

    ํ”ผ๋“œ๋ฐฑ)

    ๋ฆฌ์ŠคํŠธ 2๊ฐœ๋กœ ํ•˜์ง€ ๋ง๊ณ  ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊น”๋”ํ•˜๊ณ  ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™๋‹ค.

    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)

    ๋‹ค๋ฅธ ํ’€์ด)

    def solution(s):
    
        eng = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    
        if s.isdigit(): # ์ˆซ์ž์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ํ•จ์ˆ˜
            return int(s) # ์ˆซ์ž๋ผ๋ฉด ๋ฐ”๋กœ๋ฆฌํ„ด
    
        # ์ˆซ์ž๋ฉด ๋„˜์–ด๊ฐ€๊ณ  ๋ฌธ์ž์—ด์ด๋ผ๋ฉด ๋‹ค์Œ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€ ๋‹จ์–ด๋ฅผ ์™„์„ฑํ•œ๋‹ค. 
        # ๋ฌธ์ž์—ด์ด ์ด์–ด์ ธ์žˆ์„์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ํ•œ๋‹จ์–ด์”ฉ ๋ฌธ์ž์—ด์— ํ•ฉ์น ๋•Œ๋งˆ๋‹ค eng๋ฐฐ์—ด์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์žˆ์œผ๋ฉด temp๋ฅผ ๋นˆ๋ฌธ์ž์—ด๋กœ ์ดˆ๊ธฐํ™”์‹œํ‚จ๋‹ค.
    
        answer = ''
        temp = ''
        for i in s:
            if i.isdigit():
                answer+=i
            # ๋ฌธ์ž์—ด์ด๋ฉด 
            else:
                temp += i
                if temp in eng:
                    answer += str(eng.index(temp))
                    temp = ''
     
        return int(answer)

    ๋‹ค๋ฅธ ๋ถ„์˜ ์ฝ”๋“œ์ธ๋ฐ isdigit() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ˆซ์ž์ธ์ง€ ํŒ๋‹จ์„ ํ•˜๊ณ 

    ์ˆซ์ž๋ผ๋ฉด ๋ฐ”๋กœ ์ฒจ๋ถ€ํ•˜๊ณ ,

    ๋ฌธ์ž์—ด์ด๋ผ๋ฉด ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋ฌธ์ž์—ด์„ ํ•ฉ์นœ ๋’ค์— ๊ทธ ๊ฒƒ์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š”

    ๊ทธ๋Ÿฐ ๋ฐฉ์‹์ด์—ˆ๋‹ค!

    ์ฒ˜์Œ ๋ณด๋Š” ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํฅ๋ฏธ๋กœ์› ๋‹ค.

     

    isdigitํ•จ์ˆ˜๋Š” C/C++์—์„œ๋„ ๊ฐ๊ฐ <ctype.h>/<cctype> ํ—ค๋”ํŒŒ์ผ์„ ์„ ์–ธํ•ด์ฃผ๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!

     

     

    ๋ฌธ์ œlink)

    https://programmers.co.kr/learn/courses/30/lessons/81301

    ๋Œ“๊ธ€