Python: find all sets of numbers within a range, where each set contains values ​​that are located at a distance x from each other and do not exceed the range

Given the range, how can I find all sets of size x that match specific criteria that are at least x away from the maximum interval?

If I wanted to find sets of three numbers divided by 5 in the range of 30, I would expect to return these values:

[0,5,10] [0,6,11] [0,7,12] ..etc [1,6,11] [1,7,12] [1,8,13] ..etc 

I looked at itertools, thinking that there would be a quick relaxation, but I do not see a clear method.

I tried to collect all valid number runs by doing something like:

 lst = [(x,x+5,x+10) for x in range(20)] 

But this is only for me + 5 values, not values ​​+6, +7, etc. Remember that my distance requirement is minimal.

Any guidance would be appreciated.

I am new to Python and Stackoverflow, so if my question is distorted or unclear or ridiculous, please calm down - I am ready to clarify or continue the study as necessary; I'm just stuck at the moment. This is similar to what exists, and I just cannot find something that I need to try to generate with nested iterations.

Thanks again!

+5
source share
5 answers
 >>> print [[x,y,z] for x in range(20) for y in range(x+5,20) for z in range(y+5,20)] [[0, 5, 10], [0, 5, 11], [0, 5, 12], [0, 5, 13], ..., [8, 13, 19], [8, 14, 19], [9, 14, 19]] >>> 

Adding

The OP requested a general solution, the simplest form of a general solution is code generation, and this is my attempt to do this

 In [3]: def apart(top, delta, n): vars = ['x%d'%x for x in range(n)] code = '[['+','.join(vars)+'] for '+vars[0]+ ' in range(%d) '%top code = code + ' '.join(['for %s in range(%s+%d,%d)'%(xt,xl,d,t) for d,t in [(delta,top)] for xl,xt in zip(vars[:-1],vars[1:])])+']' return code ...: In [4]: apart(30,5,4) Out[4]: '[[x0,x1,x2,x3] for x0 in range(30) for x1 in range(x0+5,30) for x2 in range(x1+5,30) for x3 in range(x2+5,30)]' In [5]: apart(25,7,2) Out[5]: '[[x0,x1] for x0 in range(25) for x1 in range(x0+7,25)]' In [6]: 
+4
source
 [(x,y,z) for x in range(20) for y in range(20) for z in range(20) if y>=x+5 and z>=y+5] 

Used to post it as a comment

+3
source

You can throw recursion on the task (this can be massaged a little):

 def jump(n, min_gap, right_bound, so_far=None): if so_far is None: so_far = [] if n == 0: yield so_far else: min_x0 = 0 if not so_far else so_far[-1] + min_gap for i in range(min_x0, right_bound): for child in jump(n-1, min_gap, right_bound, so_far+[i]): yield child 

which gives

 >>> pprint.pprint(list(jump(3, 1, 5))) [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]] >>> pprint.pprint(list(jump(3, 2, 5))) [[0, 2, 4]] 

and matches the results of @gboffi:

 >>> list(jump(3, 5, 20)) == [[x,y,z] for x in range(20) for y in range(x+5,20) for z in range(y+5,20)] True >>> list(jump(4,5,30)) == eval(apart(30,5,4)) True 
+3
source

try it

 r=[] for a in range(20): for b in range(20): for c in range(20): if b>=a+5 and c>=a+10 and c>=b+5: r.append((a,b,c)) print(r) 
+1
source
 def intervals(n, min_, max_): if n < 2: yield from ([i] for i in range(min_, max_ + 1)) else: remaining = max_ - ((n - 1) * min_) for i in range(min_, remaining + 1): for item in intervals(n - 1, min_, max_ - i): yield [i] + item def calculate(size, lower, upper, min_interval): for item in intervals(size, min_interval, upper - lower): t = [item[0]] for v in item[1:]: t.append(v + item[-1]) yield t 
0
source

Source: https://habr.com/ru/post/1212205/


All Articles