### 將用戶輸入拆分為增加大小的列表[python]

#### [英]Splitting user input into lists of increasing size [python]

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)
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?

## 2 个解决方案

### #1

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.

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).

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
``````

### #2

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.

``````from itertools import islice

def pascal():
""" Pascal's triangle generator """
a = 
while True:
yield a
#Generate next row from current row
a = [x + y for x, y in zip( + a, a + )]

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
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
``````