From 642a83f27a84da0abfde70979d2fcfc8803b9690 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Thu, 25 Aug 2016 10:34:55 -0700 Subject: [PATCH 1/4] completed stack.rb implementation --- Stack.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Stack.rb b/Stack.rb index 25da8b60..35766652 100644 --- a/Stack.rb +++ b/Stack.rb @@ -2,19 +2,25 @@ class Stack def initialize @store = Array.new end - + def push(element) + @store.push(element) # same as @store << element + end - + def pop + return @store.pop # .pop works naturally as a stack pop (but not a a queue pop) end def top + return @store.last end - + def size + return @store.length end def empty? + return size == 0 end end From 6a26463707fa7c6cd864d851a9dae848080c88a9 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Thu, 25 Aug 2016 10:43:35 -0700 Subject: [PATCH 2/4] completed queue.rb implementation --- Queue.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Queue.rb b/Queue.rb index 57ef609b..61c265f1 100644 --- a/Queue.rb +++ b/Queue.rb @@ -1,19 +1,25 @@ class Queue def initialize + @store = [] end - + def enqueue(element) + @store.push(element) end - + def dequeue + return @store.delete[0] end def front + return @store[0] end - + def size + return @store.length end def empty? + return size == 0 end end From 856e562ee3129fd90245662f94214f2e2708f338 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Sun, 28 Aug 2016 12:41:39 -0700 Subject: [PATCH 3/4] completed job simulation assignment --- Queue.rb | 5 ++-- Stack.rb | 5 ++-- job-simulation.rb | 65 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/Queue.rb b/Queue.rb index 61c265f1..442c68f5 100644 --- a/Queue.rb +++ b/Queue.rb @@ -1,14 +1,15 @@ class Queue + attr_accessor :enqueue def initialize @store = [] end def enqueue(element) - @store.push(element) + @store << element end def dequeue - return @store.delete[0] + return @store.shift[0] end def front diff --git a/Stack.rb b/Stack.rb index 35766652..6cad5058 100644 --- a/Stack.rb +++ b/Stack.rb @@ -1,11 +1,10 @@ class Stack def initialize - @store = Array.new + @store = [] end def push(element) - @store.push(element) # same as @store << element - + @store << element # same as @store << element end def pop diff --git a/job-simulation.rb b/job-simulation.rb index 2c9c46d5..4535006c 100644 --- a/job-simulation.rb +++ b/job-simulation.rb @@ -1,15 +1,70 @@ # A company has six hiring positions with more people wanting jobs than the # number of available positions. The company managers decide in order to give # more people an opportunity to make money; they will allow people to work in -# three-month intervals. The first five people on the waiting list will be hired -# in the order that they are on the waiting list. The first six people will -# keep these positions for three months. At the end of three months, the -# manager will roll a dice to determine the number of people who will lose their -# jobs. The company will use the policy of last-hired-first-fired. For example, +# three-month intervals. +# +# The first six people on the waiting list will be hired in the order that they are on the waiting list. +# The first six people will keep these positions for three months. +# At the end of three months, the manager will roll a dice to determine the number of people who will lose their jobs. +# The company will use the policy of last-hired-first-fired. +# +# For example, # if the dice roll is 3, the last 3 people hired will lose their jobs to the # first 3 people on the waiting list. People losing their jobs will be placed on # the back of the waiting list in the order that they are fired. This process # will continue for every three-month interval. +# due Sunday night + require './Stack.rb' require './Queue.rb' + +class HiringPolicy + attr_accessor :hire, :waitlist, :hire_fire_roll, :current_hires, :waiting_list, :dice_roll + + def initialize + @current_hires = Stack.new + @waiting_list = Queue.new + @dice_roll = rand(1..6) + end + + def hire(person) + @current_hires.push(person) + end + + def waitlist(person) + @waiting_list.enqueue(person) + end + + def hire_fire_roll + @dice_roll.times do + fired_people = @current_hires.pop + # puts "list of fired people #{fired_people}" # Used to check that everything was working right + @waiting_list.enqueue(fired_people) + end + @dice_roll.times do + hired_people = @waiting_list.dequeue + # puts "list of hired people #{hired_people}" # Used to check that everything was working right + @current_hires.push(hired_people) + end + end +end + +# c = HiringPolicy.new +# +# c.hire("A") +# c.hire("B") +# c.hire("C") +# c.hire("D") +# c.hire("E") +# c.hire("F") +# c.waitlist("G") +# c.waitlist("H") +# c.waitlist("I") +# c.waitlist("J") +# c.waitlist("K") +# c.waitlist("L") +# puts "#{c.dice_roll}" +# c.hire_fire_roll +# puts "current hire: #{c.current_hires.inspect}" +# puts "waiting_list: #{c.waiting_list.inspect}" From b71e546ed12d285cb7721d991dcfbbce227c0eae Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Sun, 28 Aug 2016 12:43:20 -0700 Subject: [PATCH 4/4] completed job simulation and also had to fix a little something --- Queue.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/Queue.rb b/Queue.rb index 442c68f5..cc4821c5 100644 --- a/Queue.rb +++ b/Queue.rb @@ -1,5 +1,4 @@ class Queue - attr_accessor :enqueue def initialize @store = [] end