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

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

๐Ÿ€TIL๐Ÿ€Coding Test

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

Q1. ์Œ์–‘ ๋”ํ•˜๊ธฐ

์–ด๋–ค ์ •์ˆ˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ •์ˆ˜๋“ค์˜ ์ ˆ๋Œ“๊ฐ’์„ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด absolutes์™€ ์ด ์ •์ˆ˜๋“ค์˜ ๋ถ€ํ˜ธ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ด์€ ๋ถˆ๋ฆฌ์–ธ ๋ฐฐ์—ด signs๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์‹ค์ œ ์ •์ˆ˜๋“ค์˜ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

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

function solution(absolutes, signs) {
    let realArr = [];
    
    for(let i = 0; i < absolutes.length; i++){
        if(signs[i]){
            realArr.push(absolutes[i])
        }
        else{
            realArr.push(absolutes[i] * -1)
        }
    }
    
    return realArr.reduce((acc, cur) => acc + cur); 
}

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

function solution(absolutes, signs) {

    return absolutes.reduce((acc, val, i) => acc + (val * (signs[i] ? 1 : -1)), 0);
}
  • ์ฒ˜์Œ์— reduce๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•˜๊ธด ํ–ˆ๋Š”๋ฐ, reduce ์˜ ์ธ์ž๋กœ acc, cur ๋ง๊ณ ๋„ index ๋ฒˆํ˜ธ๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ธด๊ฐ€๋ฏผ๊ฐ€ํ•ด์„œ ์‹œ๋„ํ•˜์ง€ ๋ชป ํ–ˆ๋‹ค ใ…œใ…œ

  • ์œ„ ์ฝ”๋“œ์—์„œ reduce์˜ ๋งˆ์ง€๋ง‰ 0์ด ์˜๋ฏธํ•˜๋Š” ๊ฒƒ

    • initialValue : acc์˜ ์ดˆ๊ธฐ๊ฐ’ (optional)
  • ์ฆ‰ ์œ„ ์ฝ”๋“œ๋Š” signs[i]์˜ ๊ฐ’์ด true๋ฉด val(cur)์— 1์„ ๊ณฑํ•˜๊ณ  false๋ฉด -1์„ ๊ณฑํ•ด์„œ(์ฆ‰ ์Œ์ˆ˜๋กœ ๋งŒ๋“ค์–ด์„œ) acc์— ๋”ํ•ด์ฃผ๋Š” ๊ฒƒ

Q2. ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ชจ๋ฐ”์ผ์€ ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ๊ณ ์ง€์„œ๋ฅผ ๋ณด๋‚ผ ๋•Œ ๊ณ ๊ฐ๋“ค์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค.
์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด phone_number๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋’ท 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ ์ „๋ถ€ *์œผ๋กœ ๊ฐ€๋ฆฐ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด

  • phone_number๋Š” ๊ธธ์ด 4 ์ด์ƒ, 20์ดํ•˜์ธ ๋ฌธ์ž์—ด

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

function solution(phone_number) {
    let answer = ""
    let secretNum = phone_number.slice(0, -4).length
    
    for(let i = 0; i < secretNum; i++){
        answer += "*"
    }
    
    return answer + phone_number.slice(-4) ; 
}

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

//์ •๊ทœ์‹ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ 
function hide_numbers(s){
  return s.replace(/\d(?=\d{4})/g, "*");
}

// repeat๊ณผ slice๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ 
function hide_numbers(s){
    var result = "*".repeat(s.length - 4) + s.slice(-4);
    return result;
}
  • ์ •๊ทœ์‹์„ ์ž˜ ์“ฐ๋ฉด ๊ฐ„๊ฒฐํ•ด์ง€๋Š”๊ตฌ๋‚˜!!!...

  • ํ•˜์ง€๋งŒ replace๋กœ ์ธํ•ด ์‹คํ–‰ ์‹œ ์†๋„๊ฐ€ repeat, slice๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ณด๋‹ค 3๋ฐฐ ์ •๋„ ๋Š๋ฆฌ๋‹ค๋Š” ๋Œ“๊ธ€๋“ค๋„ ๋ณด์˜€๋‹ค.

    • ์ •๊ทœ์‹์˜ ๊ฒฝ์šฐ, ๋ฌธ์ž์—ด์˜ ํŒจํ„ด์ด๋‚˜ ์กฐ๊ฑด์ด ๋ณต์žกํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.
      ์ž…๋ ฅ๋ฐ›๋Š” ๋ฌธ์ž์—ด์ด ๋‹จ์ˆœํ•˜๋ฉด ์˜คํžˆ๋ ค slice, repeat์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋” ๋ณด๊ธฐ์—๋„ ์ง๊ด€์ ์ผ ์ˆ˜๋„ ์žˆ๋‹ค๊ณ !
  • repeat์˜ ์กด์žฌ ๊ธฐ์–ตํ•˜๊ธฐ!!

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

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