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

PYMยท2023๋…„ 9์›” 4์ผ
0

๐Ÿ€TIL๐Ÿ€Coding Test

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

Q1. ์ถ”์–ต ์ ์ˆ˜

์‚ฌ์ง„๋“ค์„ ๋ณด๋ฉฐ ์ถ”์–ต์— ์ –์–ด ์žˆ๋˜ ๋ฃจ๋Š” ์‚ฌ์ง„๋ณ„๋กœ ์ถ”์–ต ์ ์ˆ˜๋ฅผ ๋งค๊ธธ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์ง„ ์†์— ๋‚˜์˜ค๋Š” ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํ•ฉ์‚ฐํ•œ ๊ฐ’์ด ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["may", "kein", "kain"]์ด๊ณ  ๊ฐ ์ธ๋ฌผ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ [5์ , 10์ , 1์ ]์ผ ๋•Œ ํ•ด๋‹น ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 16(5 + 10 + 1)์ ์ด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ์ง„ ์† ์ธ๋ฌผ์˜ ์ด๋ฆ„์ด ["kali", "mari", "don", "tony"]์ด๊ณ  ["kali", "mari", "don"]์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ๊ฐ๊ฐ [11์ , 1์ , 55์ ]]์ด๊ณ , "tony"๋Š” ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๊ฐ€ ์—†์„ ๋•Œ, ์ด ์‚ฌ์ง„์˜ ์ถ”์–ต ์ ์ˆ˜๋Š” 3๋ช…์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ํ•ฉํ•œ 67(11 + 1 + 55)์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ์›Œํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด name, ๊ฐ ์‚ฌ๋žŒ๋ณ„ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜๋ฅผ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด yearning, ๊ฐ ์‚ฌ์ง„์— ์ฐํžŒ ์ธ๋ฌผ์˜ ์ด๋ฆ„์„ ๋‹ด์€ ์ด์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด photo๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์‚ฌ์ง„๋“ค์˜ ์ถ”์–ต ์ ์ˆ˜๋ฅผ photo์— ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 3 โ‰ค name์˜ ๊ธธ์ด = yearning์˜ ๊ธธ์ดโ‰ค 100
  • 3 โ‰ค name์˜ ์›์†Œ์˜ ๊ธธ์ด โ‰ค 7
  • name์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • name์—๋Š” ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • 1 โ‰ค yearning[i] โ‰ค 100
  • yearning[i]๋Š” i๋ฒˆ์งธ ์‚ฌ๋žŒ์˜ ๊ทธ๋ฆฌ์›€ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • 3 โ‰ค photo์˜ ๊ธธ์ด โ‰ค 100
  • 1 โ‰ค photo[i]์˜ ๊ธธ์ด โ‰ค 100
  • 3 โ‰ค photo[i]์˜ ์›์†Œ(๋ฌธ์ž์—ด)์˜ ๊ธธ์ด โ‰ค 7
  • photo[i]์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • photo[i]์˜ ์›์†Œ๋“ค์€ ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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

function solution(name, yearning, photo) {
    
    let answer = [];
    
    for(let i = 0; i < photo.length; i++){
        answer.push(0)
        for(let j = 0; j < photo[i].length; j++){
            if(name.indexOf(photo[i][j]) !== -1){
                answer[i] += yearning[name.indexOf(photo[i][j])]
            }
        }
    }
    
    return answer;
}
  • ์šฐ์„  ์ •๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•  answer ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค.

  • photo๋ฐฐ์—ด์ด ์ด์ฐจ์› ๋ฐฐ์—ด์ธ๋ฐ, ์ง€๊ธˆ ์ œํ•œ ์‚ฌํ•ญ์„ ๋ดค์„ ๋•Œ ์ค‘๋ณต for๋ฌธ์„ ์‚ฌ์šฉํ•ด๋„ ๋ฌธ์ œ์—†์„ ๊ฑฐ ๊ฐ™์•„์„œ ์ค‘๋ณต for๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋Œ์•˜๋‹ค.

    • ์ผ๋‹จ ์ดˆ๊ธฐํ™”๋œ init ๊ฐ’ 0์„ answer์— ๋„ฃ์–ด์ฃผ๊ณ , ๊ทธ answer๊ฐ’์— photo์˜ ํ•œ ํ–‰์˜ ์ ์ˆ˜๋ฅผ ํ•ฉ์‚ฐ์‹œํ‚จ๋‹ค.

    • photo์˜ ๊ฐ ํ–‰์„ ๋‚ด๋ถ€ for๋ฌธ์œผ๋กœ ๋„๋Š”๋ฐ, ๋งŒ์•ฝ name ๋ฐฐ์—ด์— ๊ทธ ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด(์ฆ‰ ์ ์ˆ˜ ๋งค๊ธธ ์ˆ˜ ์žˆ๋‹ค๋ฉด) answer[i]์— yearning[name์—์„œ์˜ ์ธ๋ฑ์Šค] ๊ฐ’์„ ํ•ฉ์‚ฐํ•œ๋‹ค.

๐Ÿ”์ธ์ƒ ๊นŠ์—ˆ๋˜ ์ฝ”๋“œ

function solution(name, yearning, photo) {
    return photo.map((v)=> v.reduce((a, c)=> a += yearning[name.indexOf(c)] ?? 0, 0))
}
  • ๋กœ์ง์€ ๋น„์Šทํ•˜์ง€๋งŒ, map๊ณผ reduce๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ ํ•จ.

  • photo๊ฐ€ ์ด์ฐจ์› ๋ฐฐ์—ด์ด๋ผ๊ณ  ์ค‘๋ณต for๋ฌธ์„ ๋Œ์ง€ ์•Š๊ณ , map์•ˆ์—์„œ reduce๋ฅผ ๋Œ๋ ธ๋‹ค. (๊ทผ๋ฐ ๊ฒฐ๊ตญ ์ด ๋‘ ๋ฉ”์„œ๋“œ๋„ ๋น„์Šทํ•œ ์‹œ๊ฐ„ ๋ณต์žก๋„ ์ผ ๊ฑฐ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ ...)

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

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