mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* File: array.js
|
||||
* Created Time: 2022-11-27
|
||||
* Author: IsChristina ([email protected])
|
||||
*/
|
||||
|
||||
/* 원소에 임의 접근 */
|
||||
function randomAccess(nums) {
|
||||
// 구간 [0, nums.length)에서 숫자 하나를 무작위로 뽑는다
|
||||
const random_index = Math.floor(Math.random() * nums.length);
|
||||
// 무작위 원소를 가져와 반환한다
|
||||
const random_num = nums[random_index];
|
||||
return random_num;
|
||||
}
|
||||
|
||||
/* 배열 길이 확장 */
|
||||
// JavaScript의 Array는 동적 배열이므로 직접 확장할 수 있다
|
||||
// 학습의 편의를 위해 이 함수에서는 Array를 길이가 고정된 배열로 취급한다
|
||||
function extend(nums, enlarge) {
|
||||
// 길이를 확장한 배열을 초기화한다
|
||||
const res = new Array(nums.length + enlarge).fill(0);
|
||||
// 원래 배열의 모든 원소를 새 배열에 복사한다
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 확장한 새 배열을 반환한다
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 배열의 인덱스 index에 원소 num 삽입 */
|
||||
function insert(nums, num, index) {
|
||||
// 인덱스 index와 그 뒤에 있는 모든 원소를 한 칸씩 뒤로 이동한다
|
||||
for (let i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// index 위치의 원소에 num을 대입한다
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 인덱스 index에 있는 원소 삭제 */
|
||||
function remove(nums, index) {
|
||||
// 인덱스 index 뒤에 있는 모든 원소를 한 칸씩 앞으로 이동한다
|
||||
for (let i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 배열 순회 */
|
||||
function traverse(nums) {
|
||||
let count = 0;
|
||||
// 인덱스를 통해 배열을 순회한다
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 배열의 원소를 직접 순회한다
|
||||
for (const num of nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* 배열에서 지정한 원소 탐색 */
|
||||
function find(nums, target) {
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
if (nums[i] === target) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 배열 초기화 */
|
||||
const arr = new Array(5).fill(0);
|
||||
console.log('배열 arr =', arr);
|
||||
let nums = [1, 3, 2, 5, 4];
|
||||
console.log('배열 nums =', nums);
|
||||
|
||||
/* 임의 접근 */
|
||||
let random_num = randomAccess(nums);
|
||||
console.log('nums에서 무작위로 가져온 원소', random_num);
|
||||
|
||||
/* 길이 확장 */
|
||||
nums = extend(nums, 3);
|
||||
console.log('배열 길이를 8로 확장한 후 nums =', nums);
|
||||
|
||||
/* 원소 삽입 */
|
||||
insert(nums, 6, 3);
|
||||
console.log('인덱스 3에 숫자 6을 삽입한 후 nums =', nums);
|
||||
|
||||
/* 원소 삭제 */
|
||||
remove(nums, 2);
|
||||
console.log('인덱스 2의 원소를 삭제한 후 nums =', nums);
|
||||
|
||||
/* 배열 순회 */
|
||||
traverse(nums);
|
||||
|
||||
/* 원소 탐색 */
|
||||
let index = find(nums, 3);
|
||||
console.log('nums에서 원소 3을 탐색하여 얻은 인덱스 =', index);
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* File: linked_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: IsChristina ([email protected]), Justin ([email protected])
|
||||
*/
|
||||
|
||||
const { printLinkedList } = require('../modules/PrintUtil');
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 연결 리스트의 노드 n0 뒤에 노드 P 삽입 */
|
||||
function insert(n0, P) {
|
||||
const n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* 연결 리스트의 노드 n0 바로 뒤에 있는 노드 삭제 */
|
||||
function remove(n0) {
|
||||
if (!n0.next) return;
|
||||
// n0 -> P -> n1
|
||||
const P = n0.next;
|
||||
const n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 연결 리스트에서 인덱스가 index인 노드에 접근 */
|
||||
function access(head, index) {
|
||||
for (let i = 0; i < index; i++) {
|
||||
if (!head) {
|
||||
return null;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 연결 리스트에서 값이 target인 첫 번째 노드 탐색 */
|
||||
function find(head, target) {
|
||||
let index = 0;
|
||||
while (head !== null) {
|
||||
if (head.val === target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 연결 리스트 초기화 */
|
||||
// 각 노드를 초기화한다
|
||||
const n0 = new ListNode(1);
|
||||
const n1 = new ListNode(3);
|
||||
const n2 = new ListNode(2);
|
||||
const n3 = new ListNode(5);
|
||||
const n4 = new ListNode(4);
|
||||
// 노드 사이의 참조를 설정한다
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
console.log('초기화한 연결 리스트');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 노드 삽입 */
|
||||
insert(n0, new ListNode(0));
|
||||
console.log('노드를 삽입한 후의 연결 리스트');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 노드 삭제 */
|
||||
remove(n0);
|
||||
console.log('노드를 삭제한 후의 연결 리스트');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 노드 접근 */
|
||||
const node = access(n0, 3);
|
||||
console.log('연결 리스트에서 인덱스 3에 있는 노드의 값 = ' + node.val);
|
||||
|
||||
/* 노드 탐색 */
|
||||
const index = find(n0, 2);
|
||||
console.log('연결 리스트에서 값이 2인 노드의 인덱스 = ' + index);
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* File: list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin ([email protected])
|
||||
*/
|
||||
|
||||
/* 리스트 초기화 */
|
||||
const nums = [1, 3, 2, 5, 4];
|
||||
console.log(`리스트 nums = ${nums}`);
|
||||
|
||||
/* 원소 접근 */
|
||||
const num = nums[1];
|
||||
console.log(`인덱스 1의 원소에 접근하여 얻은 num = ${num}`);
|
||||
|
||||
/* 원소 갱신 */
|
||||
nums[1] = 0;
|
||||
console.log(`인덱스 1의 원소를 0으로 갱신한 후 nums = ${nums}`);
|
||||
|
||||
/* 리스트 비우기 */
|
||||
nums.length = 0;
|
||||
console.log(`리스트를 비운 후 nums = ${nums}`);
|
||||
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
nums.push(5);
|
||||
nums.push(4);
|
||||
console.log(`원소를 추가한 후 nums = ${nums}`);
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
nums.splice(3, 0, 6);
|
||||
console.log(`인덱스 3에 숫자 6을 삽입한 후 nums = ${nums}`);
|
||||
|
||||
/* 원소 삭제 */
|
||||
nums.splice(3, 1);
|
||||
console.log(`인덱스 3의 원소를 삭제한 후 nums = ${nums}`);
|
||||
|
||||
/* 인덱스를 통해 리스트를 순회한다 */
|
||||
let count = 0;
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* 리스트의 원소를 직접 순회한다 */
|
||||
count = 0;
|
||||
for (const x of nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 두 리스트 연결 */
|
||||
const nums1 = [6, 8, 7, 10, 9];
|
||||
nums.push(...nums1);
|
||||
console.log(`리스트 nums1을 nums 뒤에 연결한 후 nums = ${nums}`);
|
||||
|
||||
/* 리스트 정렬 */
|
||||
nums.sort((a, b) => a - b);
|
||||
console.log(`리스트를 정렬한 후 nums = ${nums}`);
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* File: my_list.js
|
||||
* Created Time: 2022-12-12
|
||||
* Author: Justin ([email protected])
|
||||
*/
|
||||
|
||||
/* 리스트 클래스 */
|
||||
class MyList {
|
||||
#arr = new Array(); // 배열(리스트 원소 저장)
|
||||
#capacity = 10; // 리스트 용량
|
||||
#size = 0; // 리스트 길이(현재 원소 개수)
|
||||
#extendRatio = 2; // 리스트의 용량을 확장할 때마다 적용하는 배수
|
||||
|
||||
/* 생성자 */
|
||||
constructor() {
|
||||
this.#arr = new Array(this.#capacity);
|
||||
}
|
||||
|
||||
/* 리스트 길이(현재 원소 개수) 반환*/
|
||||
size() {
|
||||
return this.#size;
|
||||
}
|
||||
|
||||
/* 리스트 용량 반환 */
|
||||
capacity() {
|
||||
return this.#capacity;
|
||||
}
|
||||
|
||||
/* 원소 접근 */
|
||||
get(index) {
|
||||
// 인덱스가 범위를 벗어나면 예외를 발생시킨다. 아래에서도 동일하다
|
||||
if (index < 0 || index >= this.#size) throw new Error('인덱스가 범위를 벗어났다');
|
||||
return this.#arr[index];
|
||||
}
|
||||
|
||||
/* 원소 갱신 */
|
||||
set(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('인덱스가 범위를 벗어났다');
|
||||
this.#arr[index] = num;
|
||||
}
|
||||
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
add(num) {
|
||||
// 길이가 용량과 같으면 용량을 확장해야 한다
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// 새 원소를 리스트 맨 뒤에 추가한다
|
||||
this.#arr[this.#size] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
insert(index, num) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('인덱스가 범위를 벗어났다');
|
||||
// 원소 개수가 용량을 초과하면 용량 확장을 수행한다
|
||||
if (this.#size === this.#capacity) {
|
||||
this.extendCapacity();
|
||||
}
|
||||
// 인덱스 index와 그 뒤의 원소를 모두 한 칸씩 뒤로 이동한다
|
||||
for (let j = this.#size - 1; j >= index; j--) {
|
||||
this.#arr[j + 1] = this.#arr[j];
|
||||
}
|
||||
// 원소 개수를 갱신한다
|
||||
this.#arr[index] = num;
|
||||
this.#size++;
|
||||
}
|
||||
|
||||
/* 원소 삭제 */
|
||||
remove(index) {
|
||||
if (index < 0 || index >= this.#size) throw new Error('인덱스가 범위를 벗어났다');
|
||||
let num = this.#arr[index];
|
||||
// 인덱스 index 뒤의 원소를 모두 한 칸씩 앞으로 이동한다
|
||||
for (let j = index; j < this.#size - 1; j++) {
|
||||
this.#arr[j] = this.#arr[j + 1];
|
||||
}
|
||||
// 원소 개수를 갱신한다
|
||||
this.#size--;
|
||||
// 삭제한 원소를 반환한다
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 리스트 용량 확장 */
|
||||
extendCapacity() {
|
||||
// 길이가 원래 배열의 extendRatio배인 새 배열을 생성하고, 원래 배열을 새 배열에 복사한다
|
||||
this.#arr = this.#arr.concat(
|
||||
new Array(this.capacity() * (this.#extendRatio - 1))
|
||||
);
|
||||
// 리스트 용량을 갱신한다
|
||||
this.#capacity = this.#arr.length;
|
||||
}
|
||||
|
||||
/* 리스트를 배열로 변환 */
|
||||
toArray() {
|
||||
let size = this.size();
|
||||
// 유효한 길이 범위 안의 리스트 원소만 변환한다
|
||||
const arr = new Array(size);
|
||||
for (let i = 0; i < size; i++) {
|
||||
arr[i] = this.get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 리스트 초기화 */
|
||||
const nums = new MyList();
|
||||
/* 맨 뒤에 원소 추가 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
console.log(
|
||||
`리스트 nums = ${nums.toArray()}, 용량 = ${nums.capacity()}, 길이 = ${nums.size()}`
|
||||
);
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
nums.insert(3, 6);
|
||||
console.log(`인덱스 3에 숫자 6을 삽입한 후 nums = ${nums.toArray()}`);
|
||||
|
||||
/* 원소 삭제 */
|
||||
nums.remove(3);
|
||||
console.log(`인덱스 3의 원소를 삭제한 후 nums = ${nums.toArray()}`);
|
||||
|
||||
/* 원소 접근 */
|
||||
const num = nums.get(1);
|
||||
console.log(`인덱스 1의 원소에 접근하여 얻은 num = ${num}`);
|
||||
|
||||
/* 원소 갱신 */
|
||||
nums.set(1, 0);
|
||||
console.log(`인덱스 1의 원소를 0으로 갱신한 후 nums = ${nums.toArray()}`);
|
||||
|
||||
/* 용량 확장 동작 테스트 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
// i = 5일 때 리스트 길이가 용량을 초과하므로 용량 확장이 수행된다
|
||||
nums.add(i);
|
||||
}
|
||||
console.log(
|
||||
`용량을 확장한 후 리스트 nums = ${nums.toArray()}, 용량 = ${nums.capacity()}, 길이 = ${nums.size()}`
|
||||
);
|
||||
Reference in New Issue
Block a user