-
Notifications
You must be signed in to change notification settings - Fork 41
Earth - Jasmine #13
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 - Jasmine #13
Changes from all commits
1468eec
c7e0ff5
bce4a94
027cc68
662777f
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,63 @@ | ||
| require_relative './stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) - where n is the number of characters in the string | ||
| # Space Complexity: O(n) - worse case, all characters are openers and we push them all into the stack. | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| brackets = { | ||
| "(" => ")", | ||
| "{" => "}", | ||
| "[" => "]" | ||
| } | ||
|
|
||
| openers = brackets.keys | ||
| closers = brackets.values | ||
|
|
||
| opener_stack = [] | ||
|
|
||
| string.each_char do |char| | ||
| if openers.include?(char) | ||
| opener_stack.push(char) | ||
| end | ||
|
|
||
| if closers.include?(char) | ||
| if opener_stack.empty? | ||
| return false | ||
| else | ||
| removed_opener = opener_stack.pop | ||
| if char != brackets[removed_opener] | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return opener_stack.empty? | ||
|
|
||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) - where n is the number of characters in the expression | ||
| # Space Complexity: O(n) - We're using a stack to store values. | ||
| def evaluate_postfix(postfix_expression) | ||
|
Comment on lines
+38
to
40
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 = [] | ||
| postfix_expression.each_char do |element| | ||
| if element == element.to_i.to_s | ||
| stack.push(element.to_i) | ||
| else | ||
| b = stack.pop | ||
| a = stack.pop | ||
| case element | ||
| when "+" | ||
| stack.push(a + b) | ||
| when "-" | ||
| stack.push(a - b) | ||
| when "*" | ||
| stack.push(a * b) | ||
| when "/" | ||
| stack.push(a / b) | ||
| end | ||
| end | ||
| end | ||
| return stack.pop | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,60 @@ | ||
| class Queue | ||
|
|
||
| MAX_SIZE = 20 | ||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list = Array.new(MAX_SIZE) | ||
| @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 (@back == MAX_SIZE - 1 && @front == 0) || (@back == (@front - 1) % (MAX_SIZE - 1)) | ||
| raise "queue is full" | ||
| elsif @front == -1 # queue is empty? | ||
| @front = 0 | ||
| @back = 1 | ||
| @list[@front] = element | ||
| else | ||
| @list[@back] = element | ||
| @back = (@back + 1) % MAX_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 "queue is empty" | ||
| end | ||
|
|
||
| data = @list[@front] | ||
|
|
||
| if @front == @back # if the queue is now empty | ||
| @front = -1 | ||
| @back = -1 | ||
| else | ||
| @front = (@front + 1) % MAX_SIZE | ||
| end | ||
|
|
||
| return data | ||
| end | ||
|
|
||
| def 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. 👍 |
||
| raise NotImplementedError, "Not yet implemented" | ||
| return @list[@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. 👍 |
||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == -1 ? 0 : @back - @front + 1 | ||
| end | ||
|
|
||
| def empty? | ||
|
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 @front == -1 || @front == @back | ||
| 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 | ||
| if @front <= @back | ||
| return @list.slice(@front...@back).to_s | ||
| else | ||
| return (@list.slice(@front...MAX_SIZE) + @list.slice(0...@back)).to_s | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,18 @@ | ||
| 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" | ||
| @list = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.add_first(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.remove_first | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.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.
👍