๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[Programmers] [2018 KAKAO BLIND RECRUITMENT] [Python] Level1_๋‹คํŠธ ๊ฒŒ์ž„

Dbswnstjd 2022. 10. 27. 16:38

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/17682

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ’€์ด

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 1๋‹จ๊ณ„ - ๋‹คํŠธ๊ฒŒ์ž„
def solution(dartResult):
    stack = []
    dartResult = dartResult.replace("10", "A")
    bonus = {'S': 1, 'D': 2, 'T': 3}
    
    for i in dartResult:
        if i.isdigit() or i=='A':
            stack.append(10 if i == 'A' else int(i))
        elif i in ['S', 'D', 'T']:
            num = stack.pop()
            stack.append(num ** bonus[i])
        elif i == '#':
            stack[-1] *= -1
        elif i == '*':
            num = stack.pop()
            if len(stack):
                stack[-1] *= 2
            stack.append(2 * num)
    return sum(stack)

์ฒ˜์Œ์— 10์ด๋ผ๋Š” ์ˆซ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ 10์„ A์ด๋ผ๊ณ  ๋Œ€์ฒดํ•˜๊ณ  if ๋ฌธ์—์„œ A๋ฅผ ๋‹ค์‹œ int(10)์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค. 

๊ทธ๋ฆฌ๊ณ  bonus๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์„ ์–ธํ•˜์—ฌ ์ œ๊ณฑ์„ ๋งŒ๋“ค์—ˆ๋‹ค.