Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 54 additions & 0 deletions problem-1-1/problem-1-1.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
class Node {
item;
next;
}

class Bag {
_n;
_first;

constructor() {
this._n = 0;
}

add(item) {
const newItem = new Node();

if (this.isEmpty()) {
this._first = newItem;
this._first.item = item;
} else {
const oldFirst = this._first;

this._first = newItem;
this._first.item = item;
this._first.next = oldFirst;
}

this._n = this._n + 1;
}

isEmpty() {
return this._n === 0;
}

size() {
return this._n;
}

[Symbol.iterator]() {
let cur = this._first;

return {
next() {
if (cur) {
const value = cur.item;

cur = cur.next;

return { done: false, value };
}

return { done: true };
}
};
}
}

test('백은 비어있는 상태로 생성된다', () => {
Expand Down
71 changes: 71 additions & 0 deletions problem-1-2/problem-1-2.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
class Node {
item;
next;
}

class Bag {
_n;
_first;
_sum;

constructor() {
this._n = 0;
this._sum = 0;
}

add(item) {
const newItem = new Node();

if (this.isEmpty()) {
this._first = newItem;
this._first.item = item;
} else {
const oldFirst = this._first;

this._first = newItem;
this._first.item = item;
this._first.next = oldFirst;
}

this._n = this._n + 1;
}

isEmpty() {
return this._n === 0;
}

size() {
return this._n;
}

[Symbol.iterator]() {
let cur = this._first;

return {
next() {
if (cur) {
const value = cur.item;

cur = cur.next;

return { done: false, value };
}

return { done: true };
}
};
}
}

const solution = (numbers) => {
const bag = new Bag();

for (const num of numbers) {
bag.add(num);
}

let sum = 0;
for (const item of bag) {
sum += item;
}

return Math.floor(sum / bag.size());
};

test('숫자 배열의 평균을 반환한다', () => {
Expand Down
72 changes: 72 additions & 0 deletions problem-2-1/problem-2-1.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
class Node {
item;
next;
}

class Stack {
_n;
_first;

constructor() {
this._n = 0;
}

push(item) {
const newItem = new Node();

if (this.isEmpty()) {
this._first = newItem;
this._first.item = item;
} else {
const oldFirst = this._first;

this._first = newItem;
this._first.item = item;
this._first.next = oldFirst;
}

this._n = this._n + 1;
}

pop() {
if (this.isEmpty()) {
throw new Error('스택이 비어있습니다');
}

const target = this._first;

if (this._first.next) {
this._first = this._first.next;
} else {
this._first = undefined;
}

this._n = this._n - 1;

return target.item;
}

isEmpty() {
return this._n === 0;
}

size() {
return this._n;
}

[Symbol.iterator]() {
let cur = this._first;

return {
next() {
if (cur) {
const value = cur.item;

cur = cur.next;

return { done: false, value };
}

return { done: true };
}
};
}
}

test('스택을 생성하면 비어있다', () => {
Expand Down
103 changes: 103 additions & 0 deletions problem-2-2/problem-2-2.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,107 @@
class Node {
item;
next;
}

class Stack {
_n;
_first;

constructor() {
this._n = 0;
}

push(item) {
const newItem = new Node();

if (this.isEmpty()) {
this._first = newItem;
this._first.item = item;
} else {
const oldFirst = this._first;

this._first = newItem;
this._first.item = item;
this._first.next = oldFirst;
}

this._n = this._n + 1;
}

pop() {
if (this.isEmpty()) {
throw new Error('스택이 비어있습니다');
}

const target = this._first;

if (this._first.next) {
this._first = this._first.next;
} else {
this._first = undefined;
}

this._n = this._n - 1;

return target.item;
}

isEmpty() {
return this._n === 0;
}

size() {
return this._n;
}

[Symbol.iterator]() {
let cur = this._first;

return {
next() {
if (cur) {
const value = cur.item;

cur = cur.next;

return { done: false, value };
}

return { done: true };
}
};
}
}

const solution = (string) => {
const stack = new Stack();

const open = ['{', '(', '[']; // open 일때 push
const pair = { // close 일때 pop
'}': '{',
')': '(',
']': '[',
};

for (const str of string) {
const isOpenBracket = open.includes(str);

if (isOpenBracket) {
stack.push(str);
} else {
try {
const popped = stack.pop();

if (!pair[str] === popped) {
return false;
}
} catch(e) {
return false;
}
}
}

return true;
};

test('문자열에 포함된 괄호의 짝이 맞을 때 true를 반환한다', () => {
Expand Down