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

PYMยท2023๋…„ 8์›” 19์ผ
0

๐Ÿ€TIL๐Ÿ€Coding Test

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

Q1.์นด์šดํŠธ ์—…

์ •์ˆ˜ start_num์™€ end_num๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, start_num๋ถ€ํ„ฐ end_num๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์ฐจ๋ก€๋กœ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

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

// ์ฒซ๋ฒˆ์งธ! Array.from()์„ ์‚ฌ์šฉ
function solution(start, end) {
    idx = start;
    return Array.from({length: end-start+1}, ()=> {return start++});
}

//๋‘๋ฒˆ์งธ! Array()์™€ fill() ์‚ฌ์šฉ
function solution(start, end) {
    return Array(end-start+1).fill(start).map((x,idx) => x+idx);
}

๐Ÿฆ–Array() ์‚ฌ์šฉ๋ฒ• ์ •๋ฆฌ

Array() ์ƒ์„ฑ์ž๋Š” ์ƒˆ๋กœ์šด Array ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ

  • ๋งค๊ฐœ๋ณ€์ˆ˜ 2๊ฐ€์ง€

    • elementN : ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์ฑ„์›Œ๋„ฃ์„ ์š”์†Œ. ๋‹จ, ํ•ญ๋ชฉ์ด ํ•˜๋‚˜ ๋ฟ์ด๋ฉฐ ๊ทธ ํ•ญ๋ชฉ์˜ ์ž๋ฃŒํ˜•์ด ์ˆซ์ž์ผ ๊ฒฝ์šฐ ์•„๋ž˜์˜ arrayLength ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

    • arrayLength : Array ์ƒ์„ฑ์ž์— ์ œ๊ณตํ•œ ์œ ์ผํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 0์—์„œ 2^32-1 ์ดํ•˜์˜ ์ •์ˆ˜์ธ ๊ฒฝ์šฐ, length ์†์„ฑ์ด ํ•ด๋‹น ๊ฐ’์ธ ์ƒˆ๋กœ์šด JavaScript ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.

์‚ฌ์šฉ ์˜ˆ์‹œ

// ๋‹จ์ผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฐฐ์—ด ์ƒ์„ฑ์ž ์‚ฌ์šฉ ์˜ˆ์‹œ 
let fruits = new Array(2);

console.log(fruits.length); // 2
console.log(fruits[0]); // undefined

// ๋ณต์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฐฐ์—ด ์ƒ์„ฑ์ž

let fruits = new Array("์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜");

console.log(fruits.length); // 2
console.log(fruits[0]); // "์‚ฌ๊ณผ"

๐Ÿฆ–Array.from() ์‚ฌ์šฉ๋ฒ•

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

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

๋งค๊ฐœ๋ณ€์ˆ˜

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

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

  • thisArgOptional (์˜ต์…”๋„) : mapFn ์‹คํ–‰ ์‹œ์— this๋กœ ์‚ฌ์šฉํ•  ๊ฐ’.

์‚ฌ์šฉ์˜ˆ์‹œ

// string์—์„œ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
console.log(Array.from('foo')); // ["f", "o", "o"]

// from๊ณผ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ
console.log(Array.from([1, 2, 3], (x) => x + x)); // [2, 4, 6]

// Map์—์„œ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ (key, value ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹น ๊ฐ€๋Šฅ) 
const mapper = new Map([
  ["1", "a"],
  ["2", "b"],
]);
Array.from(mapper.values()); // ['a', 'b'];

Array.from(mapper.keys()); // ['1', '2'];

// ๋ฐฐ์—ด ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด(arguments)์—์„œ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
function f() {
  return Array.from(arguments);
}

f(1, 2, 3); // [1, 2, 3]

๐Ÿฆ–fill() ์‚ฌ์šฉ๋ฒ•

fill() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ ์ธ๋ฑ์Šค์˜ ์ด์ „๊นŒ์ง€ ์ •์ ์ธ ๊ฐ’ ํ•˜๋‚˜๋กœ ์ฑ„์›๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜

  • value : ์ฑ„์šธ ๊ฐ’
  • start : ์–ด๋””์„œ ๋ถ€ํ„ฐ ์ฑ„์šธ ์ง€, ์‹œ์ž‘ ์ธ๋ฑ์Šค
  • end : ์–ด๋””๊นŒ์ง€ ์ฑ„์šธ ์ง€, ๋ ์ธ๋ฑ์Šค

์‚ฌ์šฉ์˜ˆ์‹œ

const array1 = [1, 2, 3, 4];

// Fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// Expected output: Array [1, 2, 0, 0]

// Fill with 5 from position 1
console.log(array1.fill(5, 1));
// Expected output: Array [1, 5, 5, 5]

console.log(array1.fill(6));
// Expected output: Array [6, 6, 6, 6]

Q2. ์ ‘๋ฏธ์‚ฌ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

์–ด๋–ค ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ ์ ‘๋ฏธ์‚ฌ๋Š” ํŠน์ • ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "banana"์˜ ๋ชจ๋“  ์ ‘๋ฏธ์‚ฌ๋Š” "banana", "anana", "nana", "ana", "na", "a"์ž…๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด my_string๊ณผ is_suffix๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, is_suffix๊ฐ€ my_string์˜ ์ ‘๋ฏธ์‚ฌ๋ผ๋ฉด 1์„, ์•„๋‹ˆ๋ฉด 0์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

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

function solution(my_string, is_suffix) {
    return my_string.slice(-is_suffix.length) === is_suffix ? 1 : 0
}

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

const solution = (str, suff) => str.endsWith(suff) ? 1 : 0
  • endWith() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค! ์ด ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  endWith()๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฑธ ์ฒ˜์Œ ์•Œ์•„์„œ ์ •๋ฆฌํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

๐Ÿฆ– String.prototype.endsWith()

endsWith() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ true ํ˜น์€ false๋กœ ๋ฐ˜ํ™˜

  • ๋ฌธ๋ฒ• ๊ตฌ๋ฌธ: str.endsWith(searchString[, length])

๋งค๊ฐœ๋ณ€์ˆ˜

  • searchString
    ์ด ๋ฌธ์ž์—ด์˜ ๋์ด ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€๋ฅผ ์ฐพ๊ธฐ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด

  • length (์˜ต์…”๋„)
    ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ’์ด๋ฉฐ, ๊ธฐ๋ณธ๊ฐ’์€ ๋ฌธ์ž์—ด ์ „์ฒด ๊ธธ์ด.
    ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ’์€ ๋ฌธ์ž์—ด ์ „์ฒด ๊ธธ์ด ์•ˆ์—์„œ๋งŒ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.

์‚ฌ์šฉ์˜ˆ์‹œ

var str = "To be, or not to be, that is the question.";

console.log(str.endsWith("question.")); // true
console.log(str.endsWith("to be")); // false
console.log(str.endsWith("to be", 19)); // true

๊ทธ ์ด์™ธ์— ์ƒˆ๋กœ์ด ์•Œ๊ฒŒ๋œ ๊ฒƒ & ๊ธฐ์–ตํ•  ๊ฒƒ

๐Ÿฆ–๋ฌธ์ž๋ฅผ ์ˆซ์žํ˜•์œผ๋กœ

[...num_str].reduce((a, c) => a + +c, 0)

  • a ์™€ b ๊ฐ€ string ํ˜•์‹์ผ ๋•Œ, ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ c ์—๋งŒ + ์—ฐ์‚ฐ์ž๋ฅผ ๋ถ™์—ฌ์ค˜๋„ ์–‘ ์ชฝ ๋ณ€ ๋ชจ๋‘ ์ˆซ์žํ˜•์‹์œผ๋กœ ์ธ์‹

๐Ÿฆ–caret operator(^)

  • JavaScript์—์„œ ์บ๋Ÿฟ(^)์€ ๋น„ํŠธ XOR ์—ฐ์‚ฐ์ž (en-US)์—์„œ ์‚ฌ์šฉ

์ฐธ๊ณ  ์‚ฌ์ดํŠธ

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/Array

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/from

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/fill

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Exponentiation

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

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