You are viewing a single comment's thread. Return to all comments →
I feel like I am missing the point of this exercise with my solution. Can someone please comment on how to make my code more pythonesque and utilize list comprehensions better.
X = int(raw_input())
Y = int(raw_input())
Z = int(raw_input())
N = int(raw_input())
Xi = [x for x in range(X+1)]
Yi = [y for y in range(Y+1)]
Zi = [z for z in range(Z+1)]
results = 
for x in Xi:
for y in Yi:
for z in Zi:
if x + y + z != N:
this is fine.
Xi Yi and Zi are not much of use here. You can make your code smaller
cuboid = 
results = [cuboid.append([x, y, z]) for x in range(X+1) for y in range(Y+1) for z in range(Z+1) if x + y + z != N]
Less lines, more Pythonic
That's really great :)
print ([[a,b,c] for a in range(int(input())+1) for b in range(int(input())+1) for c in range(int(input())+1) if a+b+c!=int(input())])
why does this code not work?
can you explain?
The inputs should be captured before the for loop is executed. In this case, a new input is expected for every iteration of the for loop. Try capturing the inputs first inside some variables and use those variables here. That will work.
You don't even need to have the cuboid list to be honest :)
print ([[a,b,c] for a in range(0,x+1) for b in range(0,y+1) for c in range(0,z+1) if a + b + c != n ])
ha!, I realized that a day later. Thanks for clearing it up though gkeswani92
bro can you please explain the code?