Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : 2주차 과제 작성 #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion problem-1-2/problem-1-2.test.js
Original file line number Diff line number Diff line change
@@ -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--;
}
Comment on lines +37 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이터레이터를 직접 얻어서 구현해주셨네요. 이터레이터의 내부 동작을 자세하게 알면 이렇게 구현해도 괜찮겠네요.
for of를 쓰면 이터러블을 소비하여 동작하게 되는데, 이렇게 구현해봐도 좋아요
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

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);
Expand Down
41 changes: 20 additions & 21 deletions problem-2-1/problem-2-1.test.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -33,36 +32,36 @@ 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();
stack.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();

Expand Down
Loading