From a74fbc81a395efccdd13dbe5f645204e2df20c17 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Wed, 13 Sep 2023 23:22:15 +0900 Subject: [PATCH 01/15] solve problem-1-1 --- problem-1-1/problem-1-1.test.js | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problem-1-1/problem-1-1.test.js b/problem-1-1/problem-1-1.test.js index 6ab9348..2c6babe 100644 --- a/problem-1-1/problem-1-1.test.js +++ b/problem-1-1/problem-1-1.test.js @@ -1,4 +1,38 @@ class Bag { + #items = []; + + isEmpty() { + return this.size() === 0; + } + + size() { + return this.#items.length; + } + + add(item) { + this.#items.push(item); + } + + [Symbol.iterator]() { + let index = 0; + const data = [...this.#items]; + + return { + next() { + if (index < data.length) { + const value = data[index]; + index += 1; + + return { + done: false, + value, + }; + } + + return { done: true }; + }, + }; + } } test('백은 비어있는 상태로 생성된다', () => { From 8ce1b92aac47c3ac2a22fbbd0cc61fd374b8ed0a Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Wed, 13 Sep 2023 23:32:05 +0900 Subject: [PATCH 02/15] solve problem-1-2 --- problem-1-2/problem-1-2.test.js | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problem-1-2/problem-1-2.test.js b/problem-1-2/problem-1-2.test.js index f2956b6..1ad54ec 100644 --- a/problem-1-2/problem-1-2.test.js +++ b/problem-1-2/problem-1-2.test.js @@ -1,4 +1,52 @@ +class Bag { + #items = []; + + isEmpty() { + return this.size() === 0; + } + + size() { + return this.#items.length; + } + + add(item) { + this.#items.push(item); + } + + sum() { + return this.#items.reduce((acc, cur) => acc + cur, 0); + } + + [Symbol.iterator]() { + let index = 0; + const data = [...this.#items]; + + return { + next() { + if (index < data.length) { + const value = data[index]; + index += 1; + + return { + done: false, + value, + }; + } + + return { done: true }; + }, + }; + } +} + const solution = (numbers) => { + const scores = new Bag(); + + numbers.forEach((number) => { + scores.add(number); + }); + + return Math.floor(scores.sum() / scores.size()); }; test('숫자 배열의 평균을 반환한다', () => { From 88a2a04106537dfeb4c24cecdb41c8940afa43e0 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sat, 16 Sep 2023 22:35:31 +0900 Subject: [PATCH 03/15] solve problem-2-1 --- problem-2-1/problem-2-1.test.js | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/problem-2-1/problem-2-1.test.js b/problem-2-1/problem-2-1.test.js index 17412df..bbb5d86 100644 --- a/problem-2-1/problem-2-1.test.js +++ b/problem-2-1/problem-2-1.test.js @@ -1,4 +1,56 @@ class Stack { + #n = -1; + + #items = []; + + isEmpty() { + return this.#n === -1; + } + + size() { + return this.#n + 1; + } + + push(item) { + this.#items.push(item); + this.#n += 1; + } + + pop() { + if (this.size() === 0) { + throw new Error('스택이 비어있습니다'); + } + + const popped = this.#items[this.#n]; + + // TODO: 이렇게 구현하는 이유는? + this.#items[this.#n] = undefined; + + this.#n -= 1; + + return popped; + } + + [Symbol.iterator]() { + let index = this.#items.length - 1; + const data = [...this.#items]; + + return { + next() { + if (index >= 0) { + const value = data[index]; + index -= 1; + + return { + done: false, + value, + }; + } + + return { done: true }; + }, + }; + } } test('스택을 생성하면 비어있다', () => { From ee55f232b7af962b3d615023171bea8e4f713817 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sat, 16 Sep 2023 23:00:29 +0900 Subject: [PATCH 04/15] =?UTF-8?q?problem-2-1=20=EC=8A=A4=ED=83=9D=20push?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B0=B0=EC=97=B4=EC=9D=98=20?= =?UTF-8?q?push=20=EC=82=AC=EC=9A=A9=20=ED=95=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2-1/problem-2-1.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problem-2-1/problem-2-1.test.js b/problem-2-1/problem-2-1.test.js index bbb5d86..ca90d50 100644 --- a/problem-2-1/problem-2-1.test.js +++ b/problem-2-1/problem-2-1.test.js @@ -12,8 +12,8 @@ class Stack { } push(item) { - this.#items.push(item); this.#n += 1; + this.#items[this.#n] = item; } pop() { From 678ecf238383c479ea17628ee3eb7a06b128d138 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sat, 16 Sep 2023 23:32:06 +0900 Subject: [PATCH 05/15] =?UTF-8?q?problem-2-1=20=EB=8F=99=EC=A0=81=20?= =?UTF-8?q?=ED=81=AC=EA=B8=B0=20=EB=B3=80=EA=B2=BD=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=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 | 61 +++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/problem-2-1/problem-2-1.test.js b/problem-2-1/problem-2-1.test.js index ca90d50..b0b85d3 100644 --- a/problem-2-1/problem-2-1.test.js +++ b/problem-2-1/problem-2-1.test.js @@ -1,7 +1,18 @@ class Stack { #n = -1; - #items = []; + #items; + + #capacity; + + constructor(capacity = 0) { + this.#capacity = capacity; + this.#items = Array(capacity).fill(undefined); + } + + isFull() { + return this.size() === this.#capacity; + } isEmpty() { return this.#n === -1; @@ -12,12 +23,18 @@ class Stack { } push(item) { + if (this.isFull()) { + const newItems = Array(this.#capacity).fill(undefined); + this.#items = this.#items.concat(newItems); + this.#capacity *= 2; + } + this.#n += 1; this.#items[this.#n] = item; } pop() { - if (this.size() === 0) { + if (this.isEmpty()) { throw new Error('스택이 비어있습니다'); } @@ -28,9 +45,19 @@ class Stack { this.#n -= 1; + if (this.size() === (this.#capacity / 4)) { + this.#capacity /= 2; + + this.#items = this.#items.slice(0, this.#capacity); + } + return popped; } + get length() { + return this.#capacity; + } + [Symbol.iterator]() { let index = this.#items.length - 1; const data = [...this.#items]; @@ -130,3 +157,33 @@ test('스택은 역순으로 순회한다', () => { expect(output.reverse()).toEqual(data); }); + +test('스택에 더이상 추가할 수 없을 경우 크기가 2배로 늘어난다', () => { + const data = ['D', 'S', 'A', 'E']; + + const capacity = 3; + const stack = new Stack(capacity); + + data.forEach((i) => { + stack.push(i); + }); + + expect(stack.length).toBe((2 * capacity)); +}); + +test('아이템을 제거할 때 아이템의 수가 1 / 4이하라면 배열의 크기가 1/2로 줄어든다', () => { + const data = ['D', 'S', 'A', 'E']; + + const capacity = 4; + const stack = new Stack(capacity); + + data.forEach((i) => { + stack.push(i); + }); + + stack.pop(); + stack.pop(); + stack.pop(); + + expect(stack.length).toBe((capacity / 2)); +}); From b72c0fe0a9282140de83417eaa8fe267c7d95f87 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sat, 16 Sep 2023 23:35:55 +0900 Subject: [PATCH 06/15] solve problem-2-2 --- problem-2-2/problem-2-2.test.js | 98 ++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/problem-2-2/problem-2-2.test.js b/problem-2-2/problem-2-2.test.js index dab5212..8f5124f 100644 --- a/problem-2-2/problem-2-2.test.js +++ b/problem-2-2/problem-2-2.test.js @@ -1,4 +1,100 @@ -const solution = (string) => { +class Stack { + #n = -1; + + #items; + + #capacity; + + constructor(capacity = 0) { + this.#capacity = capacity; + this.#items = Array(capacity).fill(undefined); + } + + isFull() { + return this.size() === this.#capacity; + } + + isEmpty() { + return this.#n === -1; + } + + size() { + return this.#n + 1; + } + + push(item) { + this.#n += 1; + this.#items[this.#n] = item; + } + + pop() { + if (this.isEmpty()) { + throw new Error('스택이 비어있습니다'); + } + + const popped = this.#items[this.#n]; + + this.#items[this.#n] = undefined; + + this.#n -= 1; + + return popped; + } + + get length() { + return this.#capacity; + } + + [Symbol.iterator]() { + let index = this.#items.length - 1; + const data = [...this.#items]; + + return { + next() { + if (index >= 0) { + const value = data[index]; + index -= 1; + + return { + done: false, + value, + }; + } + + return { done: true }; + }, + }; + } +} + +const bracketPairs = { + ')': '(', + '}': '{', + ']': '[', +}; + +const solution = (bracekts = '') => { + const stack = new Stack(bracekts.length); + + for (let i = 0; i < bracekts.length; i++) { + const bracket = bracekts.charAt(i); + + if (bracket === '}' || bracket === ')' || bracket === ']') { + if (stack.isEmpty()) { + return false; + } + + const popped = stack.pop(); + + if (bracketPairs[bracket] !== popped) { + return false; + } + } else { + stack.push(bracket); + } + } + + return true; }; test('문자열에 포함된 괄호의 짝이 맞을 때 true를 반환한다', () => { From 937df01fd8ec94e74456cb8c5d1514aa581fa22e Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 17 Sep 2023 01:11:23 +0900 Subject: [PATCH 07/15] solve problem-3-1 --- problem-3-1/problem-3-1.test.js | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/problem-3-1/problem-3-1.test.js b/problem-3-1/problem-3-1.test.js index 6ae251f..0f7a9ee 100644 --- a/problem-3-1/problem-3-1.test.js +++ b/problem-3-1/problem-3-1.test.js @@ -1,4 +1,50 @@ class Queue { + #items = []; + + isEmpty() { + return this.size() === 0; + } + + size() { + return this.#items.length; + } + + enqueue(item) { + this.#items.push(item); + } + + dequeue() { + if (this.isEmpty()) { + throw new Error('큐가 비어있습니다'); + } + + const item = this.#items[0]; + + this.#items = this.#items.slice(1); + + return item; + } + + [Symbol.iterator]() { + let index = 0; + const data = [...this.#items]; + + return { + next() { + if (index < data.length) { + const value = data[index]; + index += 1; + + return { + done: false, + value, + }; + } + + return { done: true }; + }, + }; + } } test('큐를 생성하면 비어있다', () => { From e44eae16ecba45dc30527635081857fb99f036d5 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 17 Sep 2023 02:17:26 +0900 Subject: [PATCH 08/15] =?UTF-8?q?problem-2-1=20=EC=8A=A4=ED=83=9D=20?= =?UTF-8?q?=EC=97=B0=EA=B2=B0=EB=A6=AC=EC=8A=A4=ED=8A=B8=EB=A1=9C=20?= =?UTF-8?q?=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 | 99 ++++++++++----------------------- 1 file changed, 28 insertions(+), 71 deletions(-) diff --git a/problem-2-1/problem-2-1.test.js b/problem-2-1/problem-2-1.test.js index b0b85d3..08efb7a 100644 --- a/problem-2-1/problem-2-1.test.js +++ b/problem-2-1/problem-2-1.test.js @@ -1,36 +1,34 @@ -class Stack { - #n = -1; - - #items; +class NodeItem { + item; - #capacity; + next; - constructor(capacity = 0) { - this.#capacity = capacity; - this.#items = Array(capacity).fill(undefined); + constructor(item) { + this.item = item; } +} - isFull() { - return this.size() === this.#capacity; - } +class Stack { + #first; + + #size = 0; isEmpty() { - return this.#n === -1; + return this.#first === undefined; } size() { - return this.#n + 1; + return this.#size; } push(item) { - if (this.isFull()) { - const newItems = Array(this.#capacity).fill(undefined); - this.#items = this.#items.concat(newItems); - this.#capacity *= 2; - } + const oldFirst = this.#first; + + this.#first = new NodeItem(item); - this.#n += 1; - this.#items[this.#n] = item; + this.#first.next = oldFirst; + + this.#size += 1; } pop() { @@ -38,39 +36,28 @@ class Stack { throw new Error('스택이 비어있습니다'); } - const popped = this.#items[this.#n]; - - // TODO: 이렇게 구현하는 이유는? - this.#items[this.#n] = undefined; + const popped = this.#first; - this.#n -= 1; + this.#first = this.#first.next; - if (this.size() === (this.#capacity / 4)) { - this.#capacity /= 2; + this.#size -= 1; - this.#items = this.#items.slice(0, this.#capacity); - } - - return popped; - } - - get length() { - return this.#capacity; + return popped.item; } [Symbol.iterator]() { - let index = this.#items.length - 1; - const data = [...this.#items]; + let current = this.#first; return { next() { - if (index >= 0) { - const value = data[index]; - index -= 1; + if (current !== undefined) { + const value = current.item; + + current = current.next; return { - done: false, value, + done: false, }; } @@ -157,33 +144,3 @@ test('스택은 역순으로 순회한다', () => { expect(output.reverse()).toEqual(data); }); - -test('스택에 더이상 추가할 수 없을 경우 크기가 2배로 늘어난다', () => { - const data = ['D', 'S', 'A', 'E']; - - const capacity = 3; - const stack = new Stack(capacity); - - data.forEach((i) => { - stack.push(i); - }); - - expect(stack.length).toBe((2 * capacity)); -}); - -test('아이템을 제거할 때 아이템의 수가 1 / 4이하라면 배열의 크기가 1/2로 줄어든다', () => { - const data = ['D', 'S', 'A', 'E']; - - const capacity = 4; - const stack = new Stack(capacity); - - data.forEach((i) => { - stack.push(i); - }); - - stack.pop(); - stack.pop(); - stack.pop(); - - expect(stack.length).toBe((capacity / 2)); -}); From 2451fc90cd9cf973ae4ce40ea246b1950bc8c9e4 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Sun, 17 Sep 2023 15:13:18 +0900 Subject: [PATCH 09/15] =?UTF-8?q?problem-3-1=20=ED=81=90=20=EC=97=B0?= =?UTF-8?q?=EA=B2=B0=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=EB=A1=9C=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3-1/problem-3-1.test.js | 52 ++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/problem-3-1/problem-3-1.test.js b/problem-3-1/problem-3-1.test.js index 0f7a9ee..1aa5b45 100644 --- a/problem-3-1/problem-3-1.test.js +++ b/problem-3-1/problem-3-1.test.js @@ -1,16 +1,46 @@ +class LinkedNode { + item; + + next; + + constructor(item, next) { + this.item = item; + + if (next) { + this.next = next; + } + } +} + class Queue { - #items = []; + #first; + + #last; + + #size = 0; isEmpty() { return this.size() === 0; } size() { - return this.#items.length; + return this.#size; } enqueue(item) { - this.#items.push(item); + const node = new LinkedNode(item); + + if (this.isEmpty()) { + this.#first = node; + this.#last = node; + } else { + const oldLast = this.#last; + + this.#last = node; + oldLast.next = this.#last; + } + + this.#size += 1; } dequeue() { @@ -18,22 +48,24 @@ class Queue { throw new Error('큐가 비어있습니다'); } - const item = this.#items[0]; + const { item } = this.#first; - this.#items = this.#items.slice(1); + this.#first = this.#first.next; + + this.#size -= 1; return item; } [Symbol.iterator]() { - let index = 0; - const data = [...this.#items]; + let current = this.#first; return { next() { - if (index < data.length) { - const value = data[index]; - index += 1; + if (current) { + const value = current.item; + + current = current.next; return { done: false, From d0af676f21a446da0fa1b7739d3075e9f09b8cb7 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Mon, 18 Sep 2023 23:28:21 +0900 Subject: [PATCH 10/15] =?UTF-8?q?refactor:=20problem-1-1=20iterator=20?= =?UTF-8?q?=EC=96=BC=EB=A6=AC=20=EB=A6=AC=ED=84=B4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1-1/problem-1-1.test.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/problem-1-1/problem-1-1.test.js b/problem-1-1/problem-1-1.test.js index 2c6babe..3fe0021 100644 --- a/problem-1-1/problem-1-1.test.js +++ b/problem-1-1/problem-1-1.test.js @@ -19,17 +19,14 @@ class Bag { return { next() { - if (index < data.length) { - const value = data[index]; - index += 1; - - return { - done: false, - value, - }; + if (index >= data.length) { + return { done: true }; } - return { done: true }; + const value = data[index]; + index += 1; + + return { done: false, value }; }, }; } From 0f2c201ce36a0e038112159a6c24a932440448cb Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Mon, 18 Sep 2023 23:31:06 +0900 Subject: [PATCH 11/15] =?UTF-8?q?refactor:=20problem-2-2=20pop=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=82=B4=EB=B6=80=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-2-2/problem-2-2.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problem-2-2/problem-2-2.test.js b/problem-2-2/problem-2-2.test.js index 8f5124f..35cde39 100644 --- a/problem-2-2/problem-2-2.test.js +++ b/problem-2-2/problem-2-2.test.js @@ -32,13 +32,13 @@ class Stack { throw new Error('스택이 비어있습니다'); } - const popped = this.#items[this.#n]; + const item = this.#items[this.#n]; this.#items[this.#n] = undefined; this.#n -= 1; - return popped; + return item; } get length() { From 8e9d17e1b3be4190838a0792e833d163a31d96aa Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Mon, 18 Sep 2023 23:37:06 +0900 Subject: [PATCH 12/15] =?UTF-8?q?refactor:=20problem1-2=20sum=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20operator,=20initialValue=20=ED=8C=8C?= =?UTF-8?q?=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-1-2/problem-1-2.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/problem-1-2/problem-1-2.test.js b/problem-1-2/problem-1-2.test.js index 1ad54ec..2691b65 100644 --- a/problem-1-2/problem-1-2.test.js +++ b/problem-1-2/problem-1-2.test.js @@ -13,8 +13,8 @@ class Bag { this.#items.push(item); } - sum() { - return this.#items.reduce((acc, cur) => acc + cur, 0); + sum(operator = (acc, item) => acc + item, initialValue = 0) { + return this.#items.reduce(operator, initialValue); } [Symbol.iterator]() { @@ -46,7 +46,9 @@ const solution = (numbers) => { scores.add(number); }); - return Math.floor(scores.sum() / scores.size()); + const operator = (acc, item) => acc + item; + + return Math.floor(scores.sum(operator, 0) / scores.size()); }; test('숫자 배열의 평균을 반환한다', () => { From 7f7b7fc769480febf41ae57692829f138697a4c2 Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Mon, 18 Sep 2023 23:50:30 +0900 Subject: [PATCH 13/15] =?UTF-8?q?fix:=20=EB=A7=88=EC=A7=80=EB=A7=89=20?= =?UTF-8?q?=EB=85=B8=EB=93=9C=20dequeue=ED=95=A0=20=EB=95=8C=20last?= =?UTF-8?q?=EC=97=90=20undefined=20=EA=B0=92=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3-1/problem-3-1.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problem-3-1/problem-3-1.test.js b/problem-3-1/problem-3-1.test.js index 1aa5b45..ba99d36 100644 --- a/problem-3-1/problem-3-1.test.js +++ b/problem-3-1/problem-3-1.test.js @@ -54,6 +54,10 @@ class Queue { this.#size -= 1; + if (this.isEmpty()) { + this.#last = undefined; + } + return item; } From 56d2ff8677ac7695af4d83ba2389a6282fc2af0b Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Mon, 18 Sep 2023 23:52:37 +0900 Subject: [PATCH 14/15] =?UTF-8?q?refactor:=20problem-3-1=20iterator=20?= =?UTF-8?q?=EC=96=BC=EB=A6=AC=20=EB=A6=AC=ED=84=B4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problem-3-1/problem-3-1.test.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/problem-3-1/problem-3-1.test.js b/problem-3-1/problem-3-1.test.js index ba99d36..3f57e34 100644 --- a/problem-3-1/problem-3-1.test.js +++ b/problem-3-1/problem-3-1.test.js @@ -66,18 +66,15 @@ class Queue { return { next() { - if (current) { - const value = current.item; + if (!current) { + return { done: true }; + } - current = current.next; + const value = current.item; - return { - done: false, - value, - }; - } + current = current.next; - return { done: true }; + return { done: false, value }; }, }; } From 83bfdffbbdfdc6114abe0c17b059e2696610897c Mon Sep 17 00:00:00 2001 From: seungwoo Kim Date: Tue, 19 Sep 2023 00:15:57 +0900 Subject: [PATCH 15/15] solve problem-3-2 --- problem-3-2/problem-3-2.test.js | 99 +++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/problem-3-2/problem-3-2.test.js b/problem-3-2/problem-3-2.test.js index 21bd836..46076ad 100644 --- a/problem-3-2/problem-3-2.test.js +++ b/problem-3-2/problem-3-2.test.js @@ -1,4 +1,103 @@ +class LinkedNode { + item; + + next = null; + + constructor(item) { + this.item = item; + } +} + +class Queue { + #first; + + #last; + + #size = 0; + + isEmpty() { + return this.size() === 0; + } + + size() { + return this.#size; + } + + enqueue(item) { + const node = new LinkedNode(item); + + const oldLast = this.#last; + + this.#last = node; + + if (this.isEmpty()) { + this.#first = node; + } else { + oldLast.next = this.#last; + } + + this.#size += 1; + } + + dequeue() { + if (this.isEmpty()) { + throw new Error('큐가 비어있습니다'); + } + + const { item } = this.#first; + + this.#first = this.#first.next; + + this.#size -= 1; + + if (this.isEmpty()) { + this.#last = undefined; + } + + return item; + } + + [Symbol.iterator]() { + let current = this.#first; + + return { + next() { + if (!current) { + return { done: true }; + } + + const value = current.item; + + current = current.next; + + return { done: false, value }; + }, + }; + } +} + +const finalPosition = (queue, M) => { + if (queue.size() === 1) { + return queue.dequeue(); + } + + for (let i = 0; i < M - 1; i += 1) { + queue.enqueue(queue.dequeue()); + } + + queue.dequeue(); + + return finalPosition(queue, M); +}; + const solution = (N, M) => { + const queue = new Queue(); + + Array.from({ length: N }, (_, i) => i + 1).forEach((number) => { + queue.enqueue(number); + }); + + return finalPosition(queue, M); }; test('N명의 사람이 있을 때 M번째 사람을 없앨 때 마지막에 죽는 사람의 순서를 반환한다', () => {