Advanced · Comprehensions · 185 of 264
Pascal's triangle
Print the first n rows of Pascal's triangle: each row starts and ends with 1, and each inner number is the sum of the two numbers above it.
Input: n (1 to 20). Output: n rows, numbers separated by spaces.
Examples
Input
5
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Input
1
Output
1
One more test checks other inputs when you press Check.
Stuck? The idea behind comprehensions
A comprehension builds a list in one line: [x * x for x in range(5)]. Add if to keep only some: [x for x in nums if x > 0].
The same works for sets {…} and dictionaries {key: value for …}.
nums = [int(x) for x in input().split()] print([x * x for x in nums if x % 2 == 0])
Watch for: Clear first, short second: when a comprehension needs two ifs and two fors, a loop reads better.