๐Ÿ’ž[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฐฐ์—ด ํšŒ์ „์‹œํ‚ค๊ธฐ

Chobbyยท2022๋…„ 10์›” 13์ผ
1

Programmers

๋ชฉ๋ก ๋ณด๊ธฐ
103/349
post-custom-banner

๐Ÿงก๋ฌธ์ œ ์„ค๋ช…

์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด numbers์™€ ๋ฌธ์ž์—ด direction๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฐฐ์—ด numbers์˜ ์›์†Œ๋ฅผ direction๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ์”ฉ ํšŒ์ „์‹œํ‚จ ๋ฐฐ์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


๐Ÿ’›์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค numbers์˜ ๊ธธ์ด โ‰ค 100
  • direction์€ "left" ์™€ "right" ๋‘˜ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

๐Ÿ’š์ž…์ถœ๋ ฅ ์˜ˆ

numbersdirectionresult
[1, 2, 3]"right"[3, 1, 2]
[4, 455, 6, 4, -1, 45, 6]"left"[455, 6, 4, -1, 45, 6, 4]

๐Ÿ’™์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

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

  • numbers ๊ฐ€ [1, 2, 3]์ด๊ณ  direction์ด "right" ์ด๋ฏ€๋กœ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ์”ฉ ํšŒ์ „์‹œํ‚จ [3, 1, 2]๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

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

  • numbers ๊ฐ€ [4, 455, 6, 4, -1, 45, 6]์ด๊ณ  direction์ด "left" ์ด๋ฏ€๋กœ ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ์”ฉ ํšŒ์ „์‹œํ‚จ [455, 6, 4, -1, 45, 6, 4]๋ฅผ returnํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’œ๋‚˜์˜ ํ’€์ด

function solution(numbers, direction) {
    if(direction === 'right') {
        numbers.unshift(numbers.pop())
    } else {
        numbers.push(numbers.shift())
    }
    return numbers
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ
post-custom-banner

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