-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Olga #15
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 - Olga #15
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,52 @@ | ||
| require_relative './stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) - goes through all input elements | ||
| # Space Complexity: O(n) - stack will take at least half of symbols in string | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| return true if string.length == 0 | ||
|
|
||
| pairs = {"(" => ")", "{" => "}", "[" => "]"} | ||
|
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 love the use of a hash here! |
||
| expectations = Stack.new | ||
| string.chars.each do |char| | ||
| if pairs[char] | ||
| expected_element = pairs[char] | ||
| expectations.push(expected_element) | ||
| else | ||
| if expectations.pop != char | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| return expectations.empty? | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) - goes through all input elements | ||
| # Space Complexity: O(1) - stack will never have more than 2 elements | ||
| def evaluate_postfix(postfix_expression) | ||
|
Comment on lines
+23
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. 👍 |
||
| raise NotImplementedError, "Not implemented yet" | ||
| end | ||
| operands = Stack.new | ||
| postfix_expression.chars.each do |element| | ||
| if element.to_i.to_s == element | ||
| operands.push(element.to_i) | ||
| else | ||
| num2 = operands.pop | ||
| num1 = operands.pop | ||
| case element | ||
| when "+" | ||
| answer = num1 + num2 | ||
| when "*" | ||
| answer = num1 * num2 | ||
| when "-" | ||
| answer = num1 - num2 | ||
| when "/" | ||
| answer = num1 / num2 | ||
| end | ||
| operands.push(answer) | ||
| end | ||
| end | ||
| result = operands.pop | ||
| if operands.empty? | ||
| return result | ||
| else | ||
| raise Exception.new "Expression is not valid" | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,54 @@ | ||
| class 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. 👍 , Well done, good work implementing a circular buffer! |
||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new | ||
| @size = 20 | ||
| @start = 0 | ||
| @end = 0 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # check if we can add more elements | ||
| raise Exception.new "No vacant spots in queue" if @size < self.size | ||
|
|
||
| @store[@end] = element | ||
| @end = (@end + 1) % @size | ||
| end | ||
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| raise Exception.new "No elements in queue" if self.size == 0 | ||
| element = @store[@start] | ||
| @store[@start] = nil | ||
| @start = (@start + 1) % @size | ||
| return element | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| raise Exception.new "No elements in queue" if self.size == 0 | ||
| @store[@start] | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @end == @start && @store[@start] | ||
| return 20 | ||
| elsif @end > @start | ||
| return @end - @start | ||
| elsif @end < @start | ||
| return @end + @size - @start | ||
| else | ||
| return 0 | ||
| end | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @end == @start | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| if @end > @start | ||
| return @store[@start..@end].to_s | ||
| else | ||
| return (@store[@start..@size-1] + @store[0..@end-1]).to_s | ||
| end | ||
| 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.
👍