Write a Python generator code example that generates a Yang Hui triangle

Source: Internet
Author: User
The following small series for you to bring a python generator generation Yang Hui triangle method (must see). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Writing fun programs in Python feels like a dick, can't stop

#生成器生成展示杨辉三角 # The principle is to display the Yang Hui triangle in a 2-D array, empty place with 0, output, convert to "Def Yang (line):  n,leng=0,2*line-1  f_list = List (leng +2) #预先分配, insert initial hu will slow down, the bottom line, left and right there are 1 spaces  #全部初始化为0 for  i,v in Enumerate (f_list):    f_list[v] = 0  Zerolist = f_list[:] #预留一个全零的数组  F_LIST[LENG//2] = 1 #初始的第一行  re_list =f_list[:]  n=0 while  N < line:< C10/>n = n+1    yield re_list    f_list,re_list = re_list[:],zerolist[:]    start = Leng//2-n #计算一行中第一个1的位置    end = start + 2*n #计算一行中最后一个1的位置 while    start <= end:      Re_list[start] = f_list[start-1] + f_list[start+ 1] #不管是不是1, the position of the number, is the last line of the position of the left and right two numbers and      start = start + 1  return ' Done ' def printlist (L):  n = 0  p_str = " C21/>for value in L:    ch = str (value)    if value = = 0:      ch = "    p_str = p_str + ch  print (P_STR) for V Alue in Yang (8):  printlist (value)
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.