-
Notifications
You must be signed in to change notification settings - Fork 41
Earth/Taylor #22
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/Taylor #22
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,40 @@ | ||
| require_relative './stack.rb' | ||
| require_relative './Stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(nlogn) | ||
| # Space Complexity: O(n) | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| stack = Stack.new | ||
| arr = string.split('') | ||
| chars = ['(', '[', '{'] | ||
| arr.each do |char| | ||
| if chars.include?(char) | ||
| stack.push(char) | ||
| else | ||
|
Comment on lines
+10
to
+12
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. Could you do this with a hash where the keys are the open brace and he values are the matching close brace? That might help you dry up those if-else statements. |
||
| current = stack.pop | ||
| if current == '(' && char != ')' | ||
| return false | ||
| elsif current == '[' && char != ']' | ||
| return false | ||
| elsif current == '{' && char != '}' | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| return stack.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. 👍 Interesting use of eval |
||
| raise NotImplementedError, "Not implemented yet" | ||
| stack = Stack.new | ||
| postfix_expression.each_char do |char| | ||
| if char.match(/[0-9]/) | ||
| stack.push(char.to_i) | ||
| else | ||
| a = stack.pop | ||
| b = stack.pop | ||
| stack.push(eval "#{b}#{char}#{a}") | ||
| end | ||
| end | ||
| return stack.pop | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,65 @@ | ||
| 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! |
||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| attr_accessor :start, :size, :start, :end | ||
|
|
||
| def initialize(size=20) | ||
| @store = Array.new(size) | ||
| @size = size | ||
| @start, @end = -1, -1 | ||
| end | ||
|
|
||
| def full? | ||
| (@end + 1) % @size == @start | ||
| 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" | ||
| # raise ArgumentError.new("Queue full") if self.full? | ||
|
|
||
| if self.empty? | ||
| @start = 0 | ||
| @end = 1 | ||
| @store[@start] = element | ||
| elsif @start == @end | ||
| raise ArgumentError.new("Queue full!") | ||
| else | ||
| @store[@end] = element | ||
| @end = (@end + 1) % @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 @start == -1 | ||
| raise ArgumentError.new("Queue empty") | ||
| else | ||
| element = @store[@start] | ||
| @start = (@start + 1) % @size | ||
| if @start == @end | ||
| @start, @end = -1 | ||
| end | ||
| end | ||
| return element | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| raise ArgumentError.new("Queue empty") if @start == -1 | ||
| @start | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @size | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @start == -1 | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| arr = [] | ||
| current = @start | ||
| while current != @end | ||
| arr << @store[current] | ||
| current = (current+1) % size | ||
| end | ||
| return arr.to_s | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,24 @@ | ||
| 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" | ||
| @store = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.add_last(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.remove_last if [email protected]? | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store.empty? | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.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.
Since you have to go through all the chars and you're not splitting at each stage, this is O(n) for time complexity.