-
Notifications
You must be signed in to change notification settings - Fork 25
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
my solution to the exercise #23
base: master
Are you sure you want to change the base?
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,22 @@ | ||
#FIFO | ||
class Queue | ||
def initialize | ||
@store = [] | ||
end | ||
|
||
def dequeue | ||
@store.pop | ||
end | ||
|
||
def enqueue(element) | ||
@store.unshift element | ||
end | ||
|
||
def size | ||
@store.length | ||
end | ||
|
||
def empty? | ||
size == 0 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
#LIFO | ||
class Stack | ||
def initialize | ||
@store = Array.new | ||
end | ||
|
||
def pop | ||
@store.shift | ||
end | ||
|
||
def push(element) | ||
@store << element | ||
end | ||
|
||
def size | ||
@store.length | ||
end | ||
|
||
def empty? | ||
size == 0 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,47 @@ | ||
require './Stack.rb' | ||
require './Queue.rb' | ||
|
||
class Company | ||
MAX_EMPLOYEES = 6 | ||
|
||
attr_reader :waiting, :working | ||
|
||
def initialize | ||
@waiting = Queue.new | ||
10.times { |i| wait! i+1 } | ||
|
||
@working = Stack.new | ||
|
||
end_of_the_month | ||
end | ||
|
||
def end_of_the_month | ||
fire! | ||
hire! | ||
end | ||
|
||
private | ||
|
||
def hire! | ||
until working.size >= MAX_EMPLOYEES | ||
new_hire = waiting.dequeue | ||
working.push new_hire | ||
end | ||
end | ||
|
||
def fire! | ||
return if working.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. This is a great check, but what if it happens a few iterations into the following loop? We aren't checking for it there -- should we? 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'd have to write a test to know for sure. :) But I don't think so, given the limited interface of the class. Only the |
||
number_to_fire.times do | ||
fired_person = working.pop | ||
wait! fired_person | ||
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. Same question here questioning the necessity of fired_person. Maybe we could say |
||
end | ||
end | ||
|
||
def wait!(person) | ||
waiting.enqueue person | ||
end | ||
|
||
def number_to_fire | ||
rand(6) + 1 | ||
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.
Any reason we should temporarily store new_hire? Could we do this
working.push(waiting.dequeue)
instead?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.
We could; it's a personal preference. I call it out (by allocating a local variable) because
.dequeue
mutateswaiting
.