megaapps
asked on
How to write a pascals triangle in python using lists
Hi I need to know how to write a pascal triangle in python with a height parameter . So far all I have is the following code below. Or can someone explain how to get it.
def pascalsTri():
userInput = raw_input("Please enter height")
height = int(userInput)
triangle = []
for row in range(0,height+1):
triangle.append(row)
ASKER
Here are the directions of the assignment. I just don't know how to write it in code.
Take the input of the height from the user
Assign a list to be empty to represent the entire triangle.
For each row of the triangle from 0 to the height+1
Assign a list to be empty to represent one new row of the triangle.
If the new row is at a height of zero,
make the new row to be a [1].
Else…if
the new row is at height of one, make the new row to be [1, 1].
Else..
for each column in the row from 0 to the current height of the row plus one.
If the column of this row is 0,
append a 1 to the new row
Else…if the column of this row is equal to the height of the row,
append a 1 to the new row
Else…
the value for this position are stored in the triangle, the value stored at
one row up and one column back plus the value stored at one row up
and the same column.
Append this value to the new row.
After the if statements are finished, append the new row list to the triangle list.
When the outermost for loop is complete, print the triangle list
Take the input of the height from the user
Assign a list to be empty to represent the entire triangle.
For each row of the triangle from 0 to the height+1
Assign a list to be empty to represent one new row of the triangle.
If the new row is at a height of zero,
make the new row to be a [1].
Else…if
the new row is at height of one, make the new row to be [1, 1].
Else..
for each column in the row from 0 to the current height of the row plus one.
If the column of this row is 0,
append a 1 to the new row
Else…if the column of this row is equal to the height of the row,
append a 1 to the new row
Else…
the value for this position are stored in the triangle, the value stored at
one row up and one column back plus the value stored at one row up
and the same column.
Append this value to the new row.
After the if statements are finished, append the new row list to the triangle list.
When the outermost for loop is complete, print the triangle list
I would have implemented the code differently, but they seem to dictated what
you have to do.
Well here some more suggestions:
Well, they really dictate how to do your work:
So what I would do is
triangle = []
for row_number in range(0,height+1):
new_row = mk_row(triangle, row_number)
triangle.append(new_row)
then write the funtion mk_row(), (you have t write it in the lines before the above for loop)
def mk_row(triangle, row_number):
#If the new row is at a height of zero,
if row_number == 0:
row = [ 1 ]
elif row_number == 1:
#Else…if
#the new row is at height of one, make the new row to be [1, 1].
else:
#Else..
row = []
#for each column in the row from 0 to the current height of the row plus one.
#If the column of this row is 0,
#append a 1 to the new row
# Else…if the column of this row is equal to the height of the row,
#append a 1 to the new row
#Else…
# the value for this position are stored in the triangle, the value stored at
#one row up and one column back plus the value stored at one row up
# and the same column.
value = # what they told you above
# Append this value to the new row.
return row
you have to do.
Well here some more suggestions:
Well, they really dictate how to do your work:
So what I would do is
triangle = []
for row_number in range(0,height+1):
new_row = mk_row(triangle, row_number)
triangle.append(new_row)
then write the funtion mk_row(), (you have t write it in the lines before the above for loop)
def mk_row(triangle, row_number):
#If the new row is at a height of zero,
if row_number == 0:
row = [ 1 ]
elif row_number == 1:
#Else…if
#the new row is at height of one, make the new row to be [1, 1].
else:
#Else..
row = []
#for each column in the row from 0 to the current height of the row plus one.
#If the column of this row is 0,
#append a 1 to the new row
# Else…if the column of this row is equal to the height of the row,
#append a 1 to the new row
#Else…
# the value for this position are stored in the triangle, the value stored at
#one row up and one column back plus the value stored at one row up
# and the same column.
value = # what they told you above
# Append this value to the new row.
return row
ASKER
This is what I have. And it is not working.
def pascalsTri():
userInput = raw_input("Please enter height")
height = int(userInput)
triangle = []
#----------------------------------------------------------------------
def mk_row(triangle,row_number):
if row_number == 0:
row = [1]
elif row_number == 1:
new_row == [1,1]
else:
row=[]
for row in range(0,height+1):
new_row = mk_row(triangle,row_number)
triangle.append(new_row)
return row
The most important task is to divide your problem into smaller problems
So create at first the function main()
asking you the height of the triangle
then let it calculate the triangle
then print it
def main():
""" THE main function. asks for user nput
calculates pascals triangle and prints it
"""
userInput = raw_input("Please enter height: ")
height = int(userInput)
triangle = pascals_tri(height)
print_triangle(triangle)
Now you can create the function to print the triangle
def print_triangle(triangle):
""" very simple function with bad formatting
to print out a pascals triangle
"""
for row in triangle:
print row
Now write the function to create pascals trinalge
(assuming you could implement a function, that calculates a new row)
def pascals_tri(height):
""" Function to calculate a pascals triangle with max_rows """
triangle = []
for row_number in range(0,height+1):
print "T:",triangle
row = mk_row(triangle,row_number )
triangle.append(row)
return triangle
Now the only function that is missing is the function,
that creates a new row of a triangle assuming you know the row
number and you calculated already the above rows.
def mk_row(triangle, row_number):
""" function creating a row of a pascals triangle
parameters:
triangle: list of rows of the triangle,
that were calculated so far
row_number: the new row to be calculated
"""
#If the new row is at a height of zero,
if row_number == 0:
row = [ 1 ]
elif row_number == 1: #Else if
#the new row is at height of one, make the new row to be [1, 1].
else: #Else..
row = []
### ADD YOUR CODE HERE
#for each column in the row from 0 to the current
# height of the row plus one.
if col == 0:
#If the column of this row is 0,
#append a 1 to the new row
row.append(1)
# ADD YOUR CODE HERE
# Else if the column of this row is equal to the height of the row,
#append a 1 to the new row
# ADD YOUR CODE HERE
else: #Else
# the value for this position are stored in the triangle,
# the value stored at
#one row up and one column back plus the value stored at
# one row up
# and the same column.
#value = # what they told you above
value = . . . # PLEASE COMPLETE LINE
# Append this value to the new row.
row.append(value)
return row
Last but not least you have to call the man function.
It is commun practice in python to do something like that
if __name__ == "__main__":
main()
attached some code, that you had to complete.
So create at first the function main()
asking you the height of the triangle
then let it calculate the triangle
then print it
def main():
""" THE main function. asks for user nput
calculates pascals triangle and prints it
"""
userInput = raw_input("Please enter height: ")
height = int(userInput)
triangle = pascals_tri(height)
print_triangle(triangle)
Now you can create the function to print the triangle
def print_triangle(triangle):
""" very simple function with bad formatting
to print out a pascals triangle
"""
for row in triangle:
print row
Now write the function to create pascals trinalge
(assuming you could implement a function, that calculates a new row)
def pascals_tri(height):
""" Function to calculate a pascals triangle with max_rows """
triangle = []
for row_number in range(0,height+1):
print "T:",triangle
row = mk_row(triangle,row_number
triangle.append(row)
return triangle
Now the only function that is missing is the function,
that creates a new row of a triangle assuming you know the row
number and you calculated already the above rows.
def mk_row(triangle, row_number):
""" function creating a row of a pascals triangle
parameters:
triangle: list of rows of the triangle,
that were calculated so far
row_number: the new row to be calculated
"""
#If the new row is at a height of zero,
if row_number == 0:
row = [ 1 ]
elif row_number == 1: #Else if
#the new row is at height of one, make the new row to be [1, 1].
else: #Else..
row = []
### ADD YOUR CODE HERE
#for each column in the row from 0 to the current
# height of the row plus one.
if col == 0:
#If the column of this row is 0,
#append a 1 to the new row
row.append(1)
# ADD YOUR CODE HERE
# Else if the column of this row is equal to the height of the row,
#append a 1 to the new row
# ADD YOUR CODE HERE
else: #Else
# the value for this position are stored in the triangle,
# the value stored at
#one row up and one column back plus the value stored at
# one row up
# and the same column.
#value = # what they told you above
value = . . . # PLEASE COMPLETE LINE
# Append this value to the new row.
row.append(value)
return row
Last but not least you have to call the man function.
It is commun practice in python to do something like that
if __name__ == "__main__":
main()
attached some code, that you had to complete.
#!/usr/bin/env python
def main():
""" THE main function. asks for user nput
calculates pascals triangle and prints it
"""
userInput = raw_input("Please enter height: ")
height = int(userInput)
triangle = pascals_tri(height)
print_triangle(triangle)
def print_triangle(triangle):
""" very simple function with bad formatting
to print out a pascals triangle
"""
for row in triangle:
print row
def pascals_tri(height):
""" Function to calculate a pascals triangle with max_rows """
triangle = []
for row_number in range(0,height+1):
print "T:",triangle
row = mk_row(triangle,row_number)
triangle.append(row)
return triangle
def mk_row(triangle, row_number):
""" function creating a row of a pascals triangle
parameters:
triangle: list of rows of the triangle,
that were calculated so far
row_number: the new row to be calculated
"""
#If the new row is at a height of zero,
if row_number == 0:
row = [ 1 ]
elif row_number == 1: #Else if
#the new row is at height of one, make the new row to be [1, 1].
else: #Else..
row = []
for col in # COMPLETE
#for each column in the row from 0 to the current
# height of the row plus one.
if col == 0:
#If the column of this row is 0,
#append a 1 to the new row
row.append(1)
# ADD YOUR CODE HERE
# Else if the column of this row is equal to the height of the row,
#append a 1 to the new row
# ADD CODE HERE
else: #Else
# the value for this position are stored in the triangle,
# the value stored at
#one row up and one column back plus the value stored at
# one row up
# and the same column.
#value = # what they told you above
value = . . . # COMPLETE LINE
# Append this value to the new row.
row.append(value)
return row
if __name__ == "__main__":
main()
ASKER
Hi can you do an example in one function because this is kinda confusing.
Completed code.
Just look for the lines marked with " # NEW LINE" to see what I added.
Just look for the lines marked with " # NEW LINE" to see what I added.
#!/usr/bin/env python
def main():
""" THE main function. asks for user nput
calculates pascals triangle and prints it
"""
userInput = raw_input("Please enter height: ")
height = int(userInput)
triangle = pascals_tri(height)
print_triangle(triangle)
def print_triangle(triangle):
""" very simple function with bad formatting
to print out a pascals triangle
"""
for row in triangle:
print row
def pascals_tri(height):
""" Function to calculate a pascals triangle with max_rows """
triangle = []
for row_number in range(0,height+1):
#print "T:",triangle
row = mk_row(triangle,row_number)
triangle.append(row)
return triangle
def mk_row(triangle, row_number):
""" function creating a row of a pascals triangle
parameters:
triangle: list of rows of the triangle,
that were calculated so far
row_number: the new row to be calculated
"""
#If the new row is at a height of zero,
if row_number == 0:
row = [ 1 ]
elif row_number == 1: #Else if
#the new row is at height of one, make the new row to be [1, 1].
row = [1, 1] # NEW LINE
else: #Else..
row = []
for col in range(0, row_number + 1): # NEW LINE
#for each column in the row from 0 to the current
# height of the row plus one.
if col == 0:
#If the column of this row is 0,
#append a 1 to the new row
row.append(1)
# Else if the column of this row is equal to the height of the row,
elif col == row_number: # NEW LINE
#append a 1 to the new row
row.append(1) # NEW LINE
else: #Else
# the value for this position are stored in the triangle,
# the value stored at
#one row up and one column back plus the value stored at
# one row up
# and the same column.
#value = # what they told you above
value = triangle[row_number-1][col-1] + triangle[row_number-1][col] # NEW LINE
# Append this value to the new row.
row.append(value)
return row
if __name__ == "__main__":
main()
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
This sounds like an exercise and if I wrote the entire solution it won't be fun anymore.
First question you have to ask yourself is:
do you really want to store the entire triangle in the variable triangale or do you want just to
print the triangle row by row.
Next:
I would suggest a function, that will calculate the next row if you give it the previous one.
def mk_next_row(previous_row):
# calculate next row
return new_row
the very first time you call the function you give it the first row and it will return the second
the next tine you cll it you give it the second row and it will return the first
so you should call
the first row is [ 1 ]
so
mk_next_row( [ 1 ] )
should return
[1, 1]
mk_next_row( [ 1 , 1] )
should return
[1, 2, 1]
mk_next_row( [ 1 , 2, 1] )
[1, 3, 3, 1]
how do you calculate a new row?
first new element
will be 1
the next one will be the some of the first and second element of the previous row
tje next one the sum of the second and third element
. . .
second last one is the some of the second last and the last element of the previous row
and the last element will be a 1
I hope this gives you some ideas.
Don't hesitate to ask further question in case my explanations don't put you on the right path