- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15
2주차 과제 제출 #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
base: main
Are you sure you want to change the base?
2주차 과제 제출 #11
Changes from 8 commits
753ef0e
              cf272fa
              0b1a3cf
              62688be
              7f76aa3
              7ceecf1
              3770590
              4362ba8
              c3a96b9
              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,58 @@ | ||
| class Stack { | ||
| constructor(){ | ||
| this.stack = []; | ||
| } | ||
|  | ||
| isEmpty(){ | ||
| return this.stack.length === 0; | ||
| } | ||
|  | ||
| push(item){ | ||
| this.stack.push(item); | ||
| } | ||
|  | ||
| size(){ | ||
| return this.stack.length; | ||
| } | ||
|  | ||
| pop(){ | ||
| if(this.isEmpty()) throw new Error('스택이 비어있습니다'); | ||
|  | ||
| return this.stack.pop(); | ||
| } | ||
|  | ||
| [Symbol.iterator](){ | ||
| let index = 0, | ||
| stack = [...this.stack]; | ||
| stack = stack.reverse(); | ||
| 
      Comment on lines
    
      +25
     to 
      +27
    
   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. reverse메서드가 원본 배열을 수정하다보니, 스택에 있는 값들을 복사한 후 reverse를 실행하셨군요. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | ||
|  | ||
| return { | ||
| next(){ | ||
| return index < stack.length ? {done : false, value : stack[index++]} : {done : true} | ||
| } | ||
| } | ||
|  | ||
| } | ||
| } | ||
|  | ||
| const solution = (string) => { | ||
| const stack = new Stack(); | ||
| const brackets = { '[': ']', '{': '}', '(': ')' }; | ||
|  | ||
| for (const bracket of string) { | ||
| if(brackets[bracket]){ | ||
| stack.push(bracket); | ||
| }else{ | ||
|  | ||
| if(stack.isEmpty()) return false; | ||
|  | ||
| const savedBrackets = stack.pop(); | ||
|          | ||
| if(brackets[savedBrackets] !== bracket) return false; | ||
| } | ||
| } | ||
|  | ||
| return true; | ||
| 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. 검사를 모두 통과한 경우 true를 반환하고 있습니다. 만약 이런 경우의 인풋이 들어온 경우는 어떻게 될까요? 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. false를 반환해야 하는데 true를 반환하네요 ㅠ | ||
|  | ||
| }; | ||
|  | ||
| test('문자열에 포함된 괄호의 짝이 맞을 때 true를 반환한다', () => { | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,62 @@ | ||
| class Queue { | ||
| constructor(){ | ||
| this.items = []; | ||
| } | ||
|  | ||
| isEmpty(){ | ||
| return this.items.length === 0; | ||
| } | ||
|  | ||
| size(){ | ||
| return this.items.length; | ||
| } | ||
|  | ||
| enqueue(item){ | ||
| this.items.push(item); | ||
| } | ||
|  | ||
| dequeue(){ | ||
| if(this.size() === 0) throw new Error('큐가 비어있습니다'); | ||
|  | ||
| const item = this.items.shift(); | ||
| return item; | ||
| } | ||
|  | ||
| setNumbers(number){ | ||
| for(let i = 1; i <= number; i++){ | ||
| this.enqueue(i); | ||
| } | ||
| } | ||
| 
      Comment on lines
    
      +25
     to 
      +29
    
   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. Queue자료구조에 메서드를 추가하셨네요. 1주차에서 배운 추상 데이터 타입을 떠올리면서, 이 문제를 위한 추상 데이터 타입을 만들어 볼 수도 있을 것 같습니다. class People {
  constructor(peopleCount) {
    this.queue = new Queue();
    for(let i = 1; i <= peopleCount; i++){
      this.queue.enqueue(i);
    }
  }
  lastLivePositionOf(M) {
    while (this.queue.size() > 1) {
      for (let index = 0; index < M - 1; index += 1) {
        const dequeuedNumber = this.queue.dequeue();
        this.queue.enqueue(dequeuedNumber);
      }
      this.queue.dequeue();
    }
  
    return this.queue.dequeue();
  }
}
const solution = (N, M) => {
  const people = new People(N);
  return people.lastLivePositionOf(M);
};이렇게 하면 M번째 사람을 없앨 때 가장 오래 살아남는 사람을 구할 때 내부적으로 어떤 자료구조를 사용하는지 드러나지 않습니다. 도메인으로 기술된 내용이 많이 없어서 표현력을 좋게 못 끌어올렸는데, 최대한 표현해봤습니다 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. 이런.. 문제를 다시 읽어보니 결국 마지막 사람도 죽게 되는 거군요.  | ||
|  | ||
| [Symbol.iterator](){ | ||
| let index = 0; | ||
| const queue = [...this.items]; | ||
|  | ||
| return { | ||
| next() { | ||
| return index < queue.length | ||
| ? { done: false, value: queue[index++] } | ||
| : { done: true }; | ||
| }, | ||
| }; | ||
| } | ||
|  | ||
|  | ||
| } | ||
|  | ||
| const solution = (N, M) => { | ||
| const queue = new Queue(); | ||
| queue.setNumbers(N); | ||
|  | ||
| while (queue.size() > 1) { | ||
| for (let index = 0; index < M - 1; index += 1) { | ||
| const dequeuedNumber = queue.dequeue(); | ||
| queue.enqueue(dequeuedNumber); | ||
| } | ||
| queue.dequeue(); | ||
| } | ||
|  | ||
| return queue.dequeue(); | ||
| }; | ||
|  | ||
| test('N명의 사람이 있을 때 M번째 사람을 없앨 때 마지막에 죽는 사람의 순서를 반환한다', () => { | ||
|  | ||
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.
이 문제가 Bag 자료구조를 사용해서 문제를 해결하는 것이 의도였습니다.