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.
我正在嘗試生成一個代碼,用於驗證用戶輸入是否符合pascal三角形的標准。我知道如何輸入行數並使其形成一個pascal三角形,但我無法弄清楚如何讓用戶輸入類似1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1,並讓我的程序說是否是一個帕斯卡三角形。
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.
我知道第一行可以拆分數字,第二行會將數字分配給列表的各個列表。每次我嘗試讓列表在每一行中增加1時,我都會遇到str / int錯誤。一旦我越過這個小路障,我應該能夠找出其余的代碼。
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?
所以,在這段代碼中,為什么我仍然沒有得到准確的答案?
0
If you're trying to do this in some fancy way, like adapting the grouper
recipe in the itertools
docs to take an iterable of group sizes instead of a fixed group size… take a step back and write the "dumb" version first.—just write a loop.
如果你試圖以某種奇特的方式做到這一點,比如在itertools文檔中調整石斑魚配方來采用可迭代的組大小而不是固定的組大小...退后一步並首先編寫“啞”版本.-只寫一個循環。
First, split the whole string, the same way you split each line in your line-by-line version.
首先,拆分整個字符串,就像在逐行版本中拆分每一行一樣。
One thing: mapping list
over your values won't do any good; that'll just turn, e.g., '23'
into ['2', '3']
, and there's not much good you can do with that. You want a list of numbers, which you're then going to break up into a rows (each row also being a list of numbers—the same row you got by mapping int
over line.split()
in your line-by-line version).
一件事:映射列表超過您的值將不會有任何好處;這只會轉變,例如'23'進入['2','3'],你可以用它做很多好事。你想要一個數字列表,然后你將它們分成一行(每一行也是一個數字列表 - 你通過line.split()在你的逐行映射int得到的同一行版)。
So, here's some pseudocode:
所以,這里有一些偽代碼:
values = input("Enter the numbers: ").split()
nums = [int(value) for value in values]
size = 1
start = 0
while start < len(nums):
rownums = nums[start:start+size]
make sure len(rownums) == size
check rownums the same way you checked each line
update size and start
if you got here without seeing any errors, it's valid
0
One way to do this is to generate each row of Pascal's triangle, and use islice
to grab a list of the current row length from the user data and see if the data matches the row.
一種方法是生成Pascal三角形的每一行,並使用islice從用戶數據中獲取當前行長度的列表,並查看數據是否與行匹配。
from itertools import islice
def pascal():
""" Pascal's triangle generator """
a = [1]
while True:
yield a
#Generate next row from current row
a = [x + y for x, y in zip([0] + a, a + [0])]
def test_pascal(values):
it = map(int, values.split())
ok = True
for row in pascal():
data = list(islice(it, len(row)))
if not data:
break
if data != row:
ok = False
print('bad data', data, row)
break
return ok
# Test
values = '1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1'
print(test_pascal(values))
values = '1 1 1 1 2 1 1 3 3 1 1 4 6 5 1'
print(test_pascal(values))
output
True
bad data [1, 4, 6, 5, 1] [1, 4, 6, 4, 1]
False
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/05/24/720a1158ce8e81f625d862488f484eb8.html。