how do i use list comprehensions to print a list of all possible dimensions of a cuboid in python?
Question:
You are given three integers x,y and z representing the dimensions of a cuboid along with an integer n. Print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i+j+k is not equal to n. Here,0<=i<=x; 0<=j<=y;0<=k<=z. Please use list comprehensions rather than multiple loops, as a learning exercise.
I’m unable to solve this problem. Could anyone help me out with it?
Answers:
x, y, z, n = 2, 3, 4, 5
print([(i, j, k) for i in range(x + 1) for j in range(y + 1)
for k in range(z + 1) if i + j + k != n])
Output:
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 4), (0, 3, 0), (0, 3, 1), (0, 3, 3), (0, 3, 4), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 4), (1, 2, 0), (1, 2, 1), (1, 2, 3), (1, 2, 4), (1, 3, 0), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 4), (2, 1, 0), (2, 1, 1), (2, 1, 3), (2, 1, 4), (2, 2, 0), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)]
if __name__ == '__main__':
x, y, z, n = (int(input().strip()) for _ in range(4))
print([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k!=n ])
print([[a, b, c] for a in range(x + 1) for b in range(y + 1) for c in range(z + 1) if a + b + c != n])
if name == ‘main‘:
x=int(input())
y=int(input())
z=int(input())
n=int(input())
ans[]
for i in range(x+1):
for j in range(y+1):
for k in range(z+1):
if(i+j+k)!=n:
ans.append([i,j,k])
print(ans)
If your goal is to print a list of lists of all possible combinations of (i, j, k) for the given x, y, z values where the sum of i + j + k is not equal to n you can try:
print([[i, j, k] for i in range(x + 1) for j in range(y + 1)
for k in range(z + 1) if i + j + k != n])
You are given three integers x,y and z representing the dimensions of a cuboid along with an integer n. Print a list of all possible coordinates given by (i,j,k) on a 3D grid where the sum of i+j+k is not equal to n. Here,0<=i<=x; 0<=j<=y;0<=k<=z. Please use list comprehensions rather than multiple loops, as a learning exercise.
I’m unable to solve this problem. Could anyone help me out with it?
x, y, z, n = 2, 3, 4, 5
print([(i, j, k) for i in range(x + 1) for j in range(y + 1)
for k in range(z + 1) if i + j + k != n])
Output:
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 4), (0, 3, 0), (0, 3, 1), (0, 3, 3), (0, 3, 4), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 4), (1, 2, 0), (1, 2, 1), (1, 2, 3), (1, 2, 4), (1, 3, 0), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 4), (2, 1, 0), (2, 1, 1), (2, 1, 3), (2, 1, 4), (2, 2, 0), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)]
if __name__ == '__main__':
x, y, z, n = (int(input().strip()) for _ in range(4))
print([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k!=n ])
print([[a, b, c] for a in range(x + 1) for b in range(y + 1) for c in range(z + 1) if a + b + c != n])
if name == ‘main‘:
x=int(input())
y=int(input())
z=int(input())
n=int(input())
ans[]
for i in range(x+1):
for j in range(y+1):
for k in range(z+1):
if(i+j+k)!=n:
ans.append([i,j,k])
print(ans)
If your goal is to print a list of lists of all possible combinations of (i, j, k) for the given x, y, z values where the sum of i + j + k is not equal to n you can try:
print([[i, j, k] for i in range(x + 1) for j in range(y + 1)
for k in range(z + 1) if i + j + k != n])