Please check the python code as below , but the code doesn't work properly.Please adjuest it and explain . How to use the condition "lst[i] <= upper bound[i]." ? def bounded_upper_lists(upper_bounds): n=len(upper_bounds) first=n*[0] last=upper_bounds res=[first] while res[-1]!=last: res+=[lex_suc(res[-1])] return res def lex_suc(bitlst): res=bitlst[:] i=len(res)-1 while res[i]==1: res[i]=0 i-=1 res[i]=1 return res upper_bounds=[1,1,2] print(bounded_upper_lists(upper_bounds)) Task: Write a function bounded upper lists(upper bounds) that accepts as 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 list lst, holds for all indices i, lst[i] <= upper bound[i]. Hint: adapt the enumeration algorithm from the lecture that enumerates bitlists in lexicographic order. For example, >>> bounded_lists([1, 1, 2]) [[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]]
Please check the python code as below , but the code doesn't work properly.Please adjuest it and explain .
How to use the condition "lst[i] <= upper bound[i]." ?
def bounded_upper_lists(upper_bounds):
n=len(upper_bounds)
first=n*[0]
last=upper_bounds
res=[first]
while res[-1]!=last:
res+=[lex_suc(res[-1])]
return res
def lex_suc(bitlst):
res=bitlst[:]
i=len(res)-1
while res[i]==1:
res[i]=0
i-=1
res[i]=1
return res
upper_bounds=[1,1,2]
print(bounded_upper_lists(upper_bounds))
Task:
Write a function bounded upper lists(upper bounds) that accepts as 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 list lst, holds for all indices i, lst[i] <= upper bound[i].
Hint: adapt the enumeration
For example,
>>> bounded_lists([1, 1, 2])
[[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]]
Step by step
Solved in 3 steps with 1 images