-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Sara #21
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 - Sara #21
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,27 @@ | ||
| 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 | ||
| return false if string.length.odd? | ||
| stack = [] | ||
| brackets = { '{' => '}', '[' => ']', '(' => ')' } | ||
|
|
||
| string.each_char do |char| | ||
| if brackets.key?(char) | ||
| stack.push(char) | ||
| elsif brackets.values.include?(char) | ||
| return false if brackets.key(char) != stack.pop | ||
| end | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| if stack.empty? | ||
| return true | ||
| end | ||
| end | ||
|
|
||
| # # Time Complexity: ? | ||
| # # Space Complexity: ? | ||
| # def evaluate_postfix(postfix_expression) | ||
| # raise NotImplementedError, "Not implemented yet" | ||
| # end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,64 @@ | ||
| class Queue | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @size = 20 | ||
| @store = Array.new(@size) | ||
| @front, @rear = -1, -1 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def enqueue(element) #move back to the next free position | ||
|
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. 👍 |
||
| if @front == -1 && @rear -1 #first time | ||
| @front = 0 | ||
| @rear = 1 | ||
| @store[@front] = element | ||
| return element | ||
| end | ||
|
|
||
| if @front == (@rear + 1) % @size #check if is full | ||
| return nil | ||
| else | ||
| @store[@rear] = element #when not full add to the back | ||
| @rear = (@rear + 1) % @size #keeping track of the next free index | ||
| end | ||
| end | ||
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def dequeue #move front one position clockwise | ||
|
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. 👍 |
||
| if @front == -1 && @rear == -1 #first time | ||
| return nil | ||
| end | ||
|
|
||
| #remove first element | ||
| element = @store[@front] | ||
| @front = (@front + 1) % @size | ||
| #after removing check if queue is empty to set front and back -1 | ||
| if @front == @rear | ||
| @front, @rear = -1, -1 | ||
| end | ||
| return element | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if @front == @rear | ||
| 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. 👍 Well done here |
||
| raise NotImplementedError, "Not yet implemented" | ||
| return 0 if @front == @rear #empty | ||
| return @rear - @front + 1 if @rear > @front # @back ahead of @front | ||
| return (@size - @front) + (@rear + 1) | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == @rear | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| return '[]' if @front == @rear | ||
| if @rear > @front | ||
| return @store[@front..@rear - 1].to_s | ||
| else | ||
| return (@store[@front..@size - 1] + @store[0..@rear - 1]).to_s | ||
| end | ||
|
|
||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| 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_first(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if @store.empty? | ||
|
|
||
| item = @store.remove_first | ||
|
|
||
| return item | ||
| 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.