๐Ÿ€TIL๐Ÿ€[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Coding Test ์Šคํ„ฐ๋””11

PYMยท2023๋…„ 8์›” 31์ผ
0

๐Ÿ€TIL๐Ÿ€Coding Test

๋ชฉ๋ก ๋ณด๊ธฐ
9/16
post-thumbnail

2018 KAKAO BLIND RECRUITMENT

Q1. [1์ฐจ] ๋‹คํŠธ ๊ฒŒ์ž„

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

  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

๐Ÿฃ๋‚ด ์ฝ”๋“œ

function solution(dartResult) {
    
    let forwardScore = 0;
    let currentScore = 0;
    let totalScore = 0;
    
    for(let i = 0; i < dartResult.length; i++){
        if(dartResult[i] === 'S'){
            currentScore = currentScore * 1 
        }
        else if(dartResult[i] === 'D'){
            currentScore = currentScore ** 2;
        }
        else if(dartResult[i] === 'T'){
            currentScore = currentScore ** 3
        }
        else if(dartResult[i] === '*'){
            currentScore = currentScore * 2 
            forwardScore = forwardScore * 2
        }
        else if(dartResult[i] === '#'){
            currentScore = currentScore * (-1)
        }
        else{
            // cur์ ์ˆ˜ ์ด์ „ ์ ์ˆ˜๋กœ ์˜ฎ๊ธฐ๊ณ  ์ƒˆ ์ ์ˆ˜๋กœ ์—…๋Žƒ 
            if(dartResult[i] === '1' && dartResult[i+1] === '0'){
                totalScore += forwardScore; 
                forwardScore = currentScore;
                currentScore = 10
            }
            else if(dartResult[i] === '0' && dartResult[i-1] === '1'){
                currentScore = 10
            }
            else{
                totalScore += forwardScore; 
                forwardScore = currentScore;
                currentScore = dartResult[i];
            }
        }
        
    }
    
    return totalScore + forwardScore + currentScore; 
}

๐Ÿ’Ÿ๋ณ€์ˆ˜

  • forwardScore : ์ง์ „ ๋ˆ„์  ์ ์ˆ˜
    (์ฆ‰, ์ƒˆ๋กœ์šด ์ˆซ์ž ๊ฐ’์ด ๋“ค์–ด์˜ค๊ธฐ ์ง์ „๊นŒ์ง€์˜ ๊ฐ’)
  • currentScore : ํ˜„์žฌ ์ง„ํ–‰์ค‘์œผ๋กœ ๊ณ„์‚ฐ์ค‘์ธ ์ ์ˆ˜
  • totalScore : ์ตœ์ข… ๋ˆ„์  ์ ์ˆ˜

๐Ÿ’Ÿfor๋ฌธ ( dartResult ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ์ˆœํšŒํ•œ๋‹ค. )

  • dartResult[i]์˜ ๊ฐ’์ด ๋ฌธ์ž์ผ ๊ฒฝ์šฐ (S, D, T, *, #)

    • dartResult[i]์˜ ๊ฐ’์ด S์ผ ๊ฒฝ์šฐ currentScore์˜ ๊ฐ’์— 1์„ ๊ณฑํ•œ๋‹ค.

    • dartResult[i]์˜ ๊ฐ’์ด D์ผ ๊ฒฝ์šฐ currentScore์˜ ๊ฐ’์„ ์ œ๊ณฑํ•œ๋‹ค.

    • dartResult[i]์˜ ๊ฐ’์ด T์ผ ๊ฒฝ์šฐ currentScore์˜ ๊ฐ’์„ ์„ธ์ œ๊ณฑํ•œ๋‹ค.

    • dartResult[i]์˜ ๊ฐ’์ด * (์Šคํƒ€์ƒ)์ผ ๊ฒฝ์šฐ
      currentScore์™€ forwardScore์— 2๋ฅผ ๊ณฑํ•ด์ค€๋‹ค.

    • dartResult[i]์˜ ๊ฐ’์ด # (์•„์ฐจ์ƒ)์ผ ๊ฒฝ์šฐ
      currentScore์— -1์„ ๊ณฑํ•ด์ค€๋‹ค.

  • dartResult[i]์˜ ๊ฐ’์ด ์ˆซ์ž์ผ ๊ฒฝ์šฐ

    ์ˆซ์ž๊ฐ€ ๋“ฑ์žฅํ–ˆ๋‹ค๋Š” ๊ฒƒ์€, ์ž…๋ ฅ ํ˜•์‹์ธ ์ ์ˆ˜|๋ณด๋„ˆ์Šค|[์˜ต์…˜]์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด ํ•œ ์„ธํŠธ๊ฐ€ ๋๋‚˜๊ณ , ์ƒˆ๋กœ์šด ์„ธํŠธ๊ฐ€ ๋“ฑ์žฅํ–ˆ๋‹ค๋Š” ๊ฒƒ! ๋”ฐ๋ผ์„œ

  1. forwardScore๋ฅผ totalScore์— ๋ˆ„์ ์‹œํ‚ค๊ณ ,

  2. currentScore๋ฅผ forwardScore๋กœ ๋ณ€๊ฒฝ์‹œ์ผœ์ฃผ๊ณ ,

  3. ์ƒˆ๋กœ ๋“ฑ์žฅํ•œ ์ˆซ์ž๋ฅผ currentScore๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

    ํ•˜์ง€๋งŒ...!

    ์—ฌ๊ธฐ์„œ ๋ณต์žกํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ๋‹ค. ๋ฐ”๋กœ ์ˆซ์ž๊ฐ€ ์ตœ๋Œ€ ์ ์ˆ˜์ธ 10์ ์ธ ๊ฒฝ์šฐ..!
    ํ•œ ์ž๋ฆฌ ์ˆซ์ž๋งŒ ๊ณ ๋ คํ–ˆ๋”๋‹ˆ, 1์  0์ ์ด ์—ฐ๋‹ฌ์•„ ๋“ค์–ด์˜ค๋Š” ๊ฑธ๋กœ ํŒ๋‹จ๋˜์–ด ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค๐Ÿฅฒ ๊ทธ๋ž˜์„œ ์•„๋ž˜ ๋กœ์ง์„ ์ ์šฉํ–ˆ๋‹ค.

  • ๋งŒ์•ฝ dartResult[i]์˜ ๊ฐ’์ด 1์ด๊ณ  dartResult[i+1]์˜ ๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ ์ฆ‰, ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์˜จ ์ ์ˆ˜ ๊ฐ’์ด 10์ธ ๊ฒฝ์šฐ
    ๋‚˜๋จธ์ง€ 1, 2 ๋กœ์ง์€ ๊ทธ๋Œ€๋กœ๊ณ  3๋กœ์ง์„ currentScore = 10 ๋กœ ์„ค์ •

    ์ด๋ ‡๊ฒŒ๋งŒ ํ•˜๋‹ˆ๊นŒ ๋‹ค์Œ ๊ฐ’์ด 0์ด๋ผ currentScore๊ฐ€ ๋‹ค์‹œ 0์ด ๋˜์–ด๋ฒ„๋ฆผ
    ๊ทธ๋ž˜์„œ dartResult[i] ๊ฐ€ 0์ด๊ณ , dartResult[i-1]๊ฐ€ 1์ธ ๊ฒฝ์šฐ๋„ ๊ณ ๋ คํ•ด์ฃผ์—ˆ๋‹ค.
    ๊ทธ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฏธ 1, 2 ๋กœ์ง์€ ์‹คํ–‰๋œ ํ›„์˜ ๊ฒฝ์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์— (1, 0 ์—์„œ 1์ด ๋“ค์–ด์™”์„ ๋•Œ!) currentScore = 10 ๋งŒ ์—ฐ์‚ฐํ•ด ์ค€๋‹ค.

    ๐Ÿ’Ÿ๋ฐ˜ํ™˜ ๊ฐ’

  • for๋ฌธ์ด ๋๋‚˜๊ณ  ๋งˆ์ง€๋ง‰ forwardScore์™€ currentScore๋Š” totalScore์— ์•„์ง ๋”ํ•ด์ง€์ง€ ์•Š์€ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์—
    totalScore + forwardScore + currentScore์„ ๋ฆฌํ„ดํ•ด ์ค€๋‹ค.

ํ›„๊ธฐ

์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆฌ๊ธด ํ–ˆ์ง€๋งŒ ๊ทธ๋ž˜๋„ ๋‚˜๋งŒ์˜ ์ƒ๊ฐ์œผ๋กœ ํ’€์–ด๋ƒˆ๋‹ค๋Š” ์ ์—์„œ ๋‚˜๋ฆ„... ๋ฟŒ๋“ฏํ–ˆ๋‹ค๐Ÿ˜‚ ๋ธ”๋กœ๊น…ํ•˜๋ฉด์„œ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋‚ด ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์ด๋ ‡๊ฒŒ ๊ธ€๋กœ ํ’€์–ด๋‚ด๋‹ˆ ๋” ํ™•์‹คํ•˜๊ฒŒ ๋กœ์ง์„ ๊ธฐ์–ตํ•  ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™๋‹ค...! ์—ญ์‹œ ๊ธฐ๋ก์€ ์ค‘์š”ํ•ด...!๐Ÿฅน

profile
๋ชฉํ‘œ๋Š” "ํ•จ๊ป˜ ์ผํ•˜๊ณ  ์‹ถ์€, ํ•จ๊ป˜ ์ผํ•ด์„œ ์ข‹์€" Front-end ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€