-
Notifications
You must be signed in to change notification settings - Fork 41
Water - Kareha #14
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?
Water - Kareha #14
Changes from 11 commits
beacb3c
20d05ba
c064951
4fa5a4e
4f94c09
b6ae554
5be74ec
4fb0ae1
d502e58
76b2d01
a7c73b4
5f64bc9
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,13 +1,43 @@ | ||
| require_relative './stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n*m) | ||
| # Space Complexity: O(m), but also potentially O(1) | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| # assumes these are only valid chars | ||
| hash_table = {"}" => "{", "]" => "[", ")" => "("} # space: O(m)? maybe O(1) bc size of this doesnt grow with input? | ||
| stack = Stack.new | ||
|
|
||
| string.each_char do |char| # time: O(n) | ||
| if hash_table.has_value?(char) # time: O(m)? | ||
|
||
| stack.push(char) | ||
| else | ||
| return false if stack.pop != hash_table[char] # time: O(1) | ||
| end | ||
| end | ||
|
|
||
| return stack.empty? | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
|
|
||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(m), but also potentially O(1) | ||
| def evaluate_postfix(postfix_expression) | ||
|
Comment on lines
+22
to
25
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. 👍 , but the space and time complexities are O(n) |
||
| raise NotImplementedError, "Not implemented yet" | ||
| # assumes these are only valid operations | ||
| operations = {"*" => true, "+" => true, "/" => true, "-" => true} # space: O(m), maybe O(1) bc size of this doesnt grow with input? | ||
| stack = Stack.new | ||
| result = "" | ||
|
|
||
| postfix_expression.each_char do |char| # time: O(n) | ||
| if operations[char] # time: O(1) | ||
| num2 = stack.pop.to_i # time: O(1) | ||
| num1 = stack.pop.to_i # time: O(1) | ||
| result = num1.method(char).(num2) | ||
| stack.push(result) # time: O(1), space: O(1)?? because will never be larger than 3 elements | ||
| else | ||
| stack.push(char) # time: O(1) | ||
| end | ||
| end | ||
|
|
||
| return result | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,62 @@ | ||
| class Queue | ||
|
|
||
| SIZE = 20 | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(SIZE) | ||
| @front = @back = -1 | ||
| end | ||
|
|
||
| def 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. 👍 |
||
| raise NotImplementedError, "Not yet implemented" | ||
|
|
||
| if @front == -1 && @back == -1 | ||
| @front = @back = 0 | ||
| elsif (@back + 1) % SIZE == @front | ||
| raise ArgumentError.new("Queue is full") | ||
| elsif @back == SIZE - 1 | ||
| @back = 0 | ||
| else | ||
| @back += 1 | ||
| end | ||
|
|
||
| @store[@back] = element | ||
| end | ||
|
|
||
| def 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. 👍 |
||
| raise NotImplementedError, "Not yet implemented" | ||
| raise ArgumentError, "Queue is empty" if @store.empty? | ||
|
|
||
| result = @store[@front] | ||
| @store[@front] = nil | ||
| if @front == @back | ||
| @front = @back = -1 | ||
| elsif @front == SIZE - 1 | ||
| @front = 0 | ||
| else | ||
| @front += 1 | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store[@front] | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store.select{|x| !x.nil?}.length | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.map{|x| x.nil?}.all? | ||
| end | ||
|
Comment on lines
40
to
50
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. 👍 |
||
|
|
||
| def to_s | ||
|
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. 👍 |
||
| return @store.to_s | ||
| queue = [] | ||
| 20.times do |i| | ||
| index = (i + @front) % SIZE | ||
| queue.push(@store[index]) if @store[index] | ||
| end | ||
|
|
||
| return queue.to_s | ||
| end | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,18 @@ | ||
| class Stack | ||
|
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. 👍 |
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.add_first(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.remove_first | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.empty? | ||
| end | ||
|
|
||
| def to_s | ||
|
|
||
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.
👍 Since you're building a stack it can't be O(1)