在Python中计算开始时间

huangapple go评论66阅读模式
英文:

Calculate the begin time in Python

问题

我有3个列表,

第一个列表告诉我顾客的名字,

cst = [1,2,3,4,5]

第二个列表告诉我他们的类型,

cst_type = ['A', 'B', 'B', 'A', 'B']

第三个列表告诉我他们的到达时间,

arrived_at = [2,5,7,9,10]

顾客到达后,需要为其提供3分钟的服务。

我想要优先为A类型的顾客提供服务,也就是说,如果A类型的顾客到来,且有人(只有1个人)正在提供服务,或者在有人空闲后,它将优先为A类型的顾客提供服务,否则按照先到先服务的原则进行工作。

如何编写一个简单的Python程序来输出顾客何时开始接受服务?

我知道如果没有优先级,那么可以使用以下代码:

for i in range(1, 5):
    service_begin[i] = max(arrived_at[i], service_end[i-1])

但是如何修改程序以添加优先级呢?

英文:

I have 3 lists,

First list tells me customer name,

cst=[1,2,3,4,5]

Second list tells me its type,

cst_type=['A', 'B', 'B', 'A', 'B']

Third list tell me its arrival,

 arrived_at=[2,5,7,9,10]

After a customer arrives, it needs to be serviced for 3 minutes.

I want to prioritize customers with A type, that is, if A comes and the person(only 1) serving all of them is free or after becoming free, it will give priority to A, otherwise it will work according to FCFS.

How can I make a python program(simple one) to output a list of at what time they begin to be served?

I know if no priority is there then I can use:

For i in range(1,5):
   Service_begin[i]=max(arrival[i], service_end[i-1])

However how to change the program to add priority.

答案1

得分: 0

如果你熟悉数据结构,你可能想尝试使用优先队列。如果不熟悉,那么现在就是学习的时候 在Python中计算开始时间

英文:

If you have familiarity with data structures, you may want to try a priority queue. If not, well, then now it's the time 在Python中计算开始时间

答案2

得分: 0

你可以将其构建为一个模拟,其中你会在一天的分钟中前进,根据优先级从两条车道(A和B)中选择下一个顾客。

def serviceOrder(names, types, times):
    serviceDuration = 3
    laneA  = [i for i,t in enumerate(types) if t=="A"] # "A" 顾客(索引)
    laneB  = [i for i,t in enumerate(types) if t=="B"] # "B" 顾客
    minute = 0 # 当服务员空闲时的当前分钟
    while laneA or laneB:
        # 为所有当前存在/等待的 "A" 顾客提供服务
        while laneA and times[laneA[0]] <= minute:
            index = laneA.pop(0)
            yield minute, names[index], "A"
            minute += serviceDuration
        # 当没有 "A" 可以服务时,选择一个 "B" 顾客
        if laneB and times[laneB[0]] <= minute:
            index  = laneB.pop(0)
            yield minute, names[index], "B"
            minute += serviceDuration
            continue
        # 没有顾客,等待一分钟,然后再次检查
        minute += 1 

注意:这假设时间是按升序排列的。如果不是这种情况,你需要对这三个列表进行排序。

输出:

cst=[1,2,3,4,5]
cst_type=['A', 'B', 'B', 'A', 'B']
arrived_at=[2,5,7,9,10]
for time, name, prio in serviceOrder(cst, cst_type, arrived_at):
    print(time, "...", time+2, "name:", name, prio)
    
# 开始...服务结束的分钟,顾客姓名,类型
# 2 ... 4 姓名:1 A
# 5 ... 7 姓名:2 B
# 8 ... 10 姓名:3 B
# 11 ... 13 姓名:4 A
# 14 ... 16 姓名:5 B

例子中,在先到的B顾客之前为一个A顾客提供服务:

cst=[1,2,3,4,5]
cst_type=['A', 'B', 'A', 'A', 'B']
arrived_at=[2,3,4,9,10]
for time, name, prio in serviceOrder(cst, cst_type, arrived_at):
    print(time, "...", time+2, "name:", name, prio)  

# 开始...服务结束的分钟,顾客姓名,类型    
# 2 ... 4 姓名:1 A
# 5 ... 7 姓名:3 A
# 8 ... 10 姓名:2 B
# 11 ... 13 姓名:4 A
# 14 ... 16 姓名:5 B
英文:

You could build this as a simulation where you progress through the minutes of the day, picking the next customer from two lanes (A and B) based on priorities.

def serviceOrder(names,types,times):
    serviceDuration = 3
    laneA  = [i for i,t in enumerate(types) if t==&quot;A&quot;] # &quot;A&quot; cutomers (indexes)
    laneB  = [i for i,t in enumerate(types) if t==&quot;B&quot;] # &quot;B&quot; cutomers
    minute = 0 # current minute of the day when server is free
    while laneA or laneB:
        # serve all &quot;A&quot; customers currently present/waiting
        while laneA and times[laneA[0]] &lt;= minute:
            index = laneA.pop(0)
            yield minute,names[index],&quot;A&quot;
            minute += serviceDuration
        # when no As to serve, take one B customer
        if laneB and times[laneB[0]] &lt;= minute:
            index  = laneB.pop(0)
            yield minute,names[index],&quot;B&quot;
            minute += serviceDuration
            continue
        # no customers, wait a minute, and check again
        minute += 1 

note: this assumes that times are in increasing order. You'll need to sort the 3 lists if that's not the case.

output:

cst=[1,2,3,4,5]
cst_type=[&#39;A&#39;, &#39;B&#39;, &#39;B&#39;, &#39;A&#39;, &#39;B&#39;]
arrived_at=[2,5,7,9,10]
for time,name,prio in serviceOrder(cst,cst_type,arrived_at):
    print(time,&quot;...&quot;,time+2,&quot;name:&quot;,name,prio)

# start...end minute of service, customer name, type
# 2 ... 4 name: 1 A
# 5 ... 7 name: 2 B
# 8 ... 10 name: 3 B
# 11 ... 13 name: 4 A
# 14 ... 16 name: 5 B

Example with an A customer served before a B customer that arrived earlier:

cst=[1,2,3,4,5]
cst_type=[&#39;A&#39;, &#39;B&#39;, &#39;A&#39;, &#39;A&#39;, &#39;B&#39;]
arrived_at=[2,3,4,9,10]
for time,name,prio in serviceOrder(cst,cst_type,arrived_at):
    print(time,&quot;...&quot;,time+2,&quot;name:&quot;,name,prio)  

# start...end minutes of service, customer name, type    
# 2 ... 4 name: 1 A
# 5 ... 7 name: 3 A
# 8 ... 10 name: 2 B
# 11 ... 13 name: 4 A
# 14 ... 16 name: 5 B

huangapple
  • 本文由 发表于 2023年2月9日 02:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定