Skip to content

Instantly share code, notes, and snippets.

@wallstop
Last active December 17, 2015 06:29
Show Gist options
  • Save wallstop/5565869 to your computer and use it in GitHub Desktop.
Save wallstop/5565869 to your computer and use it in GitHub Desktop.
# Finds the all potential sums for some 3-space configuration of x y z dimensions
# with values corresponding to inputArray, stacked in precedence of x, y, z
def sumFinder(x, y, z, inputArray):
foundZeroSum = False
# Checks all potential sets starting with a single value
for i in range(0, len(inputArray)):
xSum = list()
ySum = list()
zSum = list()
xSum.append(inputArray[i])
ySum.append(inputArray[i])
zSum.append(inputArray[i])
# Walk all three dimensions, adding elements one by one
# X dimension
for j in range(1, x - (i % x + 1)):
if sum(xSum) == 0:
foundZeroSum = True
print(xSum)
xSum.append(inputArray[i + j])
# Y dimension
for j in range(1, y - (int(i / y) + 1)):
# The length check is to ensure no repeat lists of single 0s
if sum(ySum) == 0 and len(ySum) != 1:
foundZeroSum = True
print(ySum)
ySum.append(inputArray[i + x * j])
# Z dimension
for j in range(1, z - (int(i / (x * y)) + 1)):
if sum(zSum) == 0 and len(zSum) != 1:
foundZeroSum = True
print(zSum)
zSum.append(inputArray[i + j * x * y])
# If we haven't found anything, oh well.
if not foundZeroSum:
print("No subsets sum to 0")
# Calling function
def sumCaller():
x = 0
y = 0
z = 0
cubeArray = list()
# Grabs the dimensions from the user
inputString = input("Enter the dimensions: ").split()
x = int(inputString[0])
y = int(inputString[1])
z = int(inputString[2])
# Nicely converts the user's string into an integer array
cubeArray = [int(i) for i in input("Enter the values: ").split()]
# Checks for proper number of values
if len(cubeArray) != x * y * z:
print("Invalid dimensions for input values")
return False
sumFinder(x, y, z, cubeArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment