Here's The Program I Want To Do. Program
[x,y,z]
X = The Largest Number Of Position X, By Start From Number Zero
Y = The Largest Number Of Position Y, By Start From Number Zero
Z = The Largest Number Of Position Z, By Start From Number Zero
C = The Sum Number Of Position (x, y, z), It Must Not Equal To C.
CodePudding user response:
You could use the range() function in nested loops to get your number combinations and pick the ones that meet your criteria:
X = 1
Y = 1
Z = 2
C = 3
result = []
for x in range(X 1):
for y in range(Y 1):
for z in range(Z 1):
if x y z != C:
result.append([x,y,z])
print(result)
[[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]]
The number combinations could also be obtained using the product function from itertools. This would allow you to get the result in a list comprehension:
X = 1
Y = 1
Z = 2
C = 3
from itertools import product
result = [ p for p in product(*map(range,(X 1,Y 1,Z 1))) if sum(p) != C]
print(result)
[(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)]
CodePudding user response:
X = int(input("X:"))
Y = int(input("Y:"))
Z = int(input("Z:"))
C = int(input("C:"))
array = []
def add_one(max_array):
for i in range(max_array[0] 1):
if len(max_array) != 1:
for j in add_one(max_array[1:]):
yield [i] j
else:
yield [i]
for i in add_one([X,Y,Z]):
if sum(i) == C:
continue
array.append(i)
print(array)
#input
X:1
Y:1
Z:2
C:3
#output
[[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]]
#input
X:0
Y:2
Z:1
C:2
#output
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 2, 1]]