-
Notifications
You must be signed in to change notification settings - Fork 11
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
김승우 4주차 과제 구현 #2
base: main
Are you sure you want to change the base?
Changes from 10 commits
69caba8
a59592e
c3fa914
3d1f352
f6e3433
4990345
a893efc
b91dccc
90b1555
d7049da
259c4d8
9ef37cf
6dabc2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,95 @@ | ||
class Node { | ||
key; | ||
|
||
item; | ||
|
||
constructor(key, item) { | ||
this.key = key; | ||
this.item = item; | ||
} | ||
} | ||
|
||
class SymbolTable { | ||
#items = []; | ||
|
||
#n = 0; | ||
|
||
size() { | ||
return this.#n; | ||
} | ||
|
||
isEmpty() { | ||
return this.size() === 0; | ||
} | ||
|
||
#findIndex(key) { | ||
return this.#items.findIndex((item) => item.key === key); | ||
} | ||
|
||
get(key) { | ||
const node = this.#items.find((item) => item.key === key); | ||
|
||
return node?.item; | ||
} | ||
|
||
put(key, item) { | ||
const curIndex = this.#findIndex(key); | ||
|
||
if (curIndex >= 0) { | ||
this.#items[curIndex].item = item; | ||
|
||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const curIndex = this.#findIndex(key);
if (curIndex >= 0) {
this.#items[curIndex].item = item;
return;
} 개인적인 의견이지만, 이 사이는 붙이는게 좋은 것 같아요. 바로 위 코드랑 연관이 높고, 나온 값에 대해서 처리를 하는거라서 붙이는게 좋은 것 같아요. 아마 제가 Golang을 사용해서 영향을 받은 것도 있는 것 같기도 하고요. Golang에서는 이런 문법은 많이 쓰거든요. data, err := getSomething
if err == nil {
return
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이것도 제 습관이네요 ㅠ |
||
|
||
const node = new Node(key, item); | ||
|
||
this.#items[this.#n] = node; | ||
|
||
this.#n += 1; | ||
} | ||
|
||
delete(key) { | ||
const index = this.#findIndex(key); | ||
|
||
if (index < 0) { | ||
return; | ||
} | ||
|
||
this.#items.splice(index, 1); | ||
|
||
this.#n -= 1; | ||
} | ||
|
||
contains(key) { | ||
return !!this.get(key); | ||
} | ||
|
||
values() { | ||
return this.#items.map(({ item }) => item); | ||
} | ||
|
||
keys() { | ||
const data = [...this.#items]; | ||
let index = 0; | ||
|
||
return { | ||
[Symbol.iterator]() { | ||
return { | ||
next() { | ||
if (index >= data.length) { | ||
return { done: true }; | ||
} | ||
|
||
const { key } = data[index]; | ||
|
||
index += 1; | ||
|
||
return { done: false, value: key }; | ||
}, | ||
}; | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,18 +63,28 @@ class SymbolTable { | |
this.#n--; | ||
} | ||
|
||
rank(key, start = 0, end = this.#n - 1) { | ||
if (start > end) { | ||
return start; | ||
rank(key) { | ||
let mid = 0; | ||
let start = 0; | ||
let end = this.#n - 1; | ||
|
||
while (start <= end) { | ||
// 가운데 인덱스 | ||
mid = Math.floor((start + end) / 2); | ||
|
||
if (this.#keys[mid] === key) { | ||
return mid; | ||
} | ||
|
||
// 대소 비교로 범위 지정 | ||
if (this.#keys[mid] > key) { | ||
end = mid - 1; | ||
} else { | ||
start = mid + 1; | ||
} | ||
} | ||
|
||
const mid = start + Math.floor((end - start) / 2); | ||
if (key < this.#keys[mid]) { | ||
return this.rank(key, start, mid - 1); | ||
} if (key > this.#keys[mid]) { | ||
return this.rank(key, mid + 1, end); | ||
} | ||
return mid; | ||
return start; | ||
} | ||
|
||
min() { | ||
|
@@ -106,15 +116,54 @@ class SymbolTable { | |
} | ||
|
||
contains(key) { | ||
const i = this.rank(key); | ||
|
||
if (key === this.#keys[i] && i < this.#n) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
floor(key) { | ||
const i = this.rank(key); | ||
|
||
if (key === this.#keys[i]) { | ||
return key; | ||
} | ||
|
||
if (i === 0) { | ||
return; | ||
} | ||
|
||
return this.#keys[i - 1]; | ||
} | ||
|
||
ceiling(key) { | ||
const i = this.rank(key); | ||
|
||
if (i >= this.#n) { | ||
return; | ||
} | ||
|
||
return this.#keys[i]; | ||
} | ||
|
||
keysRange(start, end) { | ||
const i = this.rank(this.ceiling(start)); | ||
const j = this.rank(this.floor(end)); | ||
|
||
if (i > j || i < 0 || j >= this.#n) { | ||
return []; | ||
} | ||
|
||
const keys = []; | ||
|
||
for (let k = i; k <= j; k++) { | ||
keys.push(this.#keys[k]); | ||
} | ||
|
||
return keys; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 주어진 시작 키보다 큰 수 중에 가장 작은 수의 위치, 주어진 끝 키보다 작은 수 중에 가장 큰 키의 위치를 구해서, 해당 구간의 모든 키 목록을 반환하는거군요 ㅎㅎ 기존 메서드를 활용해서 구현해 주셨네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 ㅎㅎ 변수명이 i, j로 작성되어 있어서 가독성이 떨어지네요 ..! |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 더 많이 쓰긴 합니다. 왜냐하면 node라는 변수 혹은 상수를 사용하는 순간
그래서 아예 그런 의도를 차단할려면 변수를 생성하지 않는게 좋을 수도 있습니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요즘 습관적으로 변수 선언해서 값을 받은 다음 리턴하는 구조로 코드를 작성하고 있는데, 말씀해주신 대로 변수를 선언할 경우 다른 사람이 선언한 변수를 이용할 가능성도 있겠네요! 감사합니다