-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Alicia #36
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 - Alicia #36
Changes from all commits
ae7a3a8
1548e46
199e825
4812b34
4ce7855
2fb3a1c
ce56f71
bc6fc45
68cec8c
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,32 @@ | ||
| 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 % 2 != 0 | ||
|
|
||
| struct = Stack.new | ||
| left_half = ["(", "[", "{"] | ||
| right_half = [")", "]", "}"] | ||
|
|
||
| left = 0 | ||
| right = string.length - 1 | ||
| until left >= right | ||
| index = left_half.find_index { |el| el == string[left] } | ||
|
|
||
| if !index.nil? && string[right] != right_half[index] | ||
| return false if string[left + 1] != right_half[index] | ||
| end | ||
|
|
||
| left += 1 | ||
| right -= 1 | ||
| end | ||
|
|
||
| true | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| end | ||
| # def evaluate_postfix(postfix_expression) | ||
| # raise NotImplementedError, "Not implemented yet" | ||
| # end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,50 @@ | ||
| class Queue | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new | ||
| @size = 20 | ||
| @first = 0 | ||
| @last = 0 | ||
| 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" | ||
| return nil if @first == (@last + 1) % @size | ||
|
|
||
| @store[@last] = element | ||
| @last = (@last + 1) % @size | ||
| 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" | ||
| return nil if self.empty? | ||
|
|
||
| element = @store[@first] | ||
| @first = (@first + 1) % size | ||
|
|
||
| return element | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def first | ||
| @store[@first] | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @size | ||
| end | ||
|
Comment on lines
29
to
31
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. Size usually references the number of items in the Queue. |
||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @first == @last | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return nil if self.empty? | ||
|
|
||
| string_queue = Array.new | ||
| current_element = @first | ||
|
|
||
| until current_element == @last | ||
| string_queue << @store[current_element] | ||
| current_element = (current_element + 1) % @size | ||
| end | ||
|
|
||
| return string_queue.to_s | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| require_relative './linked_list.rb' | ||
|
|
||
| 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_last(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if self.empty? | ||
| element = @store.remove_last | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @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.
Did you know, you could also make a hash where the keys are the open parens and the values are the close parens. That way you wouldn't have to check the indexes like you are.