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!