-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Denisse #28
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 - Denisse #28
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,68 @@ | ||
| require_relative './stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
|
|
||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| end | ||
| # reference https://www.youtube.com/watch?v=0OJdfXyuapc | ||
| stack = Stack.new | ||
| parens = string.split('') | ||
| i = 0 | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| while i < parens.length | ||
| if (parens[i] == "(" || parens[i] == "{" || parens[i] == "[") | ||
| stack.push(parens[i]) | ||
| elsif (parens[i] == ")" || parens[i] == "}" || parens[i] == "]") | ||
| value = stack.pop | ||
|
|
||
| case parens[i] | ||
| when ")" | ||
| if (value == "{" || value == "[") | ||
| return false | ||
| end | ||
| when "}" | ||
| if (value == "(" || value == "[") | ||
| return false | ||
| end | ||
| when "]" | ||
| if (value == "(" || value == "{") | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| i += 1 | ||
| end | ||
| return stack.empty? ? true : false | ||
| end | ||
|
|
||
| # keeps giving 5 erros :( | ||
| # open_parens = [ '(', '[', '{' ] | ||
| # close_parens = [')', ']', '}' ] | ||
|
|
||
| # check if string length | ||
| # if string.length % 2 != 0 | ||
| # return false | ||
| # # check if string is empty | ||
| # elsif string.length == 0 | ||
| # return true | ||
| # else | ||
| # # check if string starts with a close paren or ends with an open paren (unbalanced) | ||
| # if (close_parens.include? string[0]) || (open_parens.include? string[-1]) | ||
| # return false | ||
| # else | ||
| # stack = Stack.new | ||
| # end | ||
| # end | ||
|
|
||
| # string.each_char do |paren| | ||
| # if open_parens.include? paren | ||
| # stack.push(paren) | ||
| # # if paren is a close paren, remove last open paren in stack and check to ensure it's a match for the close paren paren. | ||
| # elsif close_parens.include? paren | ||
| # last_in_stack = stack.pop() | ||
| # if last_in_stack != open_parens[close_parens.index(paren)] | ||
| # return false | ||
| # end | ||
| # end | ||
| # end | ||
| # return stack.empty? | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,31 +1,117 @@ | ||||||||||||||||||
| class Queue | ||||||||||||||||||
|
|
||||||||||||||||||
| def initialize | ||||||||||||||||||
| # @store = ... | ||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||
| @store = Array.new(20) | ||||||||||||||||||
| @front = -1 | ||||||||||||||||||
| @back = -1 | ||||||||||||||||||
| @size = 20 | ||||||||||||||||||
| # @empty = true | ||||||||||||||||||
| # @front = @back = 0 | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def enqueue(element) | ||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||
| if ((@front == 0 && @back == @size - 1) || (@back == (( @front - 1) % ( @size - 1)))) | ||||||||||||||||||
|
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 think this looks a little simpler
Suggested change
|
||||||||||||||||||
| raise ArgumentError | ||||||||||||||||||
| elsif (@front == - 1) | ||||||||||||||||||
| @front = 0 | ||||||||||||||||||
| @back = 0 | ||||||||||||||||||
| @store[@back] = element | ||||||||||||||||||
| elsif ((@back == @size - 1) && (@front != 0)) | ||||||||||||||||||
| @back = 0 | ||||||||||||||||||
| @store[@back] = element | ||||||||||||||||||
| else | ||||||||||||||||||
| @back = @back + 1 | ||||||||||||||||||
| @store[@back] = element | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| # if @front == @back && !@empty | ||||||||||||||||||
| # raise ArgumentError | ||||||||||||||||||
| # else | ||||||||||||||||||
| # @store[@back] = element | ||||||||||||||||||
| # @empty = false | ||||||||||||||||||
| # @back = (@back + 1) % @store.length | ||||||||||||||||||
| # end | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def dequeue | ||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||
| if (@front == - 1) | ||||||||||||||||||
| raise ArgumentError | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| new_queue = @store[@front] | ||||||||||||||||||
| @store[@front] = nil | ||||||||||||||||||
|
|
||||||||||||||||||
| if (@front == @back) | ||||||||||||||||||
| @front = -1 | ||||||||||||||||||
| @back = -1 | ||||||||||||||||||
| elsif (@front == @size - 1 ) | ||||||||||||||||||
| @front = 0 | ||||||||||||||||||
| else | ||||||||||||||||||
| @front += 1 | ||||||||||||||||||
| end | ||||||||||||||||||
|
Comment on lines
+47
to
+51
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.
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| return new_queue | ||||||||||||||||||
|
|
||||||||||||||||||
| # value = @store[@front] | ||||||||||||||||||
| # @store[@front] = nil | ||||||||||||||||||
| # @front = (@front + 1) % @store.length | ||||||||||||||||||
|
|
||||||||||||||||||
| # if @front == @back | ||||||||||||||||||
| # @empty = true | ||||||||||||||||||
| # end | ||||||||||||||||||
|
|
||||||||||||||||||
| # return value | ||||||||||||||||||
| 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. 👍 |
||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||
| if @back < @front | ||||||||||||||||||
| count = @size - @front + @back + 1 | ||||||||||||||||||
| else | ||||||||||||||||||
| count = @back - @front + 1 | ||||||||||||||||||
| end | ||||||||||||||||||
| return count | ||||||||||||||||||
|
|
||||||||||||||||||
| # if @front < @back | ||||||||||||||||||
| # return @back - @front | ||||||||||||||||||
| # else | ||||||||||||||||||
| # return @back + @store.length - @front | ||||||||||||||||||
| # end | ||||||||||||||||||
| 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) && (@back == - 1)) | ||||||||||||||||||
|
|
||||||||||||||||||
| # return @empty | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| def to_s | ||||||||||||||||||
| return @store.to_s | ||||||||||||||||||
|
|
||||||||||||||||||
| if @front == -1 && @back == -1 | ||||||||||||||||||
| return '[]' | ||||||||||||||||||
| elsif @front <= @back | ||||||||||||||||||
| return @store[@front..@back].to_s | ||||||||||||||||||
| else | ||||||||||||||||||
| i = 0 | ||||||||||||||||||
| index = @front | ||||||||||||||||||
| size = @size - @front + @back + 1 | ||||||||||||||||||
| result = [] | ||||||||||||||||||
|
|
||||||||||||||||||
| while i < size | ||||||||||||||||||
| result << @store[index] | ||||||||||||||||||
| if index < @size - 1 | ||||||||||||||||||
| index += 1 | ||||||||||||||||||
| else | ||||||||||||||||||
| index = 0 | ||||||||||||||||||
| end | ||||||||||||||||||
| i += 1 | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| return result.to_s | ||||||||||||||||||
| end | ||||||||||||||||||
| end | ||||||||||||||||||
| # return @store.to_s | ||||||||||||||||||
| end | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,23 @@ | ||
| 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" | ||
| if @store.nil? | ||
| @store.add_first(element) | ||
| else | ||
| @store.add_last(element) | ||
| end | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.remove_last() unless @store.empty? | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store.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.
👍 Thanks for documenting your source!