Fix formatting issues for Pascal
This commit is contained in:
parent
86f594cd9a
commit
16a2c25356
1 changed files with 17 additions and 6 deletions
|
|
@ -3,13 +3,26 @@
|
||||||
# Problem 13
|
# Problem 13
|
||||||
|
|
||||||
def createTriangle(numRows):
|
def createTriangle(numRows):
|
||||||
|
triangle = []
|
||||||
|
|
||||||
if numRows < 0:
|
if numRows < 0:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
for i in range(numRows+1):
|
for i in range(numRows+1):
|
||||||
spaces = (" "*(numRows-i))
|
triangle.append(createRow(i))
|
||||||
nums = f"{createRow(i)}"
|
|
||||||
print(spaces + nums)
|
lastRow = triangle[-1]
|
||||||
|
numWidth = len(str(max(lastRow))) + 1
|
||||||
|
triWidth = numWidth * len(lastRow)
|
||||||
|
|
||||||
|
for row in triangle:
|
||||||
|
triStr = ""
|
||||||
|
|
||||||
|
for num in row:
|
||||||
|
numStr = str(num)
|
||||||
|
triStr += numStr + " " * (numWidth - len(numStr))
|
||||||
|
|
||||||
|
print(triStr.center(triWidth))
|
||||||
|
|
||||||
|
|
||||||
def createRow(currentRow):
|
def createRow(currentRow):
|
||||||
|
|
@ -19,9 +32,7 @@ def createRow(currentRow):
|
||||||
coefficient = getbinomial(currentRow, i)
|
coefficient = getbinomial(currentRow, i)
|
||||||
row.append(coefficient)
|
row.append(coefficient)
|
||||||
|
|
||||||
result = " ".join(map(str, row))
|
return row
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
def getbinomial(n, k):
|
def getbinomial(n, k):
|
||||||
numerator = factorial(n)
|
numerator = factorial(n)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue