[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹คํŠธ๊ฒŒ์ž„ /ํŒŒ์ด์ฌ /Python/ 2018 KAKAO BLIND RECRUITMENT/ ์นด์นด์˜ค ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹คํŠธ๊ฒŒ์ž„ /ํŒŒ์ด์ฌ /Python/ 2018 KAKAO BLIND RECRUITMENT/ ์นด์นด์˜ค ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions )

๐Ÿ’ฌ ๋กœ์ง ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ๋ฌธ์ž์—ด ์„ธํŠธ๋ฅผ ๊ธฐํšŒ๋ณ„๋กœ ๋‚˜๋ˆ„๋Š” ๋กœ์ง๊ณผ ์ ์ˆ˜ ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์ง.

๐Ÿ’ฌ ๋จผ์ € ์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด ์„ธํŠธ๋ฅผ ๊ธฐํšŒ๋ณ„๋กœ ๋‚˜๋ˆ ์„œ ๋”•์…”๋„ˆ๋ฆฌ res์— ๋„ฃ์–ด์ฃผ๊ธฐ

๐Ÿ’ฌ ์ด๋•Œ ์ ์ˆ˜๊ฐ€ 10์ธ ๊ฒฝ์šฐ ์ฃผ์˜ ->'_'๋กœ ์น˜ํ™˜ํ•ด์„œ ๋„ฃ์–ด์ฃผ๊ธฐ

๐Ÿ’ฌ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํ‚ค, ๋ฐธ๋ฅ˜๊ฐ’์„ ๋ฝ‘์•„์™€์„œ ๊ฐ๊ฐ ์ ์ˆ˜, ๋ณด๋„ˆ์Šค, ์˜ต์…˜ ๋ณ„๋กœ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์ค€ ํ›„, ๋‹ค์‹œ ๋”•์…”๋„ˆ๋ฆฌ ์›๋ž˜ ์ž๋ฆฌ์— ๋„ฃ์–ด์ฃผ๊ธฐ

๐Ÿ’ฌ ํ•ฉ์‚ฐํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ’ ๋ฆฌํ„ด

 

๐ŸŽซcode )

def solution(dartResult):
    answer = 0
    res = {}
    idx = -1
    check = 1
    
    # ๊ธฐํšŒ๋ณ„ ์ ์ˆ˜ -> ๋”•์…”๋„ˆ๋ฆฌ์— ๊ตฌ๋ถ„ํ•ด ๋„ฃ๊ธฐ
    for i in dartResult:
        if i.isdigit():
            if check == 1:
                idx += 1
                res[idx] = ''
                res[idx] += i
            elif check == 0: # 10์ฒ˜๋ฆฌ
                res[idx] = '_'
            check = 0
        else:
            res[idx] += i
            check = 1

    for key, value in res.items():
        tmp = 0
        for i in value:
            # ์ ์ˆ˜
            if i.isdigit():
                tmp = int(i)
            # 10 ์ฒ˜๋ฆฌ
            elif i == '_':
                tmp = 10

            # ๋ณด๋„ˆ์Šค
            elif i == 'S':
                tmp = tmp ** 1
            elif i == 'D':
                tmp = tmp ** 2
            elif i == 'T':
                tmp = tmp ** 3

            # ์˜ต์…˜
            if i == '*':
                tmp = tmp * 2
                res[key] = tmp
                if key != 0:
                    res[key - 1] = res[key - 1] * 2
                    break
                break
            elif i == '#':
                res[key] = -tmp
                break
            res[key] = tmp
    # ํ•ฉ์‚ฐ
    for i in res.values():
        answer += i
    return answer

 

๐Ÿ“Œ description )

๋ฌธ์ œ์ถœ์ฒ˜ : programmers.co.kr/learn/courses/30/lessons/17682

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ๋‹คํŠธ ๊ฒŒ์ž„

 

programmers.co.kr

๋‹คํŠธ ๊ฒŒ์ž„

์นด์นด์˜คํ†ก์— ๋œฌ ๋„ค ๋ฒˆ์งธ ๋ณ„! ์‹ฌ์‹ฌํ•  ๋•? ์นด์นด์˜คํ†ก ๊ฒŒ์ž„๋ณ„~

[์•„์ด์œ ๐Ÿ’™]


์นด์นด์˜คํ†ก ๊ฒŒ์ž„๋ณ„์˜ ํ•˜๋ฐ˜๊ธฐ ์‹ ๊ทœ ์„œ๋น„์Šค๋กœ ๋‹คํŠธ ๊ฒŒ์ž„์„ ์ถœ์‹œํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. ๋‹คํŠธ ๊ฒŒ์ž„์€ ๋‹คํŠธํŒ์— ๋‹คํŠธ๋ฅผ ์„ธ ์ฐจ๋ก€ ๋˜์ ธ ๊ทธ ์ ์ˆ˜์˜ ํ•ฉ๊ณ„๋กœ ์‹ค๋ ฅ์„ ๊ฒจ๋ฃจ๋Š” ๊ฒŒ์ž„์œผ๋กœ, ๋ชจ๋‘๊ฐ€ ๊ฐ„๋‹จํžˆ ์ฆ๊ธธ ์ˆ˜ ์žˆ๋‹ค.
๊ฐ“ ์ž…์‚ฌํ•œ ๋ฌด์ง€๋Š” ์ฝ”๋”ฉ ์‹ค๋ ฅ์„ ์ธ์ •๋ฐ›์•„ ๊ฒŒ์ž„์˜ ํ•ต์‹ฌ ๋ถ€๋ถ„์ธ ์ ์ˆ˜ ๊ณ„์‚ฐ ๋กœ์ง์„ ๋งก๊ฒŒ ๋˜์—ˆ๋‹ค. ๋‹คํŠธ ๊ฒŒ์ž„์˜ ์ ์ˆ˜ ๊ณ„์‚ฐ ๋กœ์ง์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. ๋‹คํŠธ ๊ฒŒ์ž„์€ ์ด 3๋ฒˆ์˜ ๊ธฐํšŒ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
  2. ๊ฐ ๊ธฐํšŒ๋งˆ๋‹ค ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ ์ˆ˜๋Š” 0์ ์—์„œ 10์ ๊นŒ์ง€์ด๋‹ค.
  3. ์ ์ˆ˜์™€ ํ•จ๊ป˜ Single(S), Double(D), Triple(T) ์˜์—ญ์ด ์กด์žฌํ•˜๊ณ  ๊ฐ ์˜์—ญ ๋‹น์ฒจ ์‹œ ์ ์ˆ˜์—์„œ 1์ œ๊ณฑ, 2์ œ๊ณฑ, 3์ œ๊ณฑ (์ ์ˆ˜1 , ์ ์ˆ˜2 , ์ ์ˆ˜3 )์œผ๋กœ ๊ณ„์‚ฐ๋œ๋‹ค.
  4. ์˜ต์…˜์œผ๋กœ ์Šคํƒ€์ƒ(*) , ์•„์ฐจ์ƒ(#)์ด ์กด์žฌํ•˜๋ฉฐ ์Šคํƒ€์ƒ(*) ๋‹น์ฒจ ์‹œ ํ•ด๋‹น ์ ์ˆ˜์™€ ๋ฐ”๋กœ ์ „์— ์–ป์€ ์ ์ˆ˜๋ฅผ ๊ฐ 2๋ฐฐ๋กœ ๋งŒ๋“ ๋‹ค. ์•„์ฐจ์ƒ(#) ๋‹น์ฒจ ์‹œ ํ•ด๋‹น ์ ์ˆ˜๋Š” ๋งˆ์ด๋„ˆ์Šค๋œ๋‹ค.
  5. ์Šคํƒ€์ƒ(*)์€ ์ฒซ ๋ฒˆ์งธ ๊ธฐํšŒ์—์„œ๋„ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ์Šคํƒ€์ƒ(*)์˜ ์ ์ˆ˜๋งŒ 2๋ฐฐ๊ฐ€ ๋œ๋‹ค. (์˜ˆ์ œ 4๋ฒˆ ์ฐธ๊ณ )
  6. ์Šคํƒ€์ƒ(*)์˜ ํšจ๊ณผ๋Š” ๋‹ค๋ฅธ ์Šคํƒ€์ƒ(*)์˜ ํšจ๊ณผ์™€ ์ค‘์ฒฉ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ค‘์ฒฉ๋œ ์Šคํƒ€์ƒ(*) ์ ์ˆ˜๋Š” 4๋ฐฐ๊ฐ€ ๋œ๋‹ค. (์˜ˆ์ œ 4๋ฒˆ ์ฐธ๊ณ )
  7. ์Šคํƒ€์ƒ(*)์˜ ํšจ๊ณผ๋Š” ์•„์ฐจ์ƒ(#)์˜ ํšจ๊ณผ์™€ ์ค‘์ฒฉ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ค‘์ฒฉ๋œ ์•„์ฐจ์ƒ(#)์˜ ์ ์ˆ˜๋Š” -2๋ฐฐ๊ฐ€ ๋œ๋‹ค. (์˜ˆ์ œ 5๋ฒˆ ์ฐธ๊ณ )
  8. Single(S), Double(D), Triple(T)์€ ์ ์ˆ˜๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์กด์žฌํ•œ๋‹ค.
  9. ์Šคํƒ€์ƒ(*), ์•„์ฐจ์ƒ(#)์€ ์ ์ˆ˜๋งˆ๋‹ค ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ์กด์žฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์กด์žฌํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.

0~10์˜ ์ •์ˆ˜์™€ ๋ฌธ์ž S, D, T, *, #๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋  ์‹œ ์ด์ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋ผ.

์ž…๋ ฅ ํ˜•์‹

์ ์ˆ˜|๋ณด๋„ˆ์Šค|[์˜ต์…˜]์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด 3์„ธํŠธ.
์˜ˆ) 1S2D*3T

  • ์ ์ˆ˜๋Š” 0์—์„œ 10 ์‚ฌ์ด์˜ ์ •์ˆ˜์ด๋‹ค.
  • ๋ณด๋„ˆ์Šค๋Š” S, D, T ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
  • ์˜ต์„ ์€ *์ด๋‚˜ # ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ, ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์ถœ๋ ฅ ํ˜•์‹

3๋ฒˆ์˜ ๊ธฐํšŒ์—์„œ ์–ป์€ ์ ์ˆ˜ ํ•ฉ๊ณ„์— ํ•ด๋‹นํ•˜๋Š” ์ •์ˆ˜๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
์˜ˆ) 37

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

์˜ˆ์ œdartResultanswer์„ค๋ช…
1 1S2D*3T 37 11 * 2 + 22 * 2 + 33
2 1D2S#10S 9 12 + 21 * (-1) + 101
3 1D2S0T 3 12 + 21 + 03
4 1S*2T*3S 23 11 * 2 * 2 + 23 * 2 + 31
5 1D#2S*3S 5 12 * (-1) * 2 + 21 * 2 + 31
6 1T2D3D# -4 13 + 22 + 32 * (-1)
7 1D2S3T* 59 12 + 21 * 2 + 33 * 2

 

๋ฐ˜์‘ํ˜•