-
Notifications
You must be signed in to change notification settings - Fork 41
Earth - Beauttie #16
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?
Earth - Beauttie #16
Changes from all commits
21c426c
c646a8d
380c38e
a003221
65023cb
059db0b
39843c8
7525c42
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,39 @@ | ||
| 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 | ||
| string.each_char do |char| | ||
| if char == "(" || char == "[" || char == "{" | ||
| stack.push(char) | ||
| elsif char == ")" || char == "]" || char == "}" | ||
| last_pushed = stack.pop() | ||
| if last_pushed == "(" && char != ")" || | ||
| last_pushed == "[" && char != "]" || | ||
| last_pushed == "{" && char != "}" | ||
| return false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # check if stack is full of opening braces | ||
| return stack.empty? | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def evaluate_postfix(postfix_expression) | ||
|
Comment on lines
+24
to
26
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" | ||
| stack = Stack.new | ||
| postfix_expression.each_char do |char| | ||
| if /[0-9]/.match(char) | ||
| stack.push(char.to_i) | ||
| elsif /[\+\-\/\*]/.match(char) | ||
| second_operand = stack.pop() | ||
| first_operand = stack.pop() | ||
| stack.push(first_operand.send(char, second_operand)) | ||
| end | ||
| end | ||
|
|
||
| return stack.pop() | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,57 @@ | ||
| class Queue | ||
|
|
||
| ARRAY_SIZE = 20 | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(ARRAY_SIZE) | ||
| @front = @back = 0 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if self.full? | ||
| raise NoMemoryError, "Array is full" | ||
| else | ||
| @store[@back] = element | ||
| @back = (@back + 1) % ARRAY_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 self.empty? | ||
| raise IndexError, "Array is empty" | ||
| else | ||
| dequeued_element = @store[@front] | ||
| @store[@front] = nil | ||
| @front = (@front + 1) % ARRAY_SIZE | ||
| return dequeued_element | ||
| 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. 👍 nice and compact |
||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front < @back ? @back - @front : @store.length + @back - @front | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == @back | ||
| end | ||
|
|
||
| def full? | ||
|
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 @front == (@back + 1) % ARRAY_SIZE | ||
| end | ||
|
|
||
| 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 | ||
| string = "[" | ||
| i = @front | ||
| until i == @back - 1 | ||
| string << "#{@store[i]}, " | ||
| i = (i + 1) % ARRAY_SIZE | ||
| end | ||
|
|
||
| string << "#{@store[@back - 1]}]" | ||
| return string | ||
| 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.
👍