I have the following recursive generator that gives each combination of numbers from 0to top-1:
def f(width, top):
if width == 0:
yield []
else:
for v in range(top):
for subResult in f(width - 1, top):
yield [ v ] + subResult
If called as f(3, 3), it gives values
[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2],
[0, 2, 0], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 1], [1, 0, 2],
[1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2],
[2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2],
[2, 2, 0], [2, 2, 1], [2, 2, 2]
(Try to name it as list(f(3,3))to get them as a list.)
What I need to get is the same values in a different order: I want the values to be sorted by their maximum value, i. e. first the value [0, 0, 0], then all the values that have 1at most, i. e. [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], ..., then those that contain a 2, i. e. [0, 0, 2], [0, 1, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2], [2, 0, 0], ...etc.
(), , f(4, 1000), ( , ).
, , f(w, 0), f(w, 1), f(w, 2) , , , :
def g(width, top):
for t in range(top):
for v in f(width, t+1):
if t in v:
yield v
?