如何检查包含 6 个项目的列表中是否有 3 个或 4 个或 5 个连续数字?
How to check if there are 3 or 4 or 5 consecutive numbers in a list with 6 items?
如何检查包含 6 个项目的列表中是否有 3 或 4 或 5 或 6 个连续数字?
我正在使用 python
。
我试图生成数字 1-42 的组合。我有超过 500 万种组合。我试图通过删除具有 3-6 个连续数字的组合来减少组合的数量。
给出的列表:
(5,8,12,28,29,30) has 3 consecutive
(1,8,9,10,11,23) has 4 consecutive
(2,12,13,14,15,16) has 5 consecutive
(3,4,5,6,7,8) has 6 consecutive
(3,9,11,14,15,21) has 2 consecutive
(2,5,7,12,21,34)
(3,5,8,10,12,34)
通过删除具有 3-6 个连续数字的列表,输出应为:
(3,9,11,14,15,21)
(2,5,7,12,21,34)
(3,5,8,10,12,34)
这是你要的:
import itertools
def maxRunSize(t):
return 1+max(len(tuple(g)) if k else 0 for k,g in itertools.groupby(zip(t, t[1:]), key=lambda t: t[1]-t[0]==1))
输出:
In [87]: maxRunSize((5,8,12,28,29,30))
Out[87]: 3
In [88]: maxRunSize((1,8,9,10,11,23))
Out[88]: 4
In [89]: maxRunSize((2,12,13,14,14,16))
Out[89]: 3
In [90]: maxRunSize((2,12,13,14,15,16))
Out[90]: 5
In [91]: maxRunSize((3,4,5,6,7,8))
Out[91]: 6
但是,如果您只想生成没有长时间运行的组合(这样您以后就不必过滤掉它们),请查看:
import random
def generate(n, k, window):
"""
Generate n-choose-k combinations in [1,n] such that there is no window-length of consecutive numbers
"""
answer = []
while True:
if len(answer) >= window:
if answer[-window:] == list(range(answer[-window], window+1)):
answer = []
elif len(answer) == k:
yield answer
answer = []
elif len(answer) == window-1:
if answer[1-window:] == list(range(answer[1-window], window)):
answer.append(random.choice([i for i in range(1,n+1) if i!=answer[-1]+1]))
else:
answer.append(random.choice(range(1,n+1)))
如果所有数列恰好有 6 个严格递增的数字,您可以使用快速简单的测试:
if any(x[i]+2 == x[i+2] for i in range(4)):
# ... remove x ...
如何检查包含 6 个项目的列表中是否有 3 或 4 或 5 或 6 个连续数字?
我正在使用 python
。
我试图生成数字 1-42 的组合。我有超过 500 万种组合。我试图通过删除具有 3-6 个连续数字的组合来减少组合的数量。
给出的列表:
(5,8,12,28,29,30) has 3 consecutive
(1,8,9,10,11,23) has 4 consecutive
(2,12,13,14,15,16) has 5 consecutive
(3,4,5,6,7,8) has 6 consecutive
(3,9,11,14,15,21) has 2 consecutive
(2,5,7,12,21,34)
(3,5,8,10,12,34)
通过删除具有 3-6 个连续数字的列表,输出应为:
(3,9,11,14,15,21)
(2,5,7,12,21,34)
(3,5,8,10,12,34)
这是你要的:
import itertools
def maxRunSize(t):
return 1+max(len(tuple(g)) if k else 0 for k,g in itertools.groupby(zip(t, t[1:]), key=lambda t: t[1]-t[0]==1))
输出:
In [87]: maxRunSize((5,8,12,28,29,30))
Out[87]: 3
In [88]: maxRunSize((1,8,9,10,11,23))
Out[88]: 4
In [89]: maxRunSize((2,12,13,14,14,16))
Out[89]: 3
In [90]: maxRunSize((2,12,13,14,15,16))
Out[90]: 5
In [91]: maxRunSize((3,4,5,6,7,8))
Out[91]: 6
但是,如果您只想生成没有长时间运行的组合(这样您以后就不必过滤掉它们),请查看:
import random
def generate(n, k, window):
"""
Generate n-choose-k combinations in [1,n] such that there is no window-length of consecutive numbers
"""
answer = []
while True:
if len(answer) >= window:
if answer[-window:] == list(range(answer[-window], window+1)):
answer = []
elif len(answer) == k:
yield answer
answer = []
elif len(answer) == window-1:
if answer[1-window:] == list(range(answer[1-window], window)):
answer.append(random.choice([i for i in range(1,n+1) if i!=answer[-1]+1]))
else:
answer.append(random.choice(range(1,n+1)))
如果所有数列恰好有 6 个严格递增的数字,您可以使用快速简单的测试:
if any(x[i]+2 == x[i+2] for i in range(4)):
# ... remove x ...