-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Hala #24
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?
Space - Hala #24
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,66 @@ | ||
| require_relative './stack.rb' | ||
| require_relative 'stack' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(N) | ||
| # Space Complexity: O(N) | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| strStack = Stack.new | ||
| string.each_char do |char| | ||
| if ['(', '[', '{'].include?(char) | ||
| strStack.push(char) | ||
| else | ||
| element = strStack.pop | ||
| if char == ']' && element != '[' | ||
| return false | ||
| elsif char == ')' && element != '(' | ||
| return false | ||
| elsif char == '}' && element != '{' | ||
| return false | ||
| end | ||
|
Comment on lines
+12
to
+18
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 could be dried up using a hash with keys being the open brace and the value being the corresponding close brace. |
||
| end | ||
| end | ||
|
|
||
|
|
||
| return strStack.empty? | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(N) | ||
| # Space Complexity: O(N) | ||
| def evaluate_postfix(postfix_expression) | ||
|
Comment on lines
+26
to
28
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" | ||
| postfix_stack = Stack.new | ||
|
|
||
| postfix_expression.each_char do |char| | ||
| if numeric?(char) | ||
| postfix_stack.push(char.to_i) | ||
| else | ||
|
|
||
| num2 = postfix_stack.pop | ||
| num1 = postfix_stack.pop | ||
|
|
||
| case char | ||
| when "+" | ||
| answer = num1 + num2 | ||
| when "*" | ||
| answer = num1* num2 | ||
| when "-" | ||
| answer = num1- num2 | ||
| when "/" | ||
| answer = num1/ num2 | ||
| end | ||
|
|
||
| if answer== nil | ||
| postfix_stack.push(num2) | ||
| postfix_stack.push(num1) | ||
| else | ||
| postfix_stack.push(answer) | ||
| answer = nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return postfix_stack.pop | ||
|
|
||
| end | ||
|
|
||
| def numeric?(char) | ||
| char =~ /[[:digit:]]/ | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,36 @@ | ||
| require_relative "linked_list" | ||
|
|
||
| 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. This works, but isn't implemented with a circular buffer. So it's not what's asked for. |
||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list = LinkedList.new | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.add_last(element) | ||
| end | ||
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if self.empty? | ||
|
|
||
| element = @list.remove_first | ||
|
|
||
| return element | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.get_first | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.length | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @list.empty? | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return @list.to_s | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,28 @@ | ||
| require_relative "linked_list" | ||
|
|
||
| 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" | ||
| return nil if self.empty? | ||
|
|
||
| element = @list.remove_first | ||
|
|
||
| return element | ||
|
|
||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @list.empty? | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return @list.to_s | ||
| 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.
👍