I'm trying to build a function that:
- accepts as an argument a list of positive integers of length n and
- returns a list of all lists of length n consisting of non-negative integers with the following property:
- for a list
lst
it holds that for all indices i,lst[i] ≤ upper bound[i]
- for a list
For example, if the input list was [1, 1, 2]
, then the output would be
[ [ 0 , 0 , 0 ] ,
[ 0 , 0 , 1 ] ,
[ 0 , 0 , 2 ] ,
[ 0 , 1 , 0 ] ,
[ 0 , 1 , 1 ] ,
[ 0 , 1 , 2 ] ,
[ 1 , 0 , 0 ] ,
[ 1 , 0 , 1 ] ,
[ 1 , 0 , 2 ] ,
[ 1 , 1 , 0 ] ,
[ 1 , 1 , 1 ] ,
[ 1 , 1 , 2 ] , ]
This is how far I've gotten:
def bounded_list(ub):
f = len(ub) * [0]
l = ub
res = [f]
while res[-1] != l:
res += [lex_suc1(res[-1], ub)]
return res
def lex_suc1(lst, ub):
res = lst[:]
i = len(res) - 1
while res[i] == ub[i]:
res[i] = 0
i -= 1
res[i] = ub[i]
return res
which gives the output:
[[0, 0, 0],
[0, 0, 2],
[0, 1, 0],
[0, 1, 2],
[1, 0, 0],
[1, 0, 2],
[1, 1, 0],
[1, 1, 2]]
I'm not able to understand how to include the missing lists, any help woul be great.