2024.07.17 - [๐ค data structures & algorithms] - Data Structures & Algorithms with Javascript
Data Structures & Algorithms with Javascript : Intro
๐ฉ๐ป๐ป Data Structures & Algorithms with Javascript๋ฅผ ์ถ์ฒ๋ฐ์์ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋ค์์ ์๋ฃ ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด ์ดํด๋ณด๊ณ ํด๋น ๋ด์ฉ์ ๋ํด ์ ๋ฆฌํด๋ณด๊ณ ์ ํ๋ค.์์ผ๋ก์ ํฌ์คํ ์ด ๋ค๋ฃฐ ๋ด์ฉ
pyotato-dev.tistory.com
์ปดํจํฐ ํ๋ก๊ทธ๋๋ฐ์์ ๊ฐ์ฅ ์ต์ํ ์๋ฃ๊ตฌ์กฐ๋ผ๋ฉด "Array"๋ผ๊ณ ํ ์ ์๋ค.
๋ชจ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ ์ด๋ค ํํ๋ก๋ array๋ฅผ ๋ด์ฅํ๊ณ ์๋ค.
Array๊ฐ built-in์ด๊ธฐ ๋๋ฌธ์ ํจ์จ์ ์ด๊ณ ๋ฐ์ดํฐ ์ ์ฅ ๋ชฉ์ ์ ์ข์ ์ ํ์ผ๋ก ์ฌ๊ฒจ์ง๋ค.
๐ ์ค๋์ ์๋ฐ์คํฌ๋ฆฝํธ์์ array๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ์์ ์ด๋ป๊ฒ ์ด๋ค ๊ฒฝ์ฐ๋ค์ ์ฌ์ฉํ ์ง์ ๋ํด ์์๋ณด์.
Array ์ฌ์ฉ
์๋ฐ์คํฌ๋ฆฝํธ Array๋ฅผ ์ฌ์ฉํด ๋ดค๋ค๋ฉด ์์ฒญ ์ ์ฐํ๋ค๋ ๊ฑธ ๋๊ผ์ ๊ฒ์ด๋ค.
Array๋ฅผ ์์ฑ, ์์๋ฅผ ์ฐพ๊ฑฐ๋ sort ํ๋ ๋ฐฉ๋ฒ์๋ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์๋ค. ์ด ๋ฐฉ๋ฒ๋ค์ ๋ํด ํ๋์ฉ ์์๋ณด์.
Array ์์ฑ
// ๊ฐ์ฅ ๊ฐ๋จํ๊ฒ array ์ ์ธํ๋ ๋ฐฉ๋ฒ์ '[ ]' ์ฐ์ฐ์๋ฅผ ํ์ฉํ๋ ๊ฒ์ด๋ค.
const emptyArray = [];
// ์๋ฐ์คํฌ๋ฆฝํธ array์ ๋ด์ฅ๋ length ํ๋กํผํฐ๋ฅผ ํธ์ถํด์ ๊ธธ์ด๊ฐ 0 ์ธ ๋ฐฐ์ด์ด๋ผ๋ ๊ฑธ ์ ์ ์๋ค.
console.log(emptyArray.length);
// ๋ฐฐ์ด์ ์์๋ฅผ ๋ฃ์ด ๋ฐฐ์ด์ ์ ์ธํ ์ ์๋ค.
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.length); // 5
// Array constructor์ new ํค์๋๋ก ํธ์ถํ์ฌ array๋ฅผ ์์ฑํ ์ ์๋ค.
// ์ซ์ 1๊ฐ๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ๋ฉด ํด๋น ์ซ์๋งํผ์ ๋น ๋ฐฐ์ด์ ์์ฑํ๋ค.
// ์ด๋ empty๋ ๋น์ด ์๋ค๋ ๊ฒ์ด๊ณ , ์ค์ empty๋ผ๋ ๊ฐ์ด ๋ค์ด๊ฐ ์๋ ๊ฑด ์๋๋ค.
const empty10Arr = new Array(10);
console.log(empty10Arr); // [empty × 10]
// 2๊ฐ ์ด์์ ์ซ์๋ฅผ ์ ๋ฌํ ๊ฒฝ์ฐ์๋ ํด๋น ์ซ์๋ค์ ์์๋ก ํ๋ ๋ฐฐ์ด์ ์์ฑํ๋ค.
const numbers3 = new Array(1, 2, 3, 4, 5);
console.log(numbers3.length); // 5
// (์คํฌ๋ฆฝํธ ์ธ์ด๋ค ์ ์ธ) ๋ค๋ฅธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์๋ ๋ฌ๋ฆฌ ์๋ฐ์คํฌ๋ฆฝํธ array์ ์์๋ค์ ๋ค๋ฅธ ํ์
์ ๊ฐ์ง ์ ์๋ค.
const diffTypeArr = new Array(1, "Joe", true, null);
๋ค์๊ณผ ๊ฐ์ด Array.isArray()์ ๋งค๊ฐ๋ณ์๋ก ๋๊ฒจ ํ์ธํ ์ ์๋ค.
const arr = [1,2,3];
const obj = {"0":1,"1":2,"2":3};
Array.isArray(arr); // true
Array.isArray(obj); // false
Array constructor๋ฅผ ํธ์ถํ๋ ๊ฒ๋ณด๋ค๋ [ ] ์ฐ์ฐ์๋ฅผ ํ์ฉํ๋ ๊ฒ์ด ๊ฐ์ฅ ํจ์จ์ ์ด๋ผ๊ณ ํ๋ค.
Array ์์ ์ฝ์ ๊ณผ ์ ๊ทผ
// [๋ฐฐ์ด์ ์ธ๋ฑ์ค] = ๋ฐฐ์ด ์ธ๋ฑ์ค์ ํ ๋นํ ๊ฐ ํํ๋ก [] ์ฐ์ฐ์๋ฅผ ํ์ฉํด ๊ฐ์ ํ ๋นํ ์ ์๋ค.
const nums = [];
for(let i = 0; i < 100; i++){
nums[i] = i+1;
}
// ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ก ์์์ ์ ๊ทผ ๊ฐ๋ฅํ๋ค
const nums2 = [1,2,3,5,8,13,21];
let sum = 0;
for(let i = 0; i < numbers.length; i++){
sum += nums2[i];
}
console.log(sum); //53
String์ผ๋ก๋ถํฐ ๋ฐฐ์ด ์์ฑ
string์ ์๋ผ์ค ๊ธฐ์ค ๋ฌธ์์ด์ split()์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ์ฌ ๋ฐฐ์ด์ ์์ฑํ ์ ์๋ค.
const sentence = "the quick brown fox jumped over the lazy dog";
const words = sentence.split(" ");
for (let i = 0; i < words.length; ++i) {
console.log("word " + i + ": " + words[i]);
}
/**
word 0: the
word 1: quick
word 2: brown
word 3: fox
word 4: jumped
word 5: over
word 6: the
word 7: lazy
word 8: dog
*/
๋ฐฐ์ด ํฉ์ณ์ฃผ๊ธฐ
์์ ๋ณต์ฌ
// ์์ ๋ณต์ฌ : ๋ฐฐ์ด์ ๊ฐ์ฒด๋ค. ๊ฐ์ฒด๋ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์
// ์๋ณธ ๋ฐฐ์ด์ ๋ค๋ฅธ ๋ฐฐ์ด์ ํ ๋นํ๋ฉด ๋ณต์ฌ๋ณธ์ธ ๋ฐฐ์ด์ ์๋ณธ ๋ฐฐ์ด์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ์ฌ (์๋ก ๊ฐ์ ๊ฑธ ์ฐธ์กฐ)
// ๋ณต์ฌ๋ณธ์ ๋ณ๊ฒฝ์ฌํญ์ด ์๋ณธ ๋ฐฐ์ด์๋ ๋ฐ์๋๋ค (์๋ก ๋ณ๊ฒฝ ์ฌํญ์ด ๋ฐ์).
const nums = [];
for (var i = 0; i < 10; ++i) {
nums[i] = i+1;
}
const samenums = nums;
nums[0] = 400;
samenums[0];
๊น์ ๋ณต์ฌ
// ์์ ๋ณต์ฌ๋ฅผ ํ๋ฉด ๊ฐ์ด ์ด๋์ ์๋์น ์๊ฒ ๋ณ๊ฒฝ๋ ์๋ ์๋ค.
// ๊น์ ๋ณต์ฌ๋ก ํด๋น ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
// ๋ณต์ฌ ํจ์
function copy(arr1, arr2) {
for (let i = 0; i < arr1.length; ++i) {
arr2[i] = arr1[i];
}
}
// ์ฌ์ฉ
const nums = [];
for (let i = 0; i < 100; ++i) {
nums[i] = i+1;
}
const samenums = [];
copy(nums, samenums);
nums[0] = 400;
console.log(samenums[0]); // 1
Array ์ ๊ทผ ํจ์
์์์ array์ ์ธ๋ฑ์ค๋ฅผ [] ์ฐ์ฐ์๋ก ์ ๊ทผํ ์ ์๋ค๋ ๊ฑธ ์ดํด๋ดค๋ค.
์ด ์ธ์ array ์์์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์๋ ์ ๊ทผ์ ํจ์๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋๋ฐ, ์ ๊ทผ์ ํจ์๋ค์ ์ดํด๋ณด์.
indexOf() : array์ ์์๊ฐ ๋ฑ์ฅํ์ง ์์ผ๋ฉด -1, ๋ฑ์ฅํ๋ฉด ๊ฐ์ฅ ๋ง์ง๋ง ๋ฑ์ฅ ์ธ๋ฑ์ค
const names = ["David", "Cynthia", "Raymond", "Clayton", "Jennifer"];
const present_name = 'Cynthia';
const non_present_name = 'Elsa';
function getPosition(name){
const position = names.indexOf(name);
if (position >= 0) {
console.log("Found " + name + " at position " + position);
}
else {
console.log(name + " not found in array.");
}
}
getPosition(present_name); // Found Cynthia at position 1
getPosition(non_present_name); // Elsa not found in array.
Array๋ฅผ String ํํ๋ก
join() : ๋งค๊ฐ๋ณ์๋ก ๋๊ธด ๋ฌธ์์ด๋ก array ์์๋ค์ ํฉ์ณ์ค string ์์ฑ, ๋งค๊ฐ๋ณ์๋ฅผ ๋๊ธฐ์ง ์๋ค๋ฉด ', '๋ก ํฉ์ณ์ค๋ค.
toString() : ', '๋ก array ์์๋ค์ ํฉ์ณ์ค string ์์ฑ
const names = ["David", "Cynthia", "Raymond", "Clayton", "Mike", "Jennifer"];
names.join(); // 'David,Cynthia,Raymond,Clayton,Mike,Jennifer'
names.toString(); // 'David,Cynthia,Raymond,Clayton,Mike,Jennifer'
Array๋ค๋ก๋ถํฐ ์๋ก์ด Array ์์ฑ
concat() : ๋ ๊ฐ ์ด์์ array๋ฅผ ์ด์ด ๋ถ์ผ ์ ์๋ค. ๋ฐฐ์ด. concat(์ด์ด ๋ถ์ฌ์ค ๋ฐฐ์ด) ํํ
const lowercase = ["a", "b", "c", "d", "e"];
const uppercase = ["A", "B", "C","D","E"];
const lower_upper = lowercase.concat(uppercase);
const upper_lower = uppercase.concat(uppercase);
console.log(lower_upper) // ['a', 'b', 'c', 'd', 'e', 'A', 'B', 'C', 'D', 'E']
console.log(upper_lower) // ['A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E']
splice() : ๋งค๊ฐ๋ณ์๋ ์ฐจ๋ก๋๋ก ์์ ์ธ๋ฑ์ค, ์ญ์ ํด ์ค ํญ๋ชฉ ๊ฐ์, ์ถ๊ฐํด ์ค ์์๋ค ํํ.
์๋ณธ array์ ๋ณ๊ฒฝํ๊ธฐ ๋๋ฌธ์ ๋ง์ฝ์ ๋ณ๊ฒฝ์์ด ์๋ก์ด array๋ฅผ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด slice(์์์ธ๋ฑ์ค, ๋ ์ธ๋ฑ์ค)๋ฅผ ํ์ฉํ์.
const lowercase = ["a", "b", "c", "d", "e","f","g","h"];
// lowercase์ ์ธ๋ฑ์ค 3๋ฒ๋ถํฐ 3๊ฐ๋ฅผ ์๋ผ์ ๋ฐฐ์ด์ ์์ฑ
const spliced = lowercase.splice(3,3);
console.log(spliced); // ["d", "e","f"]
console.log(lowercase); // ["a", "b", "c", "g", "h"]
// ์์ ์ธ๋ฑ์ค 3์์๋ถํฐ 0๊ฐ๋ฅผ ์ญ์ ํ๊ณ ์คํ๋ ๋ ๋ฌธ๋ฒ(...)์ ํ์ฉํ์ฌ spliced์ ์์๋ค์ ์ฝ์
lowercase.splice(3,0,...spliced);
console.log(lowercase); // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Array mutator ํจ์
push() : array ๋์ ์์ ์ถ๊ฐ
const nums = [1, 2, 3, 4, 5];
console.log(nums); // 1, 2, 3, 4, 5
nums.push(6);
console.log(nums); // 1, 2, 3, 4, 5, 6
unshift() : array ์์ ์์ ์ถ๊ฐ, ๋งค๊ฐ๋ณ์๋ฅผ ','๋ก ๊ตฌ๋ถํ์ฌ ์ฌ๋ฌ ๊ฐ ์ถ๊ฐ ๊ฐ๋ฅ
// ์ง์ ๋ฐฐ์ด์ ์์๋ฅผ ํ๋์ฉ ๋ฐ์ด์ฃผ๊ณ ๋งจ ์์ ์์ ์ถ๊ฐ
const nums = [2,3,4,5];
const newnum = 1;
const N = nums.length;
for (let i = N; i >= 0; --i) {
nums[i] = nums[i-1];
}
nums[0] = newnum;
console.log(nums); // [1, 2, 3, 4, 5]
// unshift๋ฅผ ํตํด ๊ฐ๋จํ๊ฒ ์์ ์์๋ฅผ ์ถ๊ฐํ ์ ์๋ค
const newnum = 1;
let nums = [2,3,4,5];
print(nums); // [2,3,4,5]
nums.unshift(newnum);
console.log(nums); // [1,2,3,4,5]
nums = [3,4,5];
nums.unshift(newnum,1,2);
console.log(nums); // [1, 1, 2, 3, 4, 5]
Array ์์ ์ ๊ฑฐ
pop() : array ๋งจ ๋ ์์ ์ ๊ฑฐ
const nums = [1,2,3,4,5];
nums.pop(); // 5
console.log(nums); // [1,2,3,4]
shift() : array ๋งจ ์ฒซ ์์ ์ ๊ฑฐ
const nums = [1,2,3,4,5];
// ์ง์ ๋งจ ์์ ์์ ์ถ๊ฐ
const nums = [9,1,2,3,4,5];
console.log(nums); // [9,1,2,3,4,5]
for (let i = 0; i < nums.length; ++i) {
nums[i] = nums[i+1];
}
console.log(nums); // [1, 2, 3, 4, 5, undefined] // ๋ฐฐ์ด์ ํฌ๊ธฐ๋ ๊ทธ๋๋ก๋ผ์ ๋ฐ์ชฝ์ง๋ฆฌ ์ ๊ฑฐ๋ค
// shift() ํจ์๋ฅผ ํ์ฉํด ๋งจ ์์ ์์ ์ถ๊ฐ
nums.shift(); // 9
console.log(nums); // [1,2,3,4,5]
Array ์์ ์์๋๋ก ์ ๋ ฌ
reverse() : ์์๋ค์ ๋ด๋ฆผ์ฐจ์(ํฐ ๊ฐ๋ถํฐ ์์ ๊ฐ ์)์ผ๋ก ์ ๋ ฌ
const nums = [1,2,3,4,5];
nums.reverse();
console.log(nums); // [5, 4, 3, 2, 1]
sort() : ์ฌ์ ์ ์ผ๋ก ์์๋ค์ ์ ๋ ฌ
// sort() ํจ์๋ ์์๋ค์ด ๋ฌธ์์ด์ด๋ผ๊ณ ๊ฐ์ ํ๊ณ ์ ๋ ฌํ๋ค.
// ์ฆ 100์ ์ซ์ 100์ด ์๋๋ผ ๋ฌธ์ "1","0","0"์ด ์ด์ด์ง ๊ฐ์ด๊ณ , 2๋ ์ซ์๊ฐ ์๋ ๋ฌธ์ "2"์ด๋ค.
// ๋ฐ๋ผ์ ์ซ์ ์์ 1,2,3,4,100,200 ์ด ์๋๋ผ
// ๋ฌธ์ ์์ "1", "100", "2" , "200", "3", "4"๋ก ์ ๋ ฌํ๋ค.
const nums = [3, 1, 2 , 100, 4, 200];
nums.sort();
console.log(nums); // [1, 100, 2, 200, 3, 4]
// sort()ํจ์์ ๋งค๊ฐ๋ณ์๋ก ๋น๊ต ํจ์๋ฅผ ์ ๋ฌํ์ฌ ์ซ์์์ผ๋ก ์ ๋ ฌํ๋๋ก ํ ์ ์๋ค.
function compare(num1, num2) {
return num1 - num2;
}
nums.sort(compare);
console.log(nums); // [1, 2, 3, 4, 100, 200]
Iterator ํจ์
iteratorํจ์๋ ๋ฐฐ์ด ์์๋ค์ ์ํํ๋ฉด์ ์ฐ์ฐ์ ํ๊ณ ๋์ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ๋ ๊ฒ๊ณผ, ์์ฑํ์ง ์๋ ๊ฒ์ด ์๋ค.
๋จผ์ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํ์ง ์๋ iterator ํจ์๋ฅผ ์ดํด๋ณด์.
forEach() : ๋งค๊ฐ๋ณ์๋ก ํจ์๋ฅผ ์ ๋ฌํ๋ฉด ํด๋น ํจ์์ ์ฐ์ฐ์ array์ ๊ฐ ์์์ ์ ์ฉ
function square(num) {
console.log(num, num * num);
}
const nums = [1,2,3,4,5,6,7,8,9,10];
nums.forEach(square);
/**
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81 10 100
*/
every() : ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ boolean ํจ์๋ฅผ array์ ๊ฐ ์์์ ์ ์ฉํ๊ณ ๋ชจ๋ ์์๊ฐ true์ธ ๊ฒฝ์ฐ true๋ฅผ ๋ฆฌํดํ๋ค.
function isEven(num) {
return num % 2 == 0;
}
const num1 = [2,4,6,8,10];
const num2 = [2,4,6,7,8,10];
const allAreEven = (nums) => nums.every(isEven)? "all numbers are even":"not all numbers are even";
console.log(allAreEven(num1)); // all numbers are even
console.log(allAreEven(num2)); // not all numbers are even
some() : ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ boolean ํจ์๋ฅผ array์ ๊ฐ ์์์ ์ ์ฉํ๊ณ ํ๋๋ผ๋ ์์๊ฐ true์ธ ๊ฒฝ์ฐ true๋ฅผ ๋ฆฌํดํ๋ค.
function isEven(num) {
return num % 2 == 0;
}
const num1 = [1,3,5,7,8,9];
const num2 = [1,3,5,7,9];
const someAreEven = (nums) => nums.some(isEven)? "some numbers are even":"all numbers are not even";
someAreEven(num2) // 'some numbers are even'
someAreEven(num2) // 'all numbers are not even'
reduce() : ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ํจ์์ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถ์ ํด์ ํ๋์ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ค.
// reduce๋ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ฐ์ฐ์ด ์งํ๋๋ค.
function add(accumulatedValue, currentValue) {
return accumulatedValue + currentValue;
}
const nums = [1,2,3,4,5,6,7,8,9,10];
const sum = nums.reduce(add);
console.log(sum); // 55
// ๋ฐ๋ฉด reduceRight()๋ก ์ค๋ฅธ์ชฝ์์ ์ผ์ชฝ์ผ๋ก ์ฐ์ฐ์ด ๋๋๋ก ํ ์ ์๋ค.
function concat(accumulatedString, item) {
return accumulatedString + item;
}
const words = ["the ", "quick ","brown ", "fox "];
const sentence = words.reduceRight(concat);
console.log(sentence); // 'fox brown quick the '
์ด๋ฒ์๋ ์๋ก์ด array๋ฅผ ๋ฆฌํดํ๋ ํจ์๋ค์ ์ดํด๋ณด์.
map() : forEach() ํจ์์ฒ๋ผ array๋ฅผ ์ํํ๋ฉด์ ๊ฐ ์์์ ์ฐ์ฐ์ ํ๋ค. forEach()์ ๋ฌ๋ฆฌ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ด์ ์๋ก์ด array ์์ฑํ์ฌ ๋ฆฌํด
function curve(grade) {
return grade += 5;
}
const grades = [77, 65, 81, 92, 83];
const newgrades = grades.map(curve);
console.log(newgrades); // [82, 70, 86, 97, 88]
filter() : array๋ฅผ ์ํํ๋ฉด์ ๊ฐ ์์์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ฐ์ boolean ํจ์๋ฅผ ์ํํ์ฌ true์ธ ์์ ๊ฐ๋ง ๋ฆฌํดํ๋ ์๋ก์ด ๋ฐฐ์ด ์์ฑ
function isEven(num) {
return num % 2 == 0;
}
function isOdd(num) {
return num % 2 != 0;
}
const nums = [];
for (let i = 0; i < 20; ++i) {
nums[i] = i+1;
}
const evens = nums.filter(isEven);
const odds = nums.filter(isOdd);
console.log(`Even numbers: ${evens}`); // 'Even numbers: 2,4,6,8,10,12,14,16,18,20'
console.log(`Odd numbers: ${odds}`); // 'Odd numbers: 1,3,5,7,9,11,13,15,17,19'
์ด์ฐจ์/ ๋ค์ฐจ์ Array
์๋ฐ์คํฌ๋ฆฝํธ array๋ 1์ฐจ ๋ฐฐ์ด์ด๋ค. ํ์ง๋ง ๋ฐฐ์ด์ ์ค์ฒฉํ์ฌ ๋ค์ฐจ์ ๋ฐฐ์ด์ ์์ฑํ ์ ์๋ค.
์ด์ฐจ์ Array ์์ฑ
// array๋ฅผ ์์ฑํ๊ณ ๋ด๋ถ์ 5๊ฐ์ ๋น ๋ฐฐ์ด์ ์ฝ์
var twod = [];
var rows = 5;
for (var i = 0; i < rows; ++i) {
twod[i] = [];
}
console.log(twod) // (5) [Array(0), Array(0), Array(0), Array(0), Array(0)]
// ์ง์ ์์ฑํจ์ ๊ตฌํ
Array.matrix = function(numrows, numcols, initial) {
const arr = [];
for (var i = 0; i < numrows; ++i) {
let columns = [];
for (let j = 0; j < numcols; ++j) {
columns[j] = initial;
}
arr[i] = columns;
}
return arr;
}
console.log(Array.matrix(5,5,0));
/**
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
*/
// ๋ค์ด๊ฐ ์์ ๊ฐ์ ์๋ค๋ฉด ์ง์ ๊ฐ์ ๋ฃ์ ๋ฐฐ์ด๋ก ์์ฑ
let grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
console.log(grades[2][2]); // 89
// Array.from() ํ์ฉ
let arr = Array.from({length:5},()=>Array(5).fill(0));
/**
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
*/
์ด์ฐจ์ Array ๋ค๋ฃจ๊ธฐ
์ด์ฐจ์ array๋ ์์๋ฅผ (ํ)row ๋๋ (๋ ฌ) column ์์ฃผ๋ก ์ ๊ทผํ๋ ํจํด์ด ์๋ค.
๋ ํจํด์ ๋ชจ๋ ์ค์ฒฉ for๋ฌธ์ ์ฌ์ฉํด ์ดํด๋ณผ ๊ฒ์ด๋ค.
column์ค์ฌ์ ์ฒ๋ฆฌ๋ฅผ ์ํด์ ์ธ๋ถ ๋ฃจํ๋ row๋ฅผ ์ํํ๊ณ , ๋ด๋ถ ๋ฃจํ๋ column์ ์ฒ๋ฆฌํ๋ค.
๊ฐ ํ(row)์ด ํ ํ์์ ์ฑ์ ์ด๊ณ ๊ฐ cell์ด ์ํ ์ ์๋ผ๊ณ ํ ๋, ๊ฐ๊ฐ์ ํ์๋ค์ ํ๊ท ์ฑ์ ์ ๊ตฌํ๋ ๋ค์ ์์ ๋ฅผ ์ดํด๋ณด์.
const grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
// ๊ฐ ํ์์ ํ๊ท grad์ ๊ตฌํ๊ธฐ ์ํด
// ๊ฐ row์ ์์๋ค์ ๋ํ ๋ค
// row์ length๋งํผ ๋๋ ์ฃผ๋ฉด ๊ตฌํ ์ ์๋ค.
let total = 0;
let average = 0.0;
for (let row = 0; row < grades.length; ++row) {
for (let col = 0; col < grades[row].length; ++col) {
total += grades[row][col];
}
average = total / grades[row].length;
console.log("Student " + parseInt(row+1) + " average: " +
average.toFixed(2));
total = 0;
average = 0.0;
}
/**
'Student 1 average: 81.33'
'Student 2 average: 79.67'
'Student 3 average: 91.33'
*/
row์ค์ฌ์ ์ฒ๋ฆฌ๋ฅผ ์ํด์ ์ธ๋ถ ๋ฃจํ๋ column์ ์ํํ๊ณ , ๋ด๋ถ ๋ฃจํ๋ row๋ฅผ ์ฒ๋ฆฌํ๋ค.
์ด๋ฒ์๋ ๊ฐ ์ํ ๋น ํ์๋ค์ ํ๊ท ์ ๊ตฌํ๋ ๋ฒ์ ์ดํด๋ณด์.
const grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
// ๊ฐ ํ์์ ํ๊ท grad์ ๊ตฌํ๊ธฐ ์ํด
// ๊ฐ row์ ์์๋ค์ ๋ํ ๋ค
// row์ length๋งํผ ๋๋ ์ฃผ๋ฉด ๊ตฌํ ์ ์๋ค.
let total = 0;
let average = 0.0;
for (let col = 0; col < grades.length; ++col) {
for (let row = 0; row < grades[col].length; ++row) {
total += grades[row][col];
}
average = total / grades[col].length;
console.log("Test " + parseInt(col+1) + " average: " +
average.toFixed(2));
total = 0;
average = 0.0;
}
/**
'Test 1 average: 85.33'
'Test 2 average: 84.33'
'Test 3 average: 82.67'
*/
Jagged Arrays
Jagged Arrays๋ array์ ๊ฐ row์ ๊ธธ์ด๊ฐ ๋ค๋ฅธ array๋ฅผ ๋ปํ๋ค. ์๋ฅผ ๋ค๋ฉด, ์ด๋ค row๋ 3๊ฐ์ ์์๋ฅผ, ๋ค๋ฅธ row๋ 5๊ฐ์ ์์๋ฅผ, ๋ ๋ค๋ฅธ row๋ ์์๊ฐ 1๊ฐ๋ฟ์ผ ์ ์๋ ์ค์ฒฉ array๋ค. ์ด๋ฐ ๋ค์ฅ๋ ์ฅํ array๋ผ๊ณ ํด๋ javascript for-loop์ ํตํด ๋ด๋ถ array์ ๊ธธ์ด๋ค์ ๊ฐ๊ฐ ๊ตฌํ ์ ์๋ค.
let grades = [[89, 77],[76, 82, 81],[91, 94, 89, 99]];
let total = 0;
let average = 0.0;
for (var row = 0; row < grades.length; ++row) {
for (var col = 0; col < grades[row].length; ++col) {
total += grades[row][col];
}
average = total / grades[row].length;
console.log("Student " + parseInt(row+1) + " average: " +
average.toFixed(2));
total = 0;
average = 0.0;
}
/**
Student 1 average: 83.00
Student 2 average: 79.67
Student 3 average: 93.25
*/
Object๋ฅผ ์์๋ก ํ๋ Array
์ง๊ธ๊น์ง ์์ ๋ฐ์ดํฐ ํ์ ์ ๊ฐ์ ์์๋ก ๊ฐ๋ array์ ๋ํด์ ์ดํด๋ดค๋ค.
Array๋ ์์๊ฐ๋ฟ๋ง ์๋๋ผ ๊ฐ์ฒด, ํจ์, ๊ฐ์ฒด์ ํ๋กํผํฐ ๊ฐ ๋ฑ ๋ชจ๋๋ฅผ ์์๋ก ๊ฐ์ง ์ ์๋ค.
function Point(x,y) {
this.x = x;
this.y = y;
}
function displayPts(arr) {
for (var i = 0; i < arr.length; ++i) {
console.log(arr[i].x + ", " + arr[i].y);
}
}
const p1 = new Point(1,2);
const p2 = new Point(3,5);
const p3 = new Point(2,8);
const p4 = new Point(4,4);
const points = [p1,p2,p3,p4];
for (let i = 0; i < points.length; ++i) {
console.log("Point " + parseInt(i+1) + ": " + points[i].x + ", " +
points[i].y);
}
const p5 = new Point(12,-3);
points.push(p5);
console.log("After push: ");
displayPts(points);
points.shift();
console.log("After shift: ");
displayPts(points);
/**
Point 1: 1, 2
Point 2: 3, 5
Point 3: 2, 8
Point 4: 4, 4
After push:
1, 2
3, 5
2, 8
4, 4
12, -3
After shift:
3, 5
2, 8 4, 4 12, -3
*/
Array๋ฅผ ๊ฐ์ผ๋ก ๊ฐ๋ Object
์จ๋ array๋ฅผ ๊ฐ์ผ๋ก ๊ฐ๊ณ , ์จ๋๋ฅผ ๋ฐฐ์ด์ ์ถ๊ฐํ๊ณ , ํ๊ท ์ ๊ตฌํ ์ ์๋ ๊ฐ์ฒด์ ์์๋ฅผ ์ดํด๋ณด์.
function weekTemps() {
this.dataStore = [];
this.add = add;
this.average = average;
}
function add(temp) {
this.dataStore.push(temp);
}
function average() {
let total = 0;
for (let i = 0; i < this.dataStore.length; ++i) {
total += this.dataStore[i];
}
return total / this.dataStore.length;
}
const thisWeek = new weekTemps();
thisWeek.add(52);
thisWeek.add(55);
thisWeek.add(61);
thisWeek.add(65);
thisWeek.add(55);
thisWeek.add(50);
thisWeek.add(52);
thisWeek.add(49);
console.log(thisWeek.average()); // 54.875
๋๋ ํด๋์ค๋ก ํด๋น ์ฝ๋๋ฅผ ๋๊ฐ์ด ๊ตฌํํ ์ ์๋ค.
push() ๋์ add ๋ฉ์๋๋ฅผ ํ์ฉํ๋ฉด ํด๋น ํจ์๊ฐ ๋ฌด์์ ํ๋์ง ์ง๊ด์ ์ผ๋ก ์ ์ ์๋ค.
class WeekTemps{
constructor(){
this.dataStore = [];
}
add(temp){
this.dataStore.push(temp);
}
average(){
let total = 0;
for (let i = 0; i < this.dataStore.length; ++i) {
total += this.dataStore[i];
}
return total / this.dataStore.length;
}
}
const thisWeek = new WeekTemps();
thisWeek.add(52);
thisWeek.add(55);
thisWeek.add(61);
thisWeek.add(65);
thisWeek.add(55);
thisWeek.add(50);
thisWeek.add(52);
thisWeek.add(49);
console.log(thisWeek.average()); // 54.875
๐ฉ๐ป๐ป ๋ง์น๋ฉด์
์ค๋์ ์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด ๊น์ด ์ดํด๋ณด๊ธฐ ์ ์ ์๋ฐ์คํฌ๋ฆฝํธ์์์ ๊ธฐ๋ณธ ์๋ฃ๊ตฌ์กฐ์ธ Array์ ๋ํด ์ดํด๋ณด์๋ค.
์ฃผ๋ก ๋ด์ฅ๋ ํจ์๊ฐ ๋ง๊ณ , ๋ฐ์ดํฐ ํ์ ์ ์ ํ์ด ์์ด์ array๋ฅผ ๋ง์ด ํ์ฉํ๋ ๊ฑฐ ๊ฐ๋ค.
์ฑ ์ด ์ง์ด์ง ์ง (2014๋ ) 10๋ ์ด ๊ฑฐ์ ๋ ์ค๋, array ๊ด๋ จ ํจ์์ ์ฌ๋ฌ ๋ฌธ๋ฒ๋ค์ด ์ถ๊ฐ๋์๋๋ฐ ํด๋น ์ฑ ์ ๋ค๋ฃจ์ง ๋ชปํด์ ์์ฌ์ ๋ค. ์๋ฅผ ๋ค๋ฉด, ๋ ํจ์๋ฅผ ์ด์ด ์ฃผ๊ธฐ ์ํด์ ์คํ๋ ๋ ๋ฌธ๋ฒ( [...arr1, ...arr2])์ ํ์ฉํ ์ ์๋ค.
๋ค์์๋ List์ ๋ํด ์ดํด๋ณด๋๋ก ํ์.
๐ Reference
- Data Structures and Algorithms Using Javaโ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9
'๐ค data structures & algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Data Structures & Algorithms with Javascript : Linked Lists (0) | 2024.07.21 |
---|---|
Data Structures & Algorithms with Javascript : Queues (0) | 2024.07.20 |
Data Structures & Algorithms with Javascript : Stacks (0) | 2024.07.19 |
Data Structures & Algorithms with Javascript : Lists (0) | 2024.07.18 |
Data Structures & Algorithms with Javascript : Intro (0) | 2024.07.17 |