ruby -计算超市队列中的时间

e5nszbig  于 2023-01-25  发布在  Ruby
关注(0)|答案(5)|浏览(148)

假设在超市里有一个排队等待自助结账的队伍,我正试着写一个函数来计算所有顾客结账所需的总时间!
输入:
customers:代表队列的正整数数组。2每个整数代表一个客户,其值是他们结账所需的时间。
n:正整数,收银台的数量。
输出:
函数应该返回一个整数,即所需的总时间。例如:

queue_time([5,3,4], 1)
# should return 12
# because when n=1, the total time is just the sum of the times

queue_time([10,2,3,3], 2)
# should return 10
# because here n=2 and the 2nd, 3rd, and 4th people in the 
# queue finish before the 1st person has finished.

queue_time([2,3,10], 2)
# should return 12

只有一个队列服务于多个钱柜,队列的顺序永远不会改变。队列中的第一个人(数组/列表中的第一个元素)在钱柜空闲时立即进入钱柜。我试过这个,但它不能正常工作,我不知道如何在钱柜打开时让下一个人进入。

def queue_time(customers, n)
  if customers == []
    n=0
  else
    x= customers.reduce(:+) / n 
    if x < customers.max
      customers.max
    else 
      x
    end
  end
end

例如,测试

customers = [751, 304, 2, 629, 36, 674, 1] 
n = 2
expected: 1461, instead got: 1198

谢谢:-)

btxsgosb

btxsgosb1#

给定输入:

customers = [751, 304, 2, 629, 36, 674, 1]
n = 2

您可以创建一个tils数组:(每个都是数组本身)

tills = Array.new(n) { [] }
#=> [[], []]

现在,对于每个客户,将其值添加到最短钱柜中:

customers.each do |customer|
  tills.min_by(&:sum) << customer
end

最后,这将为您提供:

tills
#=> [[751, 36, 674], [304, 2, 629, 1]]

第一个总数是1461。

v7pvogib

v7pvogib2#

您的问题福尔斯离散事件模拟的范畴(DES)建模。对于简单的模型,DES有时可以通过循环和条件来处理(如所提供的其他答案),但如果您计划扩展此模型或构建更复杂的模拟,您将需要使用Winter Simulation Conference tutorial中描述的某种事件调度框架。一个名为simplekit的gem提供了教程中概念的Ruby实现(完全公开-我是本文和gem的作者)。
一个简短的总结是,一个“事件”发生在一个离散的时间点,并更新系统状态。它也可以调度进一步的事件。DES框架维护一组挂起的事件作为优先级队列,以便事件以正确的顺序发生,而不管事件被调度和执行之间的时间延迟。所有这些都由框架提供的方法相对透明地处理。
下面是模型的一个实现,它使用了不同的参数化,并进行了大量注解:

require 'simplekit'

class MyModel
  include SimpleKit

  # Constructor - initializes the model parameters.
  # param: service_times - An array of service times for each customer
  # param: max_servers - The total number of servers in the system.
  def initialize(service_times, max_servers)
    @service_times = service_times.clone
    @max_servers = max_servers
  end

  # Initialize the model state and schedule an initial set of events.
  def init
    @num_available_servers = @max_servers
    # As long as servers remain available and there are customers
    # remaining, schedule the end_service for the next customer
    # to occur after a delay equal to the customer's service time.
    # The number of available servers gets reduced by one.
    while @num_available_servers > 0 && !@service_times.empty? do
      schedule(:end_service, @service_times.shift)
      @num_available_servers -= 1
    end
  end

  # Every time there's an end of service, see if there's another customer
  # waiting in line.  If so, schedule their end_service (and the current
  # server remains tied up), otherwise the current server gets freed up.
  #
  # This model will terminate when there are no more events scheduled,
  # which happens when all the end_services have completed.
  def end_service
    if @service_times.empty?
      @num_available_servers += 1
    else
      schedule(:end_service, @service_times.shift)
    end
  end
end

model_params = [
  [[5,3,4], 1],
  [[10,2,3,3], 2],
  [[2,3,10], 2],
  [[2,3,4,1,2,5], 3],
  [[751, 304, 2, 629, 36, 674, 1], 2]
]

# SimpleKit models track and update the model_time automatically for you,
# so the current_model's model_time reflects what time it was in the
# model when the last event occurred.
model_params.each do |svc_times, servers|
  current_model = MyModel.new(svc_times, servers)
  current_model.run
  puts "#{svc_times}, #{servers} => #{current_model.model_time}"
end

其产生以下输出:

[5, 3, 4], 1 => 12.0
[10, 2, 3, 3], 2 => 10.0
[2, 3, 10], 2 => 12.0
[2, 3, 4, 1, 2, 5], 3 => 8.0
[751, 304, 2, 629, 36, 674, 1], 2 => 1461.0
goqiplq2

goqiplq23#

def queue_time(time_required, n)
  remaining_time_by_line = Array.new(n) { 0 }
  curr_time = 0
  time_required.each do |t|
    wait_time, line_assigned = remaining_time_by_line.each_with_index.min_by(&:first)
    curr_time += wait_time
    remaining_time_by_line.map! { |rt| rt - wait_time }
    remaining_time_by_line[line_assigned] = t
  end
  curr_time + remaining_time_by_line.max
end
queue_time([5,3,4], 1)
  #=> 12
queue_time([10,2,3,3], 2)
  #=> 10
queue_time([2,3,10], 2)
  #=> 12 
queue_time([2,3,4,1,2,5], 3)
  #=> 8
queue_time([751, 304, 2, 629, 36, 674, 1], 2)
  #=> 1461

我可以通过在添加puts语句之后执行该方法来最容易地解释计算。
一个二个一个一个
将显示以下内容。

time required, t = 2
line_assigned = 0
wait_time = 0
curr_time = 0
remaining_times_by_line = [2, 0, 0]
time required, t = 3
line_assigned = 1
wait_time = 0
curr_time = 0
remaining_times_by_line = [2, 3, 0]
time required, t = 4
line_assigned = 2
wait_time = 0
curr_time = 0
remaining_times_by_line = [2, 3, 4]
time required, t = 1
line_assigned = 0
wait_time = 2
curr_time = 2
remaining_times_by_line = [1, 1, 2]
time required, t = 2
line_assigned = 0
wait_time = 1
curr_time = 3
remaining_times_by_line = [2, 0, 1]
time required, t = 5
line_assigned = 1
wait_time = 0
curr_time = 3
remaining_times_by_line = [2, 5, 1]
remaining_time_by_line.max = 5
curr_time + remaining_time_by_line.max = 8
ruoxqz4g

ruoxqz4g4#

本质上与answer of Stefan相同,不同之处在于,这个答案只跟踪每个收银台的总和,而不是跟踪客户,并在每次新客户到来时重新求和。

def queue_time(customers, n)
  till  = Struct.new(:sum)
  tills = Array.new(n) { till.new(0) }

  customers.each do |customer| 
    tills.min_by(&:sum).sum += customer
  end

  tills.map(&:sum).max
end
f87krz0w

f87krz0w5#

我真的很喜欢Stefan的答案,这个答案的工作原理也差不多,只是没有使用嵌套数组。
首先创建一个tils数组,每个tils的值为0。

tills = [0] * 3

然后,迭代customer数组,将每个customer项添加到具有最小值的till项,该项通过引用最小值的索引找到。

customers.each { |customer| till[till.index(till.min)] += customer }

并隐式返回具有最大值的till项。

till.max

完整代码:

def queue_time(customers, n)
  till = [0] * n
  customers.each { |customer| till[till.index(till.min)] += customer }
  till.max
end

相关问题