Python generator to generate the Yang Hui triangle (mandatory), python Yang Hui

Source: Internet
Author: User

Python generator to generate the Yang Hui triangle (mandatory), python Yang Hui

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 python builder's method of generating the Yang Hui triangle (mandatory) is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support to the customer's house.

Related Article

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.