0

The problem is: Let's learn about list comprehensions! 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,g,k) on a 3D grid where the sum of i+j+k is not equal to n Please use list comprehensions rather than multiple loops, as a learning exercise.

Example
x = 1
y = 1
z = 2
n = 3

All permutations of [i,j,k] are:
.
[[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]]
Print an array of the elements that do not sum to**n = 3**.
[[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,2]]

My programm is:


if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    arr = [0, 0, 0] 
    res = []
    while arr[0] != x or arr[1] != y or arr[2] != z + 1:
        if arr[2] > z:
            arr[2] = 0
            arr[1] = arr[1] + 1
        if arr[1] > y:
            arr[1] = 0
            arr[0] = arr[0] + 1
        if arr[0] > x:
            arr[0] = arr[0] - 1                
        if arr[0] + arr[1] + arr[2] != n:
            res.append(arr)
            print(res)
        arr[2] = arr[2] + 1 


Input (stdin)

1
1
1
2

Your Output (stdout)
[[0, 0, 0]]
[[0, 0, 1], [0, 0, 1]]
[[0, 1, 0], [0, 1, 0], [0, 1, 0]]
[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]

Expected Output
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

  • Just to point out that you're not using list comprehensions as you said you were asked to. https://www.freecodecamp.org/news/list-comprehension-in-python/ – bigkeefer Feb 25 '23 at 10:14
  • Try something like `res.append(arr[:])`, which will push a *copy* of `arr` into `res` each time, rather than a reference to `arr` – Nick Feb 25 '23 at 10:59

0 Answers0