-
Notifications
You must be signed in to change notification settings - Fork 42
Kelly #10
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?
Kelly #10
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,9 +1,32 @@ | ||
| require_relative './stack.rb' | ||
| require_relative "./stack.rb" | ||
|
|
||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| return true if string == "" | ||
|
|
||
| stack = Stack.new | ||
|
|
||
| string.each_char do |bracket| | ||
| case bracket | ||
| when "(", "{", "[" | ||
| stack.push(bracket) | ||
| when ")" | ||
| if stack.pop != "(" | ||
| return false | ||
| end | ||
| when "}" | ||
| if stack.pop != "{" | ||
| return false | ||
| end | ||
| when "]" | ||
| if stack.pop != "[" | ||
| return false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return stack.empty? | ||
| end | ||
|
|
||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,60 @@ | ||
| class Queue | ||
| QUEUE_SIZE = 20 | ||
|
|
||
| class Queue | ||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(QUEUE_SIZE) | ||
| @front = @rear = -1 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == -1 | ||
| @rear = 1 | ||
| @front = 0 | ||
| @store[@front] = element | ||
| elsif @front == @rear | ||
| raise Error, "Queue full" | ||
| else | ||
| new_rear = (@rear + 1) % QUEUE_SIZE | ||
| @store[@rear] = element | ||
| @rear = new_rear | ||
| end | ||
| end | ||
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| removed = @store[@front] | ||
| if @front + 1 == @rear | ||
| @front = -1 | ||
| @rear = -1 | ||
| else | ||
| @store[@front] = nil | ||
| @front = (@front + 1) % QUEUE_SIZE | ||
| end | ||
| return removed | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @store.empty? | ||
| return -1 | ||
| else | ||
| return @store[@front] | ||
| end | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @store.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. This tells you the size of the internal array, not the size of the queue. |
||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == -1 | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| store_string = [] | ||
| @store.each do |num| | ||
|
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 should start with the front and traverse the array until the rear, including wrapping around the array if needed. |
||
| if num != nil | ||
| store_string << num | ||
| end | ||
| end | ||
| return store_string.to_s | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,27 @@ | ||
| require "linked_list" | ||
|
|
||
| class Stack | ||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @list.add_last(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if self.empty? | ||
|
|
||
| element = @list.remove_last | ||
|
|
||
| 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.
👍