Skip to content

Instantly share code, notes, and snippets.

@ajosanchez
Created October 16, 2015 00:06
Show Gist options
  • Save ajosanchez/7dba781fcebaec2e6691 to your computer and use it in GitHub Desktop.
Save ajosanchez/7dba781fcebaec2e6691 to your computer and use it in GitHub Desktop.
cashiers helping a line of customers
def all_times employees
employees.collect {|employee| employee[:time_working]}
end
def helped_by cust_num, employees
employees.each do |employee|
if employee[:cust_helped].include? cust_num
puts "#{employee[:name].capitalize} helped customer ##{cust_num}."
puts "\n"
end
end
end
def sort_cust customers, employees
(0..customers-1).each do |customer|
employees.each do |employee|
if employee[:time_working] == all_times(employees).min
employee[:time_working] += employee[:min_per_cust]
employee[:cust_helped] << customer+1
break
end
end
end
end
def pretty_time minutes
if minutes < 60
mins = minutes
elsif minutes/60 >= 1
hours = (minutes/60).floor
mins = minutes-hours*60
end
time = "#{hours ||= 0} hour(s), #{mins} minutes"
return time
end
#although it's more work, i chose hashes instead of arrays for readability and for practice with hashes :)
employees = [{:name => "mary", :min_per_cust => 6, :time_working => 0, :cust_helped => []},
{:name => "patty", :min_per_cust => 7.5, :time_working => 0, :cust_helped => []},
{:name => "molly", :min_per_cust => 5, :time_working => 0, :cust_helped => []},
{:name => "tom", :min_per_cust => 10, :time_working => 0, :cust_helped => []}
]
print "How many customers are there?: "
customers = gets.chomp.to_i
sort_cust(customers, employees)
puts "\n"
puts "All the customers were helped in #{pretty_time(all_times(employees).max)}"
puts "\n"
if customers > 0
print "Enter a customer's place in line and I will tell you who helped him/her: "
response = gets.chomp.to_i
helped_by(response, employees)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment