๐ŸŸฃ ์ด์ „ ๋ฌธ์ œ ํšŒ๊ณ 

๊น€ํƒœ์šฑยท2023๋…„ 4์›” 22์ผ
0
post-thumbnail

๐Ÿ˜ญ ๋ฌธ์ œ ํ•ฅ์ง #1

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv0 : ์•”ํ˜ธ ํ•ด๋…

๋ฌธ์ œ ์„ค๋ช…
๊ตฐ ์ „๋žต๊ฐ€ ๋จธ์“ฑ์ด๋Š” ์ „์Ÿ ์ค‘ ์ ๊ตฐ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์•”ํ˜ธ ์ฒด๊ณ„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•„๋ƒˆ์Šต๋‹ˆ๋‹ค.

์•”ํ˜ธํ™”๋œ ๋ฌธ์ž์—ด cipher๋ฅผ ์ฃผ๊ณ ๋ฐ›์Šต๋‹ˆ๋‹ค.
๊ทธ ๋ฌธ์ž์—ด์—์„œ code์˜ ๋ฐฐ์ˆ˜ ๋ฒˆ์งธ ๊ธ€์ž๋งŒ ์ง„์งœ ์•”ํ˜ธ์ž…๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด cipher์™€ ์ •์ˆ˜ code๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ํ•ด๋…๋œ ์•”ํ˜ธ ๋ฌธ์ž์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค cipher์˜ ๊ธธ์ด โ‰ค 1,000
  • 1 โ‰ค code โ‰ค cipher์˜ ๊ธธ์ด
  • cipher๋Š” ์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ณต๋ฐฑ๋„ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

ciphercoderesult
"dfjardstddetckdaccccdegk"4"attack"
"pfqallllabwaoclk"2"fallback"

๋‚˜์˜ ํ’€์ด

function solution(cipher, code) {
  let answer = [];
  // cipher.split("").filter((e, i) => (i + 1) % code === 0);

  for (let i = 0; i < cipher.length; i++) {
    if ((i + 1) % code === 0) answer.push(cipher[i]);
  }

  return answer.join("");
}

์ƒํƒœ ์ ‘๊ทผ
1. for ๋ฐ˜๋ณต์„ ํ†ตํ•ด if ์กฐ๊ฑด์˜ i ๊ฐ’์— 1์„ ๋”ํ•˜์—ฌ ์ธ๋ฑ์Šค ๊ฐ’์˜ ์‹œ์ž‘์„ ๋ณ€๊ฒฝ
2. 1๋ฒˆ์„ ํ†ตํ•ด ๋ฐฐ์—ด์ด ๋˜๋Š” cipher์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋Š” 0์ด ์•„๋‹Œ 1๋ถ€ํ„ฐ ์‹œ์ž‘.
3. ๋งŒ์•ฝ for ์กฐ๊ฑด์˜ i์˜ ๊ฐ’์„ 1๋ถ€ํ„ฐ ์„ค์ •ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๊ทธ๊ฒƒ์€ ๋‹จ์ง€ ์ „์ฒด ์ธ๋ฑ์Šค์—์„œ ์ธ๋ฑ์Šค[1]๋ถ€ํ„ฐ ์ˆœํšŒํ•œ๋‹ค๋Š” ๋œป
4. ๊ทธ๋ ‡๊ธฐ์— if์˜ ์กฐ๊ฑด์— code๋ฅผ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด ๋˜๋Š” ์ž๋ฆฌ์ˆ˜๋ฅผ ๋ถ„๋ช…ํ•˜๊ฒŒ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ.


๐Ÿ˜ญ ๋ฌธ์ œ ํ•ฅ์ง #2

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv0 : ์นด์šดํŠธ ๋‹ค์šด

๋ฌธ์ œ ์„ค๋ช…
์ •์ˆ˜ start์™€ end๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, start์—์„œ end๊นŒ์ง€ 1์”ฉ ๊ฐ์†Œํ•˜๋Š” ์ˆ˜๋“ค์„ ์ฐจ๋ก€๋กœ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 0 โ‰ค end โ‰ค start โ‰ค 50

์ž…์ถœ๋ ฅ ์˜ˆ

startendresult
103[10,9,8,7,6,5,4,3]

๋‚˜์˜ ํ’€์ด

function solution(start, end) {
  var answer = [];

  while (start != end - 1) {
    answer.push(start);
    start--;
  }
  return answer;
}

์ƒํƒœ ์ ‘๊ทผ
1. while์„ ํ†ตํ•ด ํ•ด๋‹น start๊ฐ’์ด end -1๊ฐ€ ๋ ๋•Œ๊นŒ์ง€ ์ˆœํšŒ.


๋‹ค๋ฅธ์‚ฌ๋žŒ ํ’€์ด

function solution(start, end) {
    return Array.from(Array(start - end + 1), (_, i) => start - i);
}

ํ’€์ด
1. Array.from( ) ๋ฉ”์†Œ๋“œ๋ฅผ ์œ ์‚ฌ๋ฐฐ์—ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๋ƒ„.

MDN - Array.from()
Array.from() ๋ฉ”์„œ๋“œ๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด(array-like object)๋‚˜ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(iterable object)๋ฅผ ์–•๊ฒŒ ๋ณต์‚ฌํ•ด ์ƒˆ๋กœ์šดArray ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

๊ตฌ๋ฌธ
Array.from(arrayLike[, mapFn[, thisArg]])

arrayLike
๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ ์ž ํ•˜๋Š”์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด๋‚˜ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด.

mapFnOptional
๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ์— ๋Œ€ํ•ด ํ˜ธ์ถœํ•  ๋งตํ•‘ ํ•จ์ˆ˜.

thisArgOptional
mapFn ์‹คํ–‰ ์‹œ์— this๋กœ ์‚ฌ์šฉํ•  ๊ฐ’.

function solution(start, end) {
  return Array.from({ length: start - end + 1 }, (_, i) => start - i);
}

๋ฐฐ์—ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ๋ฐฐ์—ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Œ.


๐Ÿ˜ญ ๋ฌธ์ œ ํ•ฅ์ง #3

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv0 : ๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด

๋ฌธ์ œ ์„ค๋ช…
๊ฐ€์œ„๋Š” 2 ๋ฐ”์œ„๋Š” 0 ๋ณด๋Š” 5๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด๋ฅผ ๋‚ด๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋‚˜ํƒ€๋‚ธ ๋ฌธ์ž์—ด rsp๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, rsp์— ์ €์žฅ๋œ ๊ฐ€์œ„ ๋ฐ”์œ„ ๋ณด๋ฅผ ๋ชจ๋‘ ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜ํƒ€๋‚ธ ๋ฌธ์ž์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 0 < rsp์˜ ๊ธธ์ด โ‰ค 100
  • rsp์™€ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ return ํ•ฉ๋‹ˆ๋‹ค.
  • rsp๋Š” ์ˆซ์ž 0, 2, 5๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

endresult
"2""0"
"205""052"

๋‚˜์˜ ํ’€์ด

function solution(rsp) {
  var answer = "";
  if (rsp.includes("2")) answer = rsp.replaceAll("2", "0");
  else if (rsp.includes("0")) answer = rsp.replaceAll("0", "5");
  else if (rsp.includes("5")) answer = rsp.replaceAll("5", "2");
  return answer;
}

์ƒํƒœ ์ ‘๊ทผ
1. else if๋ฅผ ํ†ตํ•ด ์—ฐ์†๋œ ์กฐ๊ฑด์— answer ๊ฐ’์„ ์žฌํ• ๋‹น ํ•˜๋ฉด ๊ฐ’์ด ๋‚˜์˜ฌ ์ค„ ์•Ž.
2. ํ•˜์ง€๋งŒ answer๋Š” ํ•ด๋‹น ์กฐ๊ฑด์ด ๋ชจ๋‘ ํ†ต๊ณผํ•œ ๋งˆ์ง€๋ง‰ ๊ฐ’๋งŒ ์ˆ˜์ •ํ•ด์„œ ๋ฐ˜ํ™˜.

๋ฌธ์ œ ์ ‘๊ทผ
1. ์—ฐ์†๋œ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ˜๋ณต์„ ์ฒ˜๋ฆฌํ•˜๋˜๊ฐ€.

function solution(rsp) {
  var answer = "";
  let arr = rsp.split("");
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === "0") answer += "5";
    if (arr[i] === "2") answer += "0";
    if (arr[i] === "5") answer += "2";
  }
  return answer;
}
  1. ์‚ผํ•ญ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ์ด ์žˆ์œผ๋ฉฐ
function solution(rsp) {
    return rsp.split("").map((v) => v==="2" ? 0 : (v==="0" ? 5 : 2)).join("")
}

3.๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๋งคํ•‘ํ•œ ๋‹ค์Œ ๋ฐ˜๋ณต ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

function solution(rsp) {
const mapping = {
"2": "0",
"0": "5",
"5": "2"
};
let answer = "";
for (let i = 0; i < rsp.length; i++) {
const char = rsp[i];
if (char in mapping) {
answer += mapping[char];
} else {
answer += char;
}
}
return answer;
}

์œ„ ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ•ด๋ณด๋ฉด

/*
 ๊ฐ€์œ„๋Š” 2 ๋ฐ”์œ„๋Š” 0 ๋ณด๋Š” 5
*/
function solution(rsp) {
    let arr = {
        2: 0,
        0: 5,
        5: 2
    };
    var answer = [...rsp].map(v => arr[v]).join("");
    return answer;
}

์œ„์™€ ๊ฐ™์ด ๊ฐœ์„  ๊ฐ€๋Šฅํ•จ.


๐Ÿคช ์˜ค๋Š˜์˜ ํ•™์Šต

๊ฐ์ฒด๋ฅผ ํ†ตํ•œ ๊ฐ’์„ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ์ฒด ์‚ฌ์šฉ์˜ ์˜์˜์— ๋Œ€ํ•ด ๋‹ค์‹œ๊ธˆ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ.

profile
๋ฐฉ๋ฒ•์„ ์—ฐ๊ตฌํ•  ์ค„ ์•„๋Š” ๊ฐœ๋ฐœ์ž!

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