Write a python generator to generate the sample code of Yang Hui triangle

Source: Internet
Author: User
The following small series will bring you a python generator to generate the Yang Hui triangle method (mandatory ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you a python generator to generate the Yang Hui triangle method (mandatory ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.

It feels awkward to write interesting programs in Python.

# Generate and display the yang Hui triangle by the generator # The principle is to display the yang Hui triangle in a 2-dimension group, where the null is 0. when the output is made, it is converted to ''def yang (line): n, leng = * line-1 f_list = list (range (leng + 2) # pre-allocated, the initial insert Hu will slow down, the bottom line, there is also one space on the left and right # All are initialized to 0 for I, v in enumerate (f_list): f_list [v] = 0 ZEROLIST = f_list [:] # reserve an array f_list [leng // 2] = 1 # Initial first row re_list = f_list [:] n = 0 while n <line: n = n + 1 yield re_list f_list, re_list = re_list [:], ZEROLIST [:] start = leng // 2-n # Calculate the end of the first 1 in a row = start + 2 * n # calculate the position of the last 1 in a row while start <= end: re_list [start] = f_list [start-1] + f_list [start + 1] # whether it is 1 or not, the number at this position, both start = start + 1 return 'done' def printList (L): n = 0 p_str = ''for value in L: ch = str (value) if value = 0: ch = ''p_str = p_str + ch print (p_str) for value in yang (8): printList (value)

The above is the detailed content of the sample code generated by the python generator Yang Hui triangle. For more information, see other related articles in the first PHP community!

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.