用户活动的二进制序列[关闭]

问题描述 投票:-1回答:2

我有一个用户活动期的二进制序列。当他在这一天没有活动时有一个“0”而他当时是“1”。我想证明他是否每周活动不少于两次,所以在序列的每7个位置必须至少有两个。任何人都可以帮助我实现这个想法吗?

python binary
2个回答
1
投票

它也适用于字符串或列表:

days="110000000000110011"
#days=['1', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '1', '1']

for d in range(0,len(days),7): 
    print(d//7,days[d:d+7].count("1"))

#In the output, the first number is the week number:
0 2
1 2
2 2

0
投票

在您的情况下,您可以添加这些值并检查sum是否大于或等于2。

这是代码:

activity_list = [0, 0, 1, 1, 0, 0, 0]

def is_active(activity_list: list, minimum_active_days: int):
    return sum(activity_list) >= minimum_active_days

print(is_active(activity_list, 2))
>>> True

回答评论:

如果列表长于7,我该怎么办?我想知道在每7个条目中,和是大于还是大于2?

然后,您应该对列表进行切片以获取要检查的值。

# some_long_list - stores more than 7 values
for i in range(len(some_long_list) -7):
    print(f"Is active in period: from {i}, to {i+7}")
    print(is_active(some_long_list[i:i+7], 2))
© www.soinside.com 2019 - 2024. All rights reserved.