|
6鱼币
times = [1, 3, 3.5, 6.5, 9.5, 10, 10.8]
names = ["A", "B", "C", "D", "E", "F", "G"]
i = 0
result_times = []
result_names = []
alpha_max = []
alpha_min = []
k = 1
for each in names:
result_names.append(each)
result_times.append(round(k,2))
if i < len(names) - 1:
k = times[i + 1] - times[i]
i += 1
max_times = max(result_times)
min_times = min(result_times)
for a,x in enumerate(result_times):
if x == max_times :
alpha_max.append(names[a])
for a,x in enumerate(result_times):
if x == min_times :
alpha_min.append(names[a])
print("速度最快的是:",alpha_min,"耗费的时间是:",min_times)
print("速度最慢的是:",alpha_max,"耗费的时间是:",max_times)
times = [1, 3, 3.5, 6.5, 9.5, 10, 10.8]
names = ["A", "B", "C", "D", "E", "F", "G"]
max_name = [names[0]]
min_name = [names[0]]
max_time = times[0]
min_time = times[0]
for i in range(1, len(names)):
each_name = names[i]
each_time = times[i] - times[i-1]
if each_time > max_time:
max_name.clear()
max_name.append(each_name)
max_time = each_time
elif each_time == max_time:
max_name.append(each_name)
elif each_time < min_time:
min_name.clear()
min_name.append(each_name)
min_time = each_time
elif each_time == min_time:
min_name.append(each_name)
print(f"速度最快的是:{min_name},耗费时间是:{min_time}")
print(f"速度最慢的是:{max_name},耗费时间是:{max_time}")
任务: 假设有一个密室,每次只能放一个人进去,在进去之前和出来之后都要求摁一下门口的打卡机按钮,打卡机会依次将名字和进出时间戳记录为以下的格式:
times = [1, 3, 3.5, 6.5, 9.5, 10, 10.8]
names = ["A", "B", "C", "D", "E", "F", "G"]
A 君是从时间戳为 0 的时候进入,从时间戳为 1 的时候出来,总共耗时为 1
B 君是从时间戳为 1 的时候进入,从时间戳为 3 的时候出来,总共耗时为 2
C 君是从时间戳为 3 的时候进入,从时间戳为 3.5 的时候出来,总共耗时为 0.5
G 君是从时间戳为 10 的时候进入,从时间戳为 10.8 的时候出来,总共耗时为 0.8
OK,现在要求大家编写代码,统计给定的数据,打印耗时最长和最短的人员名称。
我的问题:
1. 上面的是我的代码,请问下大佬有没有什么bug或者值得改进的地方?
2.下面的是答案的代码,请问和我的有啥区别,我的代码是否也能满足答案的代码的要求?
times = [1, 3, 3.5, 6.5, 9.5, 10, 10.8]
names = ["A", "B", "C", "D", "E", "F", "G"]
times.insert(0,0)
s=[times[x+1]-times[x] for x in range(len(times)-1) ]
def find(lst,x):
ta=[]
tmp=-1
for y in range(lst.count(x)):
tmp=lst.index(x,tmp+1,len(lst))
ta.append(names[tmp])
return ta
print(find(s,min(s)),"用时最少,用时:",min(s))
print(find(s,max(s)),"用时最长,用时:",max(s))
|
|