-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Yolotzin #38
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 - Yolotzin #38
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,27 @@ | ||
| require_relative './stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| return false if string.length.odd? | ||
| brackets = {"{" => "}", "[" => "]", "(" => ")"} | ||
| bracket_stack = Stack.new | ||
| # for each char, if bracket, push it into stack | ||
| string.chars.each do |char| | ||
| if brackets[char] | ||
| bracket_stack.push(brackets[char]) | ||
| else # pop last item, see if it matches char, and if not return false | ||
| if bracket_stack.pop != char | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| return bracket_stack.empty? # must be empty to return true | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| # new stack | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,50 @@ | ||
| class Queue | ||
|
|
||
| class Queue # FIFO; Elements added to the rear and removed from front | ||
| # An Abstract Data Type is a data type with a specific public interface, but unspecified implementation. | ||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(20) | ||
| @size = 20 | ||
| @front = 0 | ||
| @back = 0 | ||
|
|
||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def enqueue(element) # add an element to the rear of the queue | ||
|
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. 👍 |
||
| if @front == (@back + 1) % size | ||
| raise ArgumentError, "Queue is full" | ||
| else # move back to next position clockwise | ||
| @store[@back] = element | ||
| @back = (@back + 1) % @size | ||
| end | ||
| end | ||
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def dequeue # Remove & return the element from the front | ||
|
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. 👍 |
||
| if @front == @back | ||
| raise ArgumentError, "Queue is empty" | ||
| else # move front to next position clockwise | ||
| result = @store[@front] | ||
| @front = (@front + 1) % @size | ||
| return result | ||
| end | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store[@front] | ||
| end | ||
|
|
||
| def size | ||
|
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. This would be better to return the number of elements in the Queue rather than the size of the internal buffer. |
||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store.size | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == @back | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| result = [] | ||
| @store.each do |element| | ||
| result << element if element | ||
| end | ||
| return result.to_s | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,19 @@ | ||
| 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" | ||
| return nil if self.empty? | ||
| @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.
👍