-
Notifications
You must be signed in to change notification settings - Fork 44
Space - Jessica #34
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 - Jessica #34
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,31 +1,66 @@ | ||||||||||||||||||||||||||
| class Queue | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def initialize | ||||||||||||||||||||||||||
| # @store = ... | ||||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||||
| @store = [] | ||||||||||||||||||||||||||
| @max_size = 20 | ||||||||||||||||||||||||||
| @front = -1 | ||||||||||||||||||||||||||
| @rear = -1 | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def enqueue(element) | ||||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||||
| if (@front == 0 && @rear == (@max_size - 1)) || | ||||||||||||||||||||||||||
| (@rear == ((@front - 1) % (@max_size - 1))) | ||||||||||||||||||||||||||
|
Comment on lines
+11
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.
Suggested change
|
||||||||||||||||||||||||||
| raise "No more space" | ||||||||||||||||||||||||||
| elsif @front == -1 # Queue is empty | ||||||||||||||||||||||||||
| # initialize front and rear | ||||||||||||||||||||||||||
| @front += 1 | ||||||||||||||||||||||||||
| @rear += 1 | ||||||||||||||||||||||||||
| elsif (@rear == (@max_size - 1) && @front != 0) # rear needs to wrap around | ||||||||||||||||||||||||||
| @rear = 0 | ||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||
| @rear += 1 | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
Comment on lines
+14
to
+22
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 can be dried up with something like
Suggested change
|
||||||||||||||||||||||||||
| @store[@rear] = element | ||||||||||||||||||||||||||
| 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 @front == -1 | ||||||||||||||||||||||||||
| raise "Nothing to remove" | ||||||||||||||||||||||||||
| elsif @front == @rear | ||||||||||||||||||||||||||
| dequeue = front | ||||||||||||||||||||||||||
| @front = -1 | ||||||||||||||||||||||||||
| @rear = -1 | ||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||
| dequeue = front | ||||||||||||||||||||||||||
| @front += 1 | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| return dequeue | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def front | ||||||||||||||||||||||||||
|
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 @store[@front] if @front != -1 | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # get how many elements are in the array | ||||||||||||||||||||||||||
| 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. 👍 , Nicely done |
||||||||||||||||||||||||||
| raise NotImplementedError, "Not yet implemented" | ||||||||||||||||||||||||||
| if @rear >= @front | ||||||||||||||||||||||||||
| return (@rear - @front) + 1 | ||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||
| return (@max_size) - (@front - @rear) + 1 | ||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def to_s | ||||||||||||||||||||||||||
| return @store.to_s | ||||||||||||||||||||||||||
| if @front <= @rear | ||||||||||||||||||||||||||
| return @store[@front, @max_size].to_s | ||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||
| arr = @store[@front, @max_size] | ||||||||||||||||||||||||||
| arr += @store[0.. @rear] | ||||||||||||||||||||||||||
| return arr.to_s | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,18 @@ | ||
| 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" | ||
| @store.remove_first | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @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.
Just a note that you can dry this up with a hash where they keys are the close braces and the values are the corresponding open braces.