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