gpt4 book ai didi

python - 在线计算某个时间范围内的覆盖时间

转载 作者:行者123 更新时间:2023-12-01 07:00:42 26 4
gpt4 key购买 nike

假设我有一个时间范围,它可以用视频表示为开始时间(0秒)和结束时间。例如,对于持续 1 分钟的视频,我的时间范围为 0 秒到 60 秒。

假设我有一个在线流,它向我发送如下时间间隔:

[0, 10]
[8, 14]
[2, 3]
[20, 25]

假设这些间隔代表播放视频所用的时间。我想计算,每次新的间隔到达时,视频不重复播放的有效时间。

应该是这样的:

[0, 10]
#time_played = 10s
[8, 14]
#time_played = 14s because [0,10]+[8,14] -> [0,14] is the effective time interval
[2, 3]
#time_played = 14s because this time interval has already been covered
[20, 25]
#time_played = 19s because [0, 14] + [20, 25] makes 19s the video was effectively played

我找不到解决方案,因为我根本不知道问题定义是什么。您认为使用 python

解决此问题的聪明方法是什么

最佳答案

您可以将传递给数组的所有时间附加到数组中,然后像这样获取数组的长度。

times = []
def get_time(interval = None): # If we don't pass the interval it will return the current time passed
if interval:
for time in range(interval[0], interval[1]):
if time not in times: # Don't append to the array if we already have the time value
times.append(time)
return len(times) # Return the length of the array of times


print(get_time([0, 10]))
#time_played = 10s
print(get_time([8, 14]))
#time_played = 14s because [0,10]+[8,14] -> [0,14] is the effective time interval
print(get_time([2, 3]))
#time_played = 14s because this time interval has already been covered
print(get_time([20, 25]))
#time_played = 19s because [0, 14] + [20, 25] makes 19s the video was effectively played

## Expected output:
## 10
## 14
## 14
## 19

希望这对您有帮助😀

关于python - 在线计算某个时间范围内的覆盖时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58641769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com