From ddeca9893d2a6057c52bf51e68a777d146347bdd Mon Sep 17 00:00:00 2001 From: syaring Date: Sat, 18 Nov 2023 17:01:23 +0900 Subject: [PATCH 1/4] =?UTF-8?q?week2=201-1:=20Bag=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1-1/problem-1-1.test.js | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/problem-1-1/problem-1-1.test.js b/problem-1-1/problem-1-1.test.js index 6ab9348..5d01697 100644 --- a/problem-1-1/problem-1-1.test.js +++ b/problem-1-1/problem-1-1.test.js @@ -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('백은 비어있는 상태로 생성된다', () => { From 33977b653ebc40db034ffa14c3fb2f6955cad667 Mon Sep 17 00:00:00 2001 From: syaring Date: Sat, 18 Nov 2023 17:38:28 +0900 Subject: [PATCH 2/4] =?UTF-8?q?week2=201-2:=20Bag=20=ED=99=9C=EC=9A=A9=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1-2/problem-1-2.test.js | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/problem-1-2/problem-1-2.test.js b/problem-1-2/problem-1-2.test.js index f2956b6..83bfacb 100644 --- a/problem-1-2/problem-1-2.test.js +++ b/problem-1-2/problem-1-2.test.js @@ -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('숫자 배열의 평균을 반환한다', () => { From 8bc3619a7be1f1e700b3d5d1fcd71820d826175c Mon Sep 17 00:00:00 2001 From: syaring Date: Sat, 18 Nov 2023 18:30:04 +0900 Subject: [PATCH 3/4] =?UTF-8?q?week2=202-1:=20Stack=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2-1/problem-2-1.test.js | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/problem-2-1/problem-2-1.test.js b/problem-2-1/problem-2-1.test.js index 17412df..dcc14f8 100644 --- a/problem-2-1/problem-2-1.test.js +++ b/problem-2-1/problem-2-1.test.js @@ -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('스택을 생성하면 비어있다', () => { From cca10a3ebebd8bc1329faa8678ddf6dcd338ae03 Mon Sep 17 00:00:00 2001 From: syaring Date: Sat, 18 Nov 2023 18:47:10 +0900 Subject: [PATCH 4/4] =?UTF-8?q?week2=202-2:=20Stack=20=ED=99=9C=EC=9A=A9?= =?UTF-8?q?=20=EB=AC=B8=EC=A0=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2-2/problem-2-2.test.js | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/problem-2-2/problem-2-2.test.js b/problem-2-2/problem-2-2.test.js index dab5212..3d71a20 100644 --- a/problem-2-2/problem-2-2.test.js +++ b/problem-2-2/problem-2-2.test.js @@ -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를 반환한다', () => {