Generate All Combinations With Maximum Difference Of Consecutive Elements
I would like to generate combinations with length 9 out of a sorted list (length 150) without any repeated values: 24, 110, 157, 256, 278, 368, 416, 502, 593, 666, 751, 801, 827,
Solution 1:
To sort the output you can use python's sorted()
function.
Full code:
import itertools, random
defcombs(nums):
result = set()
for lower in nums:
options = [n for n in nums if lower <= n <= lower + 150]
result.update(itertools.combinations(options, 9))
return result
unsorted = combs([random.randrange(0, 5000) for _ inrange(150)])
# Go through each element in your set and sort it - must convert result to tuple
sorted_set = set()
for elem in unsorted:
elem = tuple(sorted(elem))
sorted_set.add(elem)
Post a Comment for "Generate All Combinations With Maximum Difference Of Consecutive Elements"