<\/span><\/h2>\n\n\n\nLet’s learn about list comprehensions! You are given three integers x<\/em><\/strong>, y<\/em><\/strong> and z<\/em><\/strong> representing the dimensions of a cuboid along with an integer n<\/em><\/strong>. Print a list of all possible coordinates given by (i<\/strong><\/em>, j<\/strong><\/em>, k<\/strong><\/em>) on a 3D grid where the sum of i<\/strong><\/em> + j<\/strong><\/em> + k<\/strong><\/em> is not equal to n<\/em><\/strong>. Here, 0<\/strong> <= i<\/em><\/strong> <= x<\/em>; 0 <= j<\/em> <= y<\/em>; 0 <= k<\/em> <= z<\/em><\/strong>. Please use list comprehensions rather than multiple loops, as a learning exercise.<\/p>\n\n\n\nExample<\/strong><\/p>\n\n\n\nx = 1<\/strong>
y = 1<\/strong>
z = 2<\/strong>
n = 3 <\/strong><\/p>\n\n\n\nAll permutations of [i<\/strong><\/em>, j<\/strong><\/em>, k<\/strong><\/em>] are:
[<\/strong>[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]]<\/strong>.<\/p>\n\n\n\nPrint an array of the elements that do not sum to n<\/strong><\/em> =<\/em><\/strong> 3<\/em><\/strong>.
[<\/strong>[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]]<\/strong><\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nFour integers x<\/em><\/strong>, y<\/strong><\/em>, z<\/strong><\/em> and n<\/em><\/strong>, each on a separate line.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\nPrint the list in lexicographic increasing order.<\/p>\n\n\n\n
Sample Input 0<\/strong><\/p>\n\n\n\n1\n1\n1\n2<\/code><\/pre>\n\n\n\nSample Output 0<\/strong><\/p>\n\n\n\n[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]<\/code><\/pre>\n\n\n\nExplanation 0<\/strong><\/p>\n\n\n\nEach variable x<\/em><\/strong>, y<\/strong><\/em> and z<\/em><\/strong> will have values of 0<\/strong> or 1<\/strong>. All permutations of lists in the form [i<\/strong><\/em>, j<\/strong><\/em>, k<\/strong><\/em>] = [<\/strong>[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]<\/strong>.<\/p>\n\n\n\nRemove all arrays that sum to n<\/em> = 2<\/strong> to leave only the valid permutations.<\/p>\n\n\n\nSample Input 1<\/strong><\/p>\n\n\n\n2\n2\n2\n2<\/code><\/pre>\n\n\n\nSample Output 1<\/strong><\/p>\n\n\n\n[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 2], [0, 2, 1], [0, 2, 2], [1, 0, 0], [1, 0, 2], [1, 1, 1], [1, 1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2, 0, 1], [2, 0, 2], [2, 1, 0], [2, 1, 1], [2, 1, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]]<\/code><\/pre>\n\n\n\n<\/span>Solution – List Comprehensions in Python – Hacker Rank Solution <\/strong><\/span><\/h2>\n\n\n\nif __name__ == '__main__':\n x = int(input())\n y = int(input())\n z = int(input())\n n = int(input())\n output = [];\n abc = [];\n for X in range(x+1):\n for Y in range(y+1):\n for Z in range(z+1):\n if X+Y+Z != n:\n abc = [X,Y,Z];\n output.append(abc);\nprint(output); <\/pre>\n\n\n\n