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

Lynn Trickey's Stacks, Queues and Job Simulation #46

Open
wants to merge 3 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
5 changes: 5 additions & 0 deletions lib/Stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ def initialize
end

def push(element)
@store << element
end

def pop
@store.pop
end

def top
@store.last
end

def size
@store.length
end

def empty?
@store.empty?
end

def to_s
Expand Down
43 changes: 35 additions & 8 deletions lib/job-simulation.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
require './Stack.rb'
require './Queue.rb'
require_relative 'Stack'
require_relative 'Queue'

class JobSimulation
attr_reader :workers, :waiting, :roll
attr_reader :employed, :waiting, :waitlist

Choose a reason for hiding this comment

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

You shouldn't change the variables that I put here. If this is the spec that's given to you then these are the names of the variables that will be expected in other places, so if you change it here, other programs expecting these names won't work properly. Also no adding variables (waitlist) or removing variables (roll)


def initialize (jobs_available, job_seekers)
#job_seekers must be larger than jobs_available
@waiting = job_seekers - jobs_available
give_jobs(jobs_available)
create_wait_list
end

def give_jobs(jobs_available)
@number = 1

Choose a reason for hiding this comment

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

It's fine to make this variable and use it. But if doing so, it should be a local variable, not an instance variable, as it won't be (shouldn't be) used in other methods as well.

@employed = Stack.new
jobs_available.times do
@employed.push("Worker \##{@number}")
@number += 1
end

Choose a reason for hiding this comment

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

Another solution that's a little easier to read is:

jobs_available.times do |i|
  @employed.push("Worker \##{i+1}")
end

@employed
end

Choose a reason for hiding this comment

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

It's fine to break out this task into its own method - but if doing so, it should be a private method that is used within the class, but not available outside the class (which is what making it private does)


def create_wait_list
@waitlist = Queue.new
@waiting.times do
@waitlist.enqueue("Worker \##{@number}")
@number += 1
end
@waitlist

Choose a reason for hiding this comment

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

same note about style of loop and using |i| here. Though this one is a little trickier to make sure that the numbering would start at 7, in this case, instead of 1. But it can be done with a little math.

end

def cycle

def cycle
die_roll = rand(1..6)

Choose a reason for hiding this comment

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

This should be @roll as it's the variable that I indicated in the atrr_reader

die_roll.times {@waitlist.enqueue(@employed.pop)}
die_roll.times {@employed.push(@waitlist.dequeue)}
end

end

## Allows us to run our code and see what's happening:
sim = JobSimulation.new(6,10)

puts "------------------------------"
puts "Before the simulation starts"
puts "Employed: #{sim.workers}"
puts "Waitlist: #{sim.waiting}"
puts "Employed: #{sim.employed}"
puts "Waitlist: #{sim.waitlist}"
puts "------------------------------"
print "<enter to cycle>\n"

count = 0
until gets.chomp != ""
puts "-------Cycle #{count+=1}-------"
sim.cycle
puts "Employed: #{sim.workers}"
puts "Waitlist: #{sim.waiting}"
puts "Employed: #{sim.employed}"
puts "Waitlist: #{sim.waitlist}"
end
14 changes: 7 additions & 7 deletions specs/test-queue-implementation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
end

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

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

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

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

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

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

it "returns the front element in the Queue" do
skip
# skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand Down
9 changes: 2 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,7 @@
end

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

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