๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, 2018 KAKAO BLIND RECRUITMENT] [1์ฐจ]๋‹คํŠธ ๊ฒŒ์ž„(Lv.1) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 9. 6.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

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

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

programmers.co.kr

 

ํ’€์ด

1. ์ดˆ๊ธฐํ™”

answer = []
i = 0
length = len(dartResult)
  • answer: ๊ฐ ๋ผ์šด๋“œ์—์„œ ๊ณ„์‚ฐ๋œ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ์ด 3๋ฒˆ์˜ ๋ผ์šด๋“œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ ์ ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ฐจ๋ก€๋Œ€๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • i = 0: ๋ฌธ์ž์—ด์„ ํƒ์ƒ‰ํ•˜๋Š” ์ธ๋ฑ์Šค ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•  ๋•Œ, ๋ช‡ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ถ”์ ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • length = len(dartResult): ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋งค๋ฒˆ len(dartResult)๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋˜์–ด, ์„ฑ๋Šฅ ์ธก๋ฉด์—์„œ ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. length๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๋„˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

2. ์ˆซ์ž ์ฒ˜๋ฆฌ

if dartResult[i] == '1' and i + 1 < length and dartResult[i + 1] == '0':
    score = 10
    i += 2
else:
    score = int(dartResult[i])
    i += 1
  • 10 ์ฒ˜๋ฆฌ: ๋‹คํŠธ ์ ์ˆ˜๋Š” 0์—์„œ 10 ์‚ฌ์ด์˜ ์ˆซ์ž์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 10์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด dartResult[i] == '1'์ผ ๋•Œ, ๋ฐ”๋กœ ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ '0'์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋งž๋‹ค๋ฉด, ์ ์ˆ˜๊ฐ€ 10์ž„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๋‘ ๋ฌธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ i๋ฅผ 2๋งŒํผ ์ฆ๊ฐ€์‹œ์ผœ์„œ ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
  • ๋‹จ์ผ ์ˆซ์ž ์ฒ˜๋ฆฌ: ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด, ํ•œ ์ž๋ฆฌ ์ˆซ์ž(0๋ถ€ํ„ฐ 9)๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฌธ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜(int(dartResult[i]))ํ•˜์—ฌ score์— ์ €์žฅํ•˜๊ณ , i๋ฅผ 1๋งŒํผ ์ฆ๊ฐ€์‹œ์ผœ ๋‹ค์Œ ๋ฌธ์ž๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

3. ๋ณด๋„ˆ์Šค ์ฒ˜๋ฆฌ

if dartResult[i] == 'S':
    score = score ** 1
elif dartResult[i] == 'D':
    score = score ** 2
elif dartResult[i] == 'T':
    score = score ** 3
i += 1
  • ๋ณด๋„ˆ์Šค ์ฒ˜๋ฆฌ: ์ˆซ์ž ๋ฐ”๋กœ ๋’ค์—๋Š” S, D, T ์ค‘ ํ•˜๋‚˜๊ฐ€ ์˜ต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ๊ฐ ์ ์ˆ˜์˜ 1์ œ๊ณฑ, 2์ œ๊ณฑ, 3์ œ๊ณฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • S: 1์ œ๊ณฑ
    • D: 2์ œ๊ณฑ
    • T: 3์ œ๊ณฑ
  • ๋ณด๋„ˆ์Šค ๋ฌธ์ž๊ฐ€ ์ฒ˜๋ฆฌ๋œ ํ›„์—๋Š” i๋ฅผ 1๋งŒํผ ์ฆ๊ฐ€์‹œ์ผœ ๋‹ค์Œ ๋ฌธ์ž๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

4. ์˜ต์…˜ ์ฒ˜๋ฆฌ

if i < length and (dartResult[i] == '*' or dartResult[i] == '#'):
    if dartResult[i] == '*':
        if answer:
            answer[-1] *= 2  # ์ด์ „ ์ ์ˆ˜ 2๋ฐฐ ์ฒ˜๋ฆฌ
        score *= 2  # ํ˜„์žฌ ์ ์ˆ˜ 2๋ฐฐ
    elif dartResult[i] == '#':
        score *= -1  # ํ˜„์žฌ ์ ์ˆ˜ ๋งˆ์ด๋„ˆ์Šค ์ฒ˜๋ฆฌ
    i += 1

 

  • ์˜ต์…˜ ์ฒ˜๋ฆฌ: ๋ณด๋„ˆ์Šค ๋’ค์—๋Š” *(์Šคํƒ€์ƒ) ๋˜๋Š” #(์•„์ฐจ์ƒ)์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์ด ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์œผ๋ฏ€๋กœ i < length ์กฐ๊ฑด์œผ๋กœ ๋ฌธ์ž์—ด์ด ๋๋‚˜์ง€ ์•Š์•˜๋Š”์ง€ ๋จผ์ € ํ™•์ธํ•œ ํ›„์— ์˜ต์…˜์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    • *(์Šคํƒ€์ƒ): ํ˜„์žฌ ์ ์ˆ˜๋ฅผ 2๋ฐฐ๋กœ ํ•˜๊ณ , ์ด์ „ ์ ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ ์ ์ˆ˜๋„ 2๋ฐฐ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    • #(์•„์ฐจ์ƒ): ํ˜„์žฌ ์ ์ˆ˜๋ฅผ ๋งˆ์ด๋„ˆ์Šค๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ์˜ต์…˜ ๋ฌธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„ i๋ฅผ 1๋งŒํผ ์ฆ๊ฐ€์‹œ์ผœ ๋‹ค์Œ ๋ฌธ์ž๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

5. ์ ์ˆ˜ ์ €์žฅ

answer.append(score)
  • ๊ณ„์‚ฐ๋œ ์ ์ˆ˜๋ฅผ answer ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฆฌ์ŠคํŠธ์—๋Š” ๊ฐ ๋ผ์šด๋“œ์—์„œ ๊ณ„์‚ฐ๋œ ์ ์ˆ˜๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

 

์ตœ์ข… ์ฝ”๋“œ

def solution(dartResult):
    answer = []
    i = 0
    length = len(dartResult)
    
    while i < length:
        # ์ˆซ์ž ์ฒ˜๋ฆฌ (10 ์ฒ˜๋ฆฌ ํฌํ•จ)
        if dartResult[i] == '1' and i + 1 < length and dartResult[i + 1] == '0':
            score = 10
            i += 2
        else:
            score = int(dartResult[i])
            i += 1
        
        # ๋ณด๋„ˆ์Šค ์ฒ˜๋ฆฌ (S, D, T)
        if dartResult[i] == 'S':
            score = score ** 1
        elif dartResult[i] == 'D':
            score = score ** 2
        elif dartResult[i] == 'T':
            score = score ** 3
        i += 1
        
        # ์˜ต์…˜ ์ฒ˜๋ฆฌ (*, #)
        if i < length and (dartResult[i] == '*' or dartResult[i] == '#'):
            if dartResult[i] == '*':
                if answer:
                    answer[-1] *= 2  # ์ด์ „ ์ ์ˆ˜ 2๋ฐฐ ์ฒ˜๋ฆฌ
                score *= 2  # ํ˜„์žฌ ์ ์ˆ˜ 2๋ฐฐ
            elif dartResult[i] == '#':
                score *= -1  # ํ˜„์žฌ ์ ์ˆ˜ ๋งˆ์ด๋„ˆ์Šค ์ฒ˜๋ฆฌ
            i += 1
        
        # ์ ์ˆ˜๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
        answer.append(score)
    
    return sum(answer)

 

 

 

728x90
๋ฐ˜์‘ํ˜•