Use Python to output an example of a Yang Hui's triangle _python

Source: Internet
Author: User

About Yang Hui's triangle is something that right turn Wikipedia: Yang Hui's triangle

Take a look at a slightly more intuitive picture:

Copy Code code 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
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1


The Yang Hui's triangle has the following several characteristics:

The value of each item equals the number of the upper-left corner and the number of the upper-right corner, and if there are no digits in the upper-left corner or upper-right corner, count to 0.
The number of n-tier items is always one more than the N-1 layer

To compute the Yang Hui's triangle of the nth layer, you must know the number of the N-1 layer and then add the numbers of the adjacent 2 items to get all the numbers on the next level except for the 2 1 on the edge. Sounds a bit like a recursive thought, let's assume we already know the N-1 layer of numbers to calculate n-tier numbers.

Copy Code code as follows:

def _yanghui_trangle (n, result):
if n = 1:
return [1]
Else
return [sum (i) for I with ZIP ([0] + result, result + [0])]

In the code above, result represents the number of Yang Hui's triangles in the N-1 layer. Internship, we are in the list 2 each complement a 0, and then calculate the adjacent items and, you can directly get results.

Slightly refine the code:

Copy Code code as follows:

def yanghui_trangle (n):
def _yanghui_trangle (n, result):
if n = 1:
return [1]
Else
return [sum (i) for I with ZIP ([0] + result, result + [0])]
Pre_result = []
For I in Xrange (n):
Pre_result = _yanghui_trangle (i + 1, pre_result)
Yield Pre_result

if __name__ = = "__main__":
For line in Yanghui_trangle1 (5):
Print Line


_yanghui_trangle can be abbreviated with a lambda, but the readability will get worse, so keep the status quo good.

Tips: The above program does not consider the problem of data formatting, which means that the output is not a perfect triangle.

In view of the recent learning of Erlang, a version of Erlang has not been tested on performance, but it is still a marvel at the expressive power of functional languages:

Copy Code code as follows:

-module (Yanghui).
-author (LFYZJCK).
-export ([TRIANGLE/1]).

Triangle_next (P)->
Lists:zipwith (Fun (X, Y)-> x+y end, [0| P], p + + [0]).

Triangle (1)->
[[1]];
Triangle (N)->
L = triangle (N-1),
[H|_] = L,
[Triangle_next (H) | L].

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.