-
Notifications
You must be signed in to change notification settings - Fork 6
Ringo - Earth #3
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: master
Are you sure you want to change the base?
Changes from all commits
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,17 +1,61 @@ | ||
| const Stack = require('./stack'); | ||
| /* | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n), the function has to visit every node once | ||
| but operations performed in that loop are O(1) | ||
| Space Complexity: O(n), worst case scenario the stack could have on it | ||
| every character in the string | ||
| */ | ||
| const balanced = (str) => { | ||
| throw new Error("This method has not been implemented!"); | ||
| const stack = new Stack(); | ||
| const pairs = { '{': '}', '[': ']', '(': ')' } | ||
|
|
||
| for (const char of str) { | ||
| if (pairs[char]) { | ||
| stack.push(char); | ||
| } else { | ||
| const top = stack.pop(); | ||
| if (!(pairs[top] === char)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return stack.isEmpty(); | ||
| } | ||
|
|
||
| /* | ||
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| Time Complexity: O(n), the function has to check every character in expr, | ||
| but operations in the for loop are O(1) | ||
| Space Complexity: O(n), the controlling factor is the height of the stack, | ||
| which does not grow quickly with longer expr in a normal use case but I suppose | ||
| in a worst case scenario with some weird postfix expressions would increase in size | ||
| linear to the length of the weird expression you were using | ||
| */ | ||
| const evaluatePostfix = (expr) => { | ||
|
Comment on lines
+27
to
34
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. 👍 |
||
| throw new Error("This method has not been implemented!"); | ||
| const stack = new Stack(); | ||
|
|
||
| for (const char of expr) { | ||
| const current = parseInt(char); | ||
| if (Number.isInteger(current)) { | ||
| stack.push(current); | ||
| } else { | ||
| const second = stack.pop(); | ||
| const first = stack.pop(); | ||
|
|
||
| if (char === '+') { | ||
| stack.push(first + second); | ||
| } else if (char === '-') { | ||
| stack.push(first - second); | ||
| } else if (char === '*') { | ||
| stack.push(first * second); | ||
| } else if (char === '/') { | ||
| const quotient = first / second; | ||
| quotient > 0 ? stack.push(Math.floor(quotient)) : stack.push(Math.ceil(quotient)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return stack.pop(); | ||
| } | ||
|
|
||
| exports.balanced = balanced; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,58 @@ | ||
| const QUEUE_SIZE = 20; | ||
|
|
||
| class Queue { | ||
| constructor() { | ||
| // this.store = ... | ||
| throw new Error("This method has not been implemented!"); | ||
| this.store = new Array(QUEUE_SIZE); | ||
| this.head = 0; | ||
| this.tail = 0; | ||
| } | ||
|
|
||
| enqueue(element) { | ||
|
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. 👍 Nice work, very compact |
||
| throw new Error("This method has not been implemented!"); | ||
| if (this.isFull()) { | ||
| throw new Error("your queue is full"); | ||
| } else { | ||
| this.store[this.tail] = element; | ||
| this.tail = (this.tail + 1) % QUEUE_SIZE; | ||
| } | ||
| } | ||
|
|
||
| dequeue() { | ||
|
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. 👍 Very compact! However what happens if you dequeue an empty queue? |
||
| throw new Error("This method has not been implemented!"); | ||
| const element = this.store[this.head]; | ||
| this.store[this.head] = null; | ||
| this.head = (this.head + 1) % QUEUE_SIZE; | ||
| return element; | ||
| } | ||
|
|
||
| front() { | ||
| throw new Error("This method has not been implemented!"); | ||
| return this.store[this.head]; | ||
| } | ||
|
|
||
| size() { | ||
| throw new Error("This method has not been implemented!"); | ||
| let count = 0; | ||
| let pointer = this.head; | ||
|
|
||
| while (this.store[pointer]) { | ||
| count++; | ||
| pointer = (pointer + 1) % QUEUE_SIZE; | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| isEmpty() { | ||
| throw new Error("This method has not been implemented!"); | ||
| return this.head === this.tail; | ||
| } | ||
|
|
||
| isFull() { | ||
| return this.head === (this.tail + 1) % QUEUE_SIZE; | ||
| } | ||
|
|
||
| toString() { | ||
| let arr; | ||
| if (this.head > this.tail) { | ||
| arr = this.store.slice(this.head, this.capacity).concat(this.store.slice(0, this.tail)); | ||
| } else { | ||
| arr = this.store | ||
| arr = this.store; | ||
| } | ||
| return JSON.stringify(arr.filter((v) => v !== null)); | ||
| } | ||
|
|
||
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.
👍