-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Alex #25
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?
Time - Alex #25
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,13 +1,37 @@ | ||||||
| require_relative './stack.rb' | ||||||
|
|
||||||
| # Time Complexity: ? | ||||||
| # Space Complexity: ? | ||||||
| # Time Complexity: O(n) | ||||||
| # Space Complexity: O(n) | ||||||
| def balanced(string) | ||||||
| raise NotImplementedError, "Not implemented yet" | ||||||
| stack = Stack.new | ||||||
| twosomes = { | ||||||
| "[" => "]", | ||||||
| "(" => ")", | ||||||
| "{" => "}" | ||||||
| } | ||||||
|
|
||||||
| string.each_char do |char| | ||||||
| if twosomes.values.include?(char) | ||||||
|
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. Note about
Suggested change
|
||||||
| return false if twosomes.key(char) != stack.pop | ||||||
| elsif twosomes.key?(char) | ||||||
| stack.push(char) | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| # Time Complexity: ? | ||||||
| # Space Complexity: ? | ||||||
| # Time Complexity: O(n) | ||||||
| # Space Complexity: O(n) | ||||||
| def evaluate_postfix(postfix_expression) | ||||||
| raise NotImplementedError, "Not implemented yet" | ||||||
| postfix_expression.each_char do |char| | ||||||
| if char.match(/\d/) | ||||||
| stack.push(char.to_i) | ||||||
| else | ||||||
| front = stack.pop | ||||||
| back = stack.pop | ||||||
| evaluation = #{front} #{char} #{back} | ||||||
| stack.push(evaluation.to_i) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| return stack.pop | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,67 @@ | ||
| class Queue | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @queue = Array.new | ||
| @size = 20 | ||
| @front = -1 | ||
| @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 | ||
| @front = 0 | ||
| @queue[@front] = element | ||
| @back = 1 | ||
| elsif @front == @back | ||
| raise ArgumentError, "The queue is full; cannot enqueue at this time." | ||
| else | ||
| @queue[@back] = element | ||
| @back = (1 + @back) % @size | ||
| end | ||
| 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" | ||
| if @front == -1 | ||
| raise ArgumentError, "The queue is empty." | ||
| elsif @front == @back | ||
| interim = @queue[@front] | ||
| @front = -1 | ||
| @back = -1 | ||
|
|
||
| return interim | ||
| else | ||
| interim = @queue[@front] | ||
| @front = (1 + @front) % @size | ||
|
|
||
| return interim | ||
| end | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @queue[@front] | ||
| end | ||
|
Comment on lines
40
to
+42
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. You should also check to see if the Queue is empty |
||
|
|
||
| @private | ||
| def get_queue_contents() | ||
| if @queue.empty? | ||
| return [] | ||
| elsif @front > @back | ||
| return @queue[@front...@size] + @queue[0...@back] | ||
| else | ||
| return @queue[@front...@back] | ||
| end | ||
| end | ||
|
Comment on lines
+45
to
53
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 like this one! |
||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| self.get_queue_contents.length | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return self.get_queue_contents.empty? | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return self.get_queue_contents.to_s | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,25 @@ | ||
| 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" | ||
| @stack = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @stack.add_front(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if self.empty? | ||
|
|
||
| element = stack.remove_front | ||
|
|
||
| return element | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @stack.empty? | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return @stack.to_s | ||
| end | ||
| end | ||
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.
👍