Python uses a for, while loop to print the Yang Hui triangle exercise (list index exercise).

Source: Internet
Author: User

Python uses a for while loop to print the Yang Hui triangle exercise (list index exercise).

Yang Hui Triangle is a number of triangular tables, the general form is as follows:

1

1 1

1 2 1

1 3 3 1

1 4 6) 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

.......................

The most essential feature of the Yang Hui Triangle is that its two hypotenuse is made up of the number 1, while the remaining number equals the sum of two numbers on its shoulders.


Method One:


__author__ = ' Brad '

n = Int (input (' Enter the number of lines you want to print Yang Hui triangle: ')

newline = [1]

Oldline = []

Print (newline)

For I in Range (1,n):

Oldline = New.copy ()

Oldline.append (0)

Newline.clear ()

For j in Range (i+1):

Newline.append (Oldline[j-1]+oldline[j])

Print (newline)





Features of the Yang Hui triangle: the nth line has n elements, from the third line, newline[1]=oldline[0]+oldine[1] ... The Yang Hui triangle can be expanded on the right to a column of all 0, starting from the second line, Newline[0]=oldline[-1]+oldline[0], and newline[0] can also be computed by the elements of Oldline.


1 0

1 1 0

1 2 1 0

1 3 3) 1 0

1 4 6 4 1 0

1 5 10 10 5 1 0

1 6 15 20 15 6 1 0

#第一行的列表需要直接打印

#这个newline是经过计算

#通过对这个oldline的元素进行计算得出newline中的元素

#剩余n-1 lines, the computed newline are printed separately by cyclic n-1, so the interval is set to (1,n)

Start calculating the second line and copy the first line to Oldline for calculation.

Add a 0 to the Oldline list to become the Yang Hui Triangle variant we want.

Make a blank line newline, the elements inside are calculated by calculating

OLDLINE[0]+OLDLINE[1],OLDLINE[0]+OLDLINE[1] Index order repeated bad, you can try to use negative index oldline[-1]+oldline[0],oldline[0]+oldline[1] Just the second line of the element: [0,1], then try to set the interval of J to [] [i+1]

The subsequent row-by-line also meets the requirements.

The result is:


: 6

[1]

[1, 1]

[1, 2, 1]

[1, 3, 3, 1]

[1, 4, 6, 4, 1]

[1, 5, 10, 10, 5, 1]


Method Two:


Yang Hui Triangle starts from the third row, triangle[1] to triangle[n-1] can be calculated from the elements of the previous line.




__author__ = ' Brad '

n = Int (input (' Enter the number of lines you want to print Yang Hui triangle: ')

triangle=[[1],[1,1]]

For I in Range (2,n):

PRE=TRIANGLE[I-1]

CUR=[1]

For j in Range (I-1):

Cur.append (Pre[j]+pre[j+1])

Cur.append (1)

Triangle.append (cur)

Print (triangle)


Triangle[0] and triangle[n-1] do not calculate, respectively, in the first cycle of the definition and increment, only the remaining elements, the elements of each row is added as a triangle element, as the next row cur calculation of the pre.

The first two lines are not convenient to calculate, directly listed. Starting from the third line, I = 2 pre=tirangle[1],cur[1]=1+1 is pre[0]+pre[1], so the interval of J is set to (I-1), the next calculation is smooth, direct printing is the Yang Hui triangle.

The result is:


: 6

[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]


Method Three:


__author__ = ' Brad '

n = Int (input (' Enter the number of lines you want to print Yang Hui triangle: ')

Triangle=[]

For I in range (n):

ROW=[1]

Triangle.append (Row)

If i==0:

Continue

For j in Range (I-1):

Row.append (Triangle[i-1][j]+triangle[i-1][j+1])

Row.append (1)

Print (triangle)

In this method, row is equivalent to the cur in method two, and each calculation is initially [1].


When i==0, triangle add [1] to [[1],[1]],


When i==2, row[1] Add triangle[1][0]+triangle[1][0], then add [1], and change to [[1],[2],[1].

And then print in turn. The result is:


: 6

[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]


Python uses a for, while loop to print the Yang Hui triangle exercise (list index exercise).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.