I've taken 1 course in Java and 1 in C++ and now I'm tinkering with Python (love it so far). My module I'm currently working on is designed to solve a certain puzzle. To understand the code you'll need to understand the puzzle:

There is an upside down triangle with n amount of numbers on the top row. For example, if n were 3 the triangle would look like this:

_ _ _

_ _

_

There are a total of 6 spaces in this example. The puzzle is to find all the possible solutions so that the difference between any 2 numbers above 1 number is equal to the number below. Below is an example of one particular solution. In this example, we can use the numbers 1 - 6 since there are 6 spaces. Note that we can say "3 - 2 = 1" and also "2 - 3 = 1" for this puzzle (just take the absolute value of the difference):

6 2 5

4 3

1

This is my solution. My code attempts to find all possible solutions, but I have hit a roadblock. I can't remember my Grade 12 statistics anymore and am having trouble testing all the possible combinations of the puzzle. I'm getting confused as to how to do this. For example, I understand that in this puzzle order matters, so I would say "6 choose 6" which is "6!" or 720 different combinations. I don't know how to systematically rotate through all of them. I'll post my code below. Any help is greatly appreciated.

```
"""Currently this module contains functions which solve
the upside down triangle puzzle thing."""
def triangle(toprow):
"""This function solves all possible answers for the
funny upside down triangle puzzle thing from calculus."""
print("You entered a puzzle with " + str(toprow) + " spaces on the top row.\n")
# Find the total number of numbers to be used in the puzzle
total = toprow
for i in range(0, toprow):
total = total + i
# State how many possibilities there are for the puzzle
if (toprow > 17):
print("There are A LOT of possibilities. Hopefully the program doesn't crash...\n")
elif (toprow > 4):
print("There are " + "%e" % npr(total) + " possibilities to test.\n")
else:
print("There are " + str(npr(total)) + " possibilities to test.\n")
# Apply the initial numbers to the triangle
tri = []
line = []
count = total
for k in range(0, toprow):
line = []
for l in range(0, (toprow - k)):
line.append(count)
count = count - 1
tri.append(line)
print("The initial triangle is: " + str(tri) + "\n")
# The array tri is correct up to here
# Ex. triangle(3) gives tri = [[6, 5, 4], [3, 2], [1]]
# Do all the possible arrangements of the numbers. Ugh.
# Currently this is not working. I need help here!
sols = []
for m in range(0, toprow):
for n in range(0, (toprow - m)):
for o in range(m, toprow):
for p in range(n, (toprow - o)):
print("Testing triangle " + str(tri) + "...")
if (test(tri)):
sols.append(tri)
print("The triangle " + str(tri) + " is a solution.")
temp = tri[o][n]
if (o == (toprow - 1)):
tri[o][n] = tri[m][n]
tri[m][n] = temp
elif (n == (toprow - o - 1)):
tri[o][n] = tri[(o + 1)][0]
tri[(o + 1)][0] = temp
else:
tri[o][n] = tri[o][(n + 1)]
tri[o][(n + 1)] = temp
return sols
def test(arr):
"""This complimentary function tests the array to see
if it is a solution."""
count = len(arr) - 1
# Test if the difference between two tops is equal to
# their corresponding bottom (directly below the pair)
for i in range(count):
for j in range(count - i):
if (((arr[i][j] - arr[i][(j + 1)]) != arr[(i + 1)][j]) and ((arr[i][(j + 1)] - arr[i][j]) != arr[(i + 1)][j])):
return 0
return 1
def npr(n):
"""This function determines how many possible arrangements
of the numbers there are (nPr from Math 12 statistics)."""
# Do n factorial (there is a math module function but ya)
for i in range(1, n):
n = n * i
return n
def triprint(arr):
"""This function prints the triangle."""
count = len(arr)
# Print the numbers
for i in range(0, count):
print(str(arr[i]))
```