Skip to content
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

Queues - Cara Comfort - stacks-queues #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/Queue.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
class Queue
def initialize
@store = Array.new
end

def enqueue(element)
@store << element
end

def dequeue
@store.shift
end

def front
@store.first
end

def size
@store.length
end

def empty?
@store.empty?
end

def to_s
return @store.to_s
end
end
10 changes: 9 additions & 1 deletion lib/Stack.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
class Stack
def initialize
@store = Array.new
@store = Array.new # use an array to store the data
end

def push(element)
@store << element
end

def pop
raise ArgumentError.new("Cannot pop an empty stack") if @store.empty? #OR self.empty?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good!

@store.pop
end

def top
raise ArgumentError.new("There's no top in an empty stack") if self.size == 0
@store.last
end

def size
@store.length
end

def empty?
#@store.size == 0
@store.empty?
end

def to_s
Expand Down
42 changes: 42 additions & 0 deletions lib/job-simulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,52 @@ class JobSimulation
attr_reader :workers, :waiting, :roll

def initialize (jobs_available, job_seekers)
# assume jobs_available and job_seekers are ints
# assume always more job_seekers than jobs_available
raise ArgumentError.new("The economy is bad. There will always be more job seekers than jobs.") if jobs_available > job_seekers

@workers = Stack.new
(1..jobs_available).each do |worker|
@workers.push("Worker ##{worker}")
end

@waiting = Queue.new
# seekers_left = job_seekers - jobs_available
((jobs_available + 1)..job_seekers).each do |worker|
@waiting.enqueue("Worker ##{worker}")
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! nice!


@roll

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this as it's not needed here

end

def cycle
roll

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put the rolling method body here - it's only one line, so no need for a new method for it

puts "Managers roll a #{@roll}"

# fire workers specified by the roll, removing them from the hired list
# and adding them to the waiting list
(1..@roll).each do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would read cleaner if you did @roll.times instead

fired_worker = @workers.pop
puts "Fire: #{fired_worker}"
@waiting.enqueue(fired_worker)
end

# hire workers specified by the roll, removing them from the waiting list
# and adding them to the hired list
(1..@roll).each do
hired_worker = @waiting.dequeue
puts "Hire: #{hired_worker}"
@workers.push(hired_worker)
end

end

private

def roll
# Assume managers rolling a 6 sided die
@roll = rand(1..6)
end
end

## Allows us to run our code and see what's happening:
Expand All @@ -29,3 +69,5 @@ def cycle
puts "Employed: #{sim.workers}"
puts "Waitlist: #{sim.waiting}"
end

# Code would probably be better organized by making hired and fired private methods
7 changes: 0 additions & 7 deletions specs/test-queue-implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
end

it "adds something to an empty Queue" do
skip
q = Queue.new
q.enqueue(10)
q.to_s.must_equal "[10]"
end

it "adds multiple somethings to a Queue" do
skip
q = Queue.new
q.enqueue(10)
q.enqueue(20)
Expand All @@ -26,14 +24,12 @@
end

it "starts the size of a Queue at 0" do
skip
q = Queue.new
q.size.must_equal 0
q.empty?.must_equal true
end

it "removes something from the Queue" do
skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -43,7 +39,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -55,7 +50,6 @@
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
q = Queue.new
q.empty?.must_equal true
q.enqueue(-1)
Expand All @@ -70,7 +64,6 @@
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand Down
34 changes: 27 additions & 7 deletions specs/test-stack-implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
end

it "pushes something onto a empty Stack" do
skip
s = Stack.new
s.push(10)
s.to_s.must_equal "[10]"
end

it "pushes multiple somethings onto a Stack" do
skip
s = Stack.new
s.push(10)
s.push(20)
Expand All @@ -26,14 +24,12 @@
end

it "starts the size of a Stack at 0" do
skip
s = Stack.new
s.size.must_equal 0
s.empty?.must_equal true
end

it "removes something from the stack" do
skip
s = Stack.new
s.push(5)
removed = s.pop
Expand All @@ -43,7 +39,6 @@
end

it "removes the right something (LIFO)" do
skip
s = Stack.new
s.push(5)
s.push(3)
Expand All @@ -55,7 +50,6 @@
end

it "properly adjusts the size with pushing and poping" do
skip
s = Stack.new
s.empty?.must_equal true
s.push(-1)
Expand All @@ -70,13 +64,39 @@
end

it "returns the top element on the stack" do
skip
s = Stack.new
s.push(40)
s.push(22)
s.top.must_equal 22
end

it "doesn't alter the size when you call top" do
s = Stack.new
s.push(8)
s.top
s.size.must_equal 1
end

it "raises an error if you try to pop from an empty stack" do
s = Stack.new
s.empty?.must_equal true

proc {
s.pop
}.must_raise ArgumentError

end

it "raises an error if you try to call top on an empty stack" do
s = Stack.new
s.empty?.must_equal true

proc {
s.top
}.must_raise ArgumentError
end


# Challege Tests you could write yourself:
# it "doesn't alter the size when you call top" do
# it "raises an error if you try to pop from an empty stack" do
Expand Down