Recursive Pascals Triangle Layout
So i've managed to get Pascals Triangle to print successfully in terms of what numbers are printed, however, i can't get the formatting correct using: n = int(input('Enter value of
Solution 1:
You want to use the str.join()
function, which prints out all elements in a list separated by a string:
>>>L = printPascal(6)>>>for row in L:...print' '.join(map(str, row))...
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
' '.join(list)
means you're printing out every element in a list separated by a space (' '
).
However, every element in the list needs to be a string in order for the join
function to work. Yours are integers. To fix this, I've changed all the integers to strings by doing map(str, row)
. This is equivalent to:
new_list = []
for item in row:
new_list.append(str(item))
Or as a list comprehension:
[str(item) for item in row]
Post a Comment for "Recursive Pascals Triangle Layout"