Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pascal's triangle
#1
Pascal's triangle is a mathematical array of binomial coefficients. write a program that outputs the Nth row of the triangle, where N is given as input integer. In following good computer science tradition, we'll define the first row to be row 0. Implementations should support up to row 53.
Reply
#2
We won't do it for you. Give your homework a try, and if you have trouble, we'll help.

But we won't do it for you.
Reply
#3
#!/usr/local/bin/python
# input =5
# 1
# 1 1
# 1 2 1
# 1 3 3 1
# 1 4 6 4 1

import sys

num = input("How many levels of Pascals Traingle?")
num += 1

if (num ==1):
     print("1")
     sys.exit()

upper = [None] * num
lower = [None] * num

upper[0] = 1
upper[1] = 1
upper[0] = 1
upper[0] = 1

def assignArray( a, b, size ):
    for x in range(size):
        a[x] = b[x]
    return

for x in range(num):
    for z in range(x):
        # check for edges
        # if not in edge upper array add values
        if(z == 0 or z == x - 1):
            print('1'),
            lower[z] = 1
        else :
            lower[z] = upper[z-1] + upper[z]
            print(lower[z]),

    assignArray(upper, lower, x)
    print(" ")
Reply
#4
What about it doesn't work? Park of making an effort is explaining what you tried and what you expected clearly, ideally isolating it to the most minimal amount of code that reproduces your problem.
Reply
#5
Explain to yourself in words what the relationship is between this row and the previous row and then start to code once that is clear.
Reply
#6
sounds like a fun project. are you going to code this in Python? i've done something like this before, though not in Python. maybe i should do one in Python. i'll show you mine if you show me yours (working code) first.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,908 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  Print user input into triangle djtjhokie 1 2,418 Nov-07-2020, 07:01 PM
Last Post: buran
  Tkinter - The Reuleaux Triangle andrewapk 1 1,982 Oct-06-2020, 09:01 PM
Last Post: deanhystad
  Python - networkx - Triangle inequality - Graph Nick_A 0 2,119 Sep-11-2020, 04:29 PM
Last Post: Nick_A
  Triangle function program m8jorp8yne 2 8,934 Dec-13-2019, 05:24 PM
Last Post: Clunk_Head
  Print triangle using while loop tuxandrew 3 4,978 Dec-05-2019, 07:17 PM
Last Post: micseydel
  Intersection of a triangle and a circle Gira 3 3,645 May-19-2019, 06:04 PM
Last Post: heiner55
  Draw isosceles triangle fen1c5 4 12,958 Jul-07-2018, 10:20 AM
Last Post: fen1c5
  Object oriented area of a triangle xterakojede 2 8,973 Apr-20-2018, 01:42 PM
Last Post: xterakojede
  Turtle drawing Right Triangle Zatoichi 3 5,758 Feb-26-2018, 12:24 AM
Last Post: Zatoichi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020