diff --git a/problem-1-1/problem-1-1.test.js b/problem-1-1/problem-1-1.test.js index 6ab9348..4cdac1c 100644 --- a/problem-1-1/problem-1-1.test.js +++ b/problem-1-1/problem-1-1.test.js @@ -1,25 +1,111 @@ +/* eslint-disable no-unused-vars */ +/* eslint-disable lines-between-class-members */ +/* eslint-disable quotes */ +class Bag1 { + #items; + #n; + + constructor(size = 10) { + this.#items = new Array(size); + this.#n = 0; + } + + add(item) { + this.#items[this.#n] = item; + this.#n++; + } + + size() { + return this.#n; + } + + isEmpty() { + return this.#n === 0; + } + + [Symbol.iterator]() { + let index = 0; + const n = this.#n; + const data = this.#items; + + return { + next() { + return index < n + ? { done: false, value: data[index++] } + : { done: true }; + }, + }; + } +} + +// 연결리스트로 구현 +class Node { + item; + next; +} + class Bag { + // 연결리스트로 생성한 백, 테스트 코드를 바꾸지 않아도 동작하는 것을 보고 우리는 추상 데이터 타입을 잘 구현했다고 한다. + #first; // 맨 처음 요소를 가리키는 일종의 포인터 + #n; + + constructor() { + this.#n = 0; + } + + add(item) { + const oldFirst = this.#first; + this.#first = new Node(); + this.#first.item = item; + this.#first.next = oldFirst; + + this.#n++; + } + + size() { + return this.#n; + } + + isEmpty() { + return this.#first === undefined; + } + + [Symbol.iterator]() { + let current = this.#first; + + return { + next() { + if (current === undefined) { + return { done: true }; + } + + const value = current.item; + current = current.next; + return { done: false, value }; + }, + }; + } } -test('백은 비어있는 상태로 생성된다', () => { +test("백은 비어있는 상태로 생성된다", () => { const bag = new Bag(); expect(bag.isEmpty()).toBe(true); }); -test('백에 값을 추가하면, 개수가 증가한다', () => { +test("백에 값을 추가하면, 개수가 증가한다", () => { const bag = new Bag(); const oldSize = bag.size(); - bag.add('D'); + bag.add("D"); const newSize = bag.size(); expect(newSize - oldSize).toBe(1); }); -test('백의 모든 요소를 순회할 수 있다', () => { +test("백의 모든 요소를 순회할 수 있다", () => { const bag = new Bag(); [1, 3, 5, 7, 9].forEach((i) => { diff --git a/problem-1-2/problem-1-2.test.js b/problem-1-2/problem-1-2.test.js index f2956b6..bc0ac7b 100644 --- a/problem-1-2/problem-1-2.test.js +++ b/problem-1-2/problem-1-2.test.js @@ -1,7 +1,51 @@ +class Bag { + #items = []; + + add(item) { + this.#items.push(item); + } + + isEmpty() { + return this.#items.length === 0; + } + + size() { + return this.#items.length; + } + + [Symbol.iterator]() { + let index = 0; + let data = this.#items; + + return { + next() { + return index < data.length + ? { done: false, value: data[index++] } + : { done: true }; + }, + }; + } +} + const solution = (numbers) => { + // 백을 만든다. + const bag = new Bag(); + //백에 배열 요소들을 다 넣는다. + for (let i = 0; i < numbers.length; i++) { + bag.add(numbers[i]); + } + const iterator = bag[Symbol.iterator](); + let size = numbers.length; + let result = 0; + + while (size != 0) { + result += iterator.next().value; + size--; + } + return Math.floor(result / bag.size()); }; -test('숫자 배열의 평균을 반환한다', () => { +test("숫자 배열의 평균을 반환한다", () => { expect(solution([1, 2, 3, 4, 5])).toEqual(3); expect(solution([1, 3, 5, 7, 9, 11])).toEqual(6); expect(solution([2, 4, 6, 8, 10, 12, 13, 14, 15])).toEqual(9); diff --git a/problem-2-1/problem-2-1.test.js b/problem-2-1/problem-2-1.test.js index 17412df..817a2cd 100644 --- a/problem-2-1/problem-2-1.test.js +++ b/problem-2-1/problem-2-1.test.js @@ -1,28 +1,27 @@ -class Stack { -} +class Stack {} -test('스택을 생성하면 비어있다', () => { +test("스택을 생성하면 비어있다", () => { const stack = new Stack(); expect(stack.isEmpty()).toEqual(true); }); -test('스택에 값을 추가하면 개수가 증가한다', () => { +test("스택에 값을 추가하면 개수가 증가한다", () => { const stack = new Stack(); const oldSize = stack.size(); - stack.push('D'); + stack.push("D"); const newSize = stack.size(); expect(newSize - oldSize).toEqual(1); }); -test('스택에서 요소를 제거하면 개수가 감소한다', () => { +test("스택에서 요소를 제거하면 개수가 감소한다", () => { const stack = new Stack(); - stack.push('D'); + stack.push("D"); const oldSize = stack.size(); @@ -33,24 +32,24 @@ test('스택에서 요소를 제거하면 개수가 감소한다', () => { expect(newSize - oldSize).toEqual(-1); }); -test('가장 최근에 삽입한게 먼저 나온다', () => { +test("가장 최근에 삽입한게 먼저 나온다", () => { const stack = new Stack(); - stack.push('D'); - stack.push('S'); - stack.push('A'); + stack.push("D"); + stack.push("S"); + stack.push("A"); - expect(stack.pop()).toBe('A'); - expect(stack.pop()).toBe('S'); - expect(stack.pop()).toBe('D'); + expect(stack.pop()).toBe("A"); + expect(stack.pop()).toBe("S"); + expect(stack.pop()).toBe("D"); }); -test('스택이 비어있는데 pop을 하면 예외를 던진다', () => { +test("스택이 비어있는데 pop을 하면 예외를 던진다", () => { const stack = new Stack(); - stack.push('D'); - stack.push('S'); - stack.push('A'); + stack.push("D"); + stack.push("S"); + stack.push("A"); stack.pop(); stack.pop(); @@ -58,11 +57,11 @@ test('스택이 비어있는데 pop을 하면 예외를 던진다', () => { expect(() => { stack.pop(); - }).toThrowError('스택이 비어있습니다'); + }).toThrowError("스택이 비어있습니다"); }); -test('스택은 역순으로 순회한다', () => { - const data = ['D', 'S', 'A', 'E', 'X', 'A', 'M', 'P', 'L', 'E']; +test("스택은 역순으로 순회한다", () => { + const data = ["D", "S", "A", "E", "X", "A", "M", "P", "L", "E"]; const stack = new Stack();