๐Ÿ“Œ 1. ๋ฌธ์ œ


[Programmers, Level1] ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ (์ •๋ ฌ)


์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ์ˆ˜๋ฅผ ๋ฝ‘์•„ ๋”ํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


numbers
[2,1,3,4,1]
result
[2,3,4,5,6,7]


โœ ํ’€์ด

function solution(numbers) {
  let answer = [];
  for (let i = 0; i < numbers.length; i++) {
    for (let j = i + 1; j < numbers.length; j++) {
      answer.push(numbers[i] + numbers[j]);
    }
  }
  const arr = [...new Set(answer)];
  const result = arr.sort((a, b) => a - b);
  return result;
}


  • const set = new Set(array) set ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ค‘๋ณต์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค๊ณ , set๊ฐ์ฒด๋กœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ํ›„, Spread Operator, ์ „๊ฐœ์—ฐ์‚ฐ์ž [...set] ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ–ˆ๋‹ค.


๐Ÿ“Œ 2. ๋ฌธ์ œ


[Programmers, Level1] K๋ฒˆ์งธ์ˆ˜


๋ฐฐ์—ด array์˜ i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ณ  ์ •๋ ฌํ–ˆ์„ ๋•Œ, k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.


์˜ˆ๋ฅผ ๋“ค์–ด array๊ฐ€ [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3์ด๋ผ๋ฉด


array์˜ 2๋ฒˆ์งธ๋ถ€ํ„ฐ 5๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฅด๋ฉด [5, 2, 6, 3]์ž…๋‹ˆ๋‹ค. 1์—์„œ ๋‚˜์˜จ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋ฉด [2, 3, 5, 6]์ž…๋‹ˆ๋‹ค. 2์—์„œ ๋‚˜์˜จ ๋ฐฐ์—ด์˜ 3๋ฒˆ์งธ ์ˆซ์ž๋Š” 5์ž…๋‹ˆ๋‹ค.


array
[1, 5, 2, 6, 3, 7, 4]
commands
[[2, 5, 3], [4, 4, 1], [1, 7, 3]]
return
[5, 6, 3]


โœ ํ’€์ด

function solution(array, commands) {
  let answer = [];
  for (let i = 0; i < commands.length; i++) {
    const arr = array.slice(commands[i][0] - 1, commands[i][1]);
    const result = arr.sort((a, b) => a - b);
    answer.push(result[commands[i][2] - 1]);
  }
  return answer;
}


๐Ÿ“Œ 3. ๋ฌธ์ œ


[Programmers, Level1] K๋ฒˆ์งธ์ˆ˜


๋‹จ์–ด s์˜ ๊ฐ€์šด๋ฐ ๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜๋ผ๋ฉด ๊ฐ€์šด๋ฐ ๋‘๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์žฌํ•œ์‚ฌํ•ญ
s๋Š” ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ, 100์ดํ•˜์ธ ์ŠคํŠธ๋ง์ž…๋‹ˆ๋‹ค.


s
"a,b,c,d,e"
return
"c"

s
"a,b,c,d"
return
"b,c"


โœ ํ’€์ด

function solution(s) {
  const arr = s.split("");
  const numbers = arr.length;
  if (numbers % 2) {
    return arr[Math.floor(numbers / 2)];
  } else {
    return arr[numbers / 2 - 1] + arr[numbers / 2];
  }
}


  • ์ „์—๋„ codewars์—์„œ ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฌธ์ œ๋ฅผ ํ‘ผ ์ ์ด ์žˆ๋Š”๋ฐ programmers์—์„œ๋„ ํ’€๊ฒŒ๋˜์—ˆ๋‹ค..! ์ €๋ฒˆ๊ณผ ์ฝ”๋“œ๋ฅผ ๋น„๊ตํ•˜๋‹ˆ ๋˜ ์‚ผํ•ญ์—ฐ์‚ฐ์ž ์•ˆ์“ฐ๊ณ  ๊ตณ์ด ๊ธธ๊ฒŒ ์ผ๋„ค..