Python3 Practice-Yang Hui triangle/Pascal Triangle

Source: Internet
Author: User

The Yang Hui triangular form is as follows:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 each 5 10 Ten 5 1
#Expected output:#[1]#[1, 1]#[1, 2, 1]#[1, 3, 3, 1]#[1, 4, 6, 4, 1]#[1, 5, ten, 5, 1]#[1, 6, 6, 1]#[1, 7, A, 7, 1]#[1, 8,---8, 1]#[1, 9, 126, 126, 9, 1]n =0 forTinchtriangles ():Print(t) n= n + 1ifn = = 10:         Break
ideas:1, the first I-1 line of the last 0, the length and line I will be consistent;2, the nth element value of line I is L (i) [n] = L (i-1) [N-1]+l (I-1) [n] For example:1, i=3, at this time the collection is [1,3,3,1];2, i-1 row set complement 0, set for [1,2,1, 0]3, L (3) [0]=l (2) [ -1]+l (2) [0]=0+1=1     L (3) [1]=l (2) [0]+l (2) [1]=1+2=3      ...]
The code is as follows: def triangles ():     = [1]    while  True:        yield  L        l.append (0)        for   in range (len (L))]n=0 for in triangles ():     Print(t)    = n + 1    if n = =Ten        : break

Python3, List[-1] is represented as the last element in the list collection

Other Related:

This exercise stems from learning the notes in the Python process.

Learning website from Liaoche's official website: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000

Links to related chapters:

Http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 0014317799226173f45ce40636141b6abc8424e12b5fb27000

Python3 Practice-Yang Hui triangle/Pascal Triangle

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.