10 kinds of solutions to realize Yang Hui triangle--experience the beauty of Python

Source: Internet
Author: User

This paper collects a variety of solutions using Python to achieve the Yang Hui Triangle, mainly for online collection, and some of them are written by themselves. It is possible to understand the different ideas of Python writing an algorithm and the characteristics of Python syntax.


What is the Yang Hui triangle? It looks like this:


                          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                  1   7   21  35  35  21  7   1                1   8   28  56  70  56  28  8   1              1   9   36  84  126 126 84  36  9   1            1   10  45  120 210 252 210 120 45  10  1          1   11  55  165 330 462 462 330 165 55  11  1         1   12  66  220 495 792 924 792 495 220 66  12  1 ...


The solution function is as follows. Personally feel that the first 7,8,9,10 these are the most ingenious implementation.


Def triangle1 ():
p = [1]
A = 0
While True:
Q = []
i = 0
While I <= a:
If i = = 0 or i = = Len (p):
Q.append (1)
Else
Q.append (P[i-1] + p[i])
i + = 1
p = q
Yield Q
A + = 1

Def triangle2 ():
Q = [1]
While True:
Yield Q
i = 0
While 0 <= i <= Len (q)-1:
If i = = 0:
Pass
Else
Q[i] = P[i-1] + p[i]
i + = 1
Q.append (1)
p = Tuple (q) # or can be P = q[:]

Def triangle3 ():
Q = [1]
While True:
Yield Q
i = 1
While 1 <= i <= len (q)-1:
Q[i] = P[i-1] + p[i]
i + = 1
Q.append (1)
p = q[:]

Def triangle4 ():
Q = [1]
While True:
Yield Q
For I in range (1, len (q)):
Q[i] = P[i-1] + p[i]
Q.append (1)
p = q[:]

Def triangle5 ():
p = [1]
While True:
Yield P
p = [P[0] if i = = 0 or i = = Len (p) Else p[i-1] + p[i] for I in range (Len (p) + 1)]

Def triangle6 ():
p = [1]
While True:
Yield P
p = [1] + [p[i] + p[i+1] for I in range (Len (p)-1)] + [1]

Def triangle7 ():
p = [1]
While True:
Yield P
P.insert (0,0)
P.append (0)
p = [P[i] + p[i+1] for I in range (Len (p)-1)]

Def triangle8 ():
p = [1]
While True:
Yield P
A = p[:]
b = p[:]
A.insert (0,0)
B.append (0)
p = [A[i] + b[i] for I in range (Len (a))]

Def triangle9 ():
p = [1]
While True:
Yield P
P.append (0)
p = [P[i-1] + p[i] for I in range (Len (p))]


Def triangle10 ():
A = [1]
While True:
Yield a
A = [sum (i) for i in Zip ([0] + A, A + [0])]


def triangle11 (n):
if n = = 1:
return [1]
If n > 1:
A = Triangle11 (n-1)
b = Triangle11 (n-1)
A.insert (0,0)

B.append (0)
return [A[i] + b[i] for I in range (n)]

n = 0
For I in Triangle9 ():
Print I
n + = 1
if n = = 11:
Break

For I in range (1, 12):
Print Triangle10 (i)

10 kinds of solutions to realize Yang Hui triangle--experience the beauty of Python

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.