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.