We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
List Comprehensions
List Comprehensions
Sort by
recency
|
1718 Discussions
|
Please Login in order to post a comment
import itertools if name == 'main': x = int(input()) y = int(input()) z = int(input()) n = int(input()) ls = [] ls1 = [] ls2 = [] for i in range (x + 1) : ls .append (i) for i in range (y + 1) : ls1 .append (i) for i in range (z + 1) : ls2 .append (i) result = list(itertools.product(ls , ls1 , ls2)) filtered_result = [list(x) for x in result if sum(x[i] for i in range (len(x))) != n] print(filtered_result)
Easy solution for the comprehesion list is
if name == 'main': x = int(input()) y = int(input()) z = int(input()) n = int(input()) result = [] for i in range(0,x+1): for j in range(0,y+1): for k in range(0,z+1): if i+j+k != n: result.append([i,j,k]) print(result)
For me it was harder than an easy challenge but it helped me understand the comprehension list by first coding in for loop: