pythoncoordinate-systems

Print a list of all possible coordinates given (i, j, k) by on a 3D grid where the sum of i+j+k is not equal to n, using list comprehension


Here is the task I want to solve as a Python Beginner: 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. Please use list comprehensions rather than multiple loops, as a learning exercise.

I also got help of chatGPT and GPT explain each and everything in the task, also provided code, but my concept for all possible coordinates is not clearing, my mind is not satisfied. Maybe reason is, I am beginner to it. If anyone want to explain it to me, I will be thankful to you, or share any resources dedicated to this topic

I know about list comprehension but main problem to possible coordinates and how these works.

There are already asked same question by someone on stackoverflow but everyone just writing code. I need explanation theoretically for possible coordinates

Here is the code:

**x = 1
y = 1
z = 1
n = 2

coordinates = [(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(coordinates)**

and it also works, but as I say I am unable to understand the concept of coordinates combination.


Solution

  • You are trying to find simpler mathematical relationship from what you defined while there is no actual simple relationship beyond what you explained.

    x,y,z and sum of them won't become n.

    x,y,z and the power two of the first and the last parameter becomes twice as the middle.

    x,y,z and x*y*z becomes less than mean of their sum.

    These examples all suggest a straightforward computation: calculating all possible permutations, checking invalid permutations, subtract all invalid permutations from all possible permutations. the best way to find invalid permutations is by computational labor as what chatgpt suggests to you by for loops method. but if we are to take the hard way by finding a relationship in number of invalid permutations, let's see:

    when sum of three parameters ranging from 0 to their actual values results in a certain value(= n), according to stars and bars theorem (help from ChatGPT), all possible permutations become = (x+1)*(y+1)*(z+1), while below Binomial Coefficient (hereafter we refer to it as the original binomial coefficient) calculates all the invalid permutations of x,y,z when their sum becomes k (k = n) when no maximum permutation of a parameter (such as x+1) is out of bound (less than n):

    binomial coefficient

    which can be rewritten by factorial equation of: (k+2)! / (2! * (k+2 - 2)! which again can be simplified as (k+2)*(k+1)/2 .

    we don't have to calculate the binomial coefficient as scipy library can do that for us. if permutations are unbound (meaning x+1,y+1, or z+1 is less than n) so then we should subtract their differential binomial coefficient from the original binomial coefficient. for example, if x+1 < n, a binomial coefficient of difference between x+1 and n should be produced by replacing k in the original binomial coefficient with n-(x+1). then the differential binomial coefficient should be subtracted from the original binomial coefficient. The same goes when y and z are out of bound so now two differential binomial coefficients should be produced and become subtracted from the original binomial coefficient.

    below codes show how to do it. notice if presented number of permutation by the for loops method and binomial coefficient formula were different by one, probably the formula is more accurate (I checked some examples with chatgpt).

    from scipy.special import binom
    
    x = 8
    y = 4
    z = 10
    n = 7
    
    coordinates = [(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('Valid permutations:',len(coordinates))
    
    # if no parameter was out of bound
    if n <= x+1 and n <= y+1 and n <= z+1: 
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2)))
    # if there were only one parameter out of bound
    if n > x+1 and n <= y+1 and n <= z+1: 
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2) - binom(n-(x+1)+2,2)))
    if n <= x+1 and n > y+1 and n <= z+1:                                
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2) - binom(n-(y+1)+2,2)))
    if n <= x+1 and n <= y+1 and n > z+1:                         
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2) - binom(n-(z+1)+2,2)))
        
    # if there were two parameters out of bound
    if n > x+1 and n > y+1 and n <= z+1:                         
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2) - (binom(n-(x+1)+2,2) + binom(n-(y+1)+2,2))  )  )    
    if n > x+1 and n <= y+1 and n > z+1:                         
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2) - (binom(n-(x+1)+2,2) + binom(n-(z+1)+2,2))  )  )    
    if n <= x+1 and n > y+1 and n > z+1:
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2) - (binom(n-(y+1)+2,2) + binom(n-(z+1)+2,2))  )  )    
        
        
    # if all three parameters were out of bound
    if n > x+1 and n > y+1 and n > z+1:
        print((x+1)*(y+1)*(z+1) - (binom(n+2,2) - (  binom(n-(x+1)+2,2) + binom(n-(y+1)+2,2) + binom(n-(z+1)+2,2)  )  ))
    
    print('All permutations:',(x+1)*(y+1)*(z+1))