I am trying to produce a code that verifies whether or not a user input meets the criteria of a pascal triangle. I know how to go about inputting the number of lines and having it develop a pascal triangle, but I am having trouble figuring out how to get a user to input something like 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
, and having my program say whether it is a pascal triangle or not.
values = input("Enter the numbers: ").split()
pascals_triangle = list(map(list, values))
I know the first line could split the numbers, and the second line would assign the numbers into individual lists of a list. Every time I attempt to have the lists increase by 1 in every row, I get str/int
errors. Once I get past this little road block, I should be able to figure out the rest of the code.
data = input("Enter values: ").split()
def pascal_triangle(data):
size = int(data[0])
n = 2 * size + 1
grid = [[0 for x in range(n)] for y in range(size)]
left = 1
for i in range(size, 0, -1):
grids = data[i].split(' ')
count = 0
for g in grids:
grid[i - 1][left + 2 * count] = int(g)
count += 1
left += 1
if count != i:
return False
left = 1
for i in range(size - 1, -1, -1):
if i == 0:
return grid[i][left] == 1
numbers = i + 1
count = 0
while count < numbers:
current = grid[i][left + count * 2]
upper_left = grid[i - 1][left - 1 + count * 2]
upper_right = grid[i - 1][left + 1 + count * 2]
if current != (upper_left + upper_right):
return False
count += 1
left += 1
return False
status = pascal_triangle(data)
if status:
print('It is a pascal triangle')
else:
print('It is not a pascal triangle')
So, in this code, why am I still not getting the accurate answers?