Use python print Pascal's Triangle

With python print Pascal's Triangle

Introduction

Pascal's Triangle, is a series of high school early, the core idea that is generating a number of columns, the number of columns in each element are a number of columns before, the same elements and the location of the previous element and.

Just python, that is to generate a list, the list of elements, the elements are the same place as before and the previous list element, and. Here, I think the method is to be calculated by an iterative, since each generation the number of columns, are needed before a number of columns as a basis.

Method to realize

First, the need to implement a list of the N-th generation Pascal triangle, by analyzing the entire Pascal triangle, in addition to the first and second number of columns accident, the other can all be generated by an iterative manner. Specific functions are as follows:

def Traingle(x):
    '''定义杨辉三角的函数,即下一行的第N个元素,是本行的第N个元素和第N-2个元素的和,最后返回一个列表'''
    x = int(x)
    Traingle_list = []
    if x == 1:
        Traingle_list = [1]
    elif x == 2:
        Traingle_list = [1,1]
    else:
        Traingle_list_temp = Traingle(x-1)#获取上一行三角列表
        Traingle_list = [1,1]
        temp = 1
        while temp <= x-2:
            Traingle_list.insert(temp,Traingle_list_temp[temp]+Traingle_list_temp[temp-1])
            temp += 1

    return Traingle_list

In the above function, 1 and 2 is better understood, because relatively simple, starting from the first three, are acquired before a first number of columns, and then use a circular manner, in a corresponding position, insertion, insertion elements , that is, before a series of numbers, and the same place and before an element. Note also that, because of the initial list and the index list, the first element and the last element is not inserted, so that the last element x-1, is inserted up to a position of x-2.

Returned result is a list that can be called directly print.

In order to be Pascal's Triangle looks more regularly, when printing needs to do some processing, such as centered. I can think of would be to use the string centered, first converted into a list of strings, then the entire string centered.

In processing the data, since increasingly larger numbers, the length of each element are not the same, if the fixed die centered, for the latter list, it is ugly. Therefore, during the treatment, the default maximum first remove the largest element in the list, and then calculate the length of the element, all the elements of each list are performed according to the length of the centering element is filled, thus ensuring all listings All elements of the same length is fixed, you can look good looks.

Further, since the length of the entire list, is converted into a list of strings defined by a string, and spaces of the elements, the elements are fixed length, the number of the spaces are fixed, there are N elements, there is N- one space. Therefore, a print-defined function, the output of print Pascal triangle, as follows:

def PrintTringle(x):
    '''
    打印杨辉三角,其中利用到的是字符串居中显示,周围填充空格,但是由于不确定得到的结果的长度,所以需要得到整个列表中最大的那个元素,然后计算该元素的长度,利用该长度,为每一个杨辉三角的值进行填充剧中。
    :param x:
    :return:
    '''
    n = 1
    x = int(x)
    # 获取最大的元素,判断列表最大的元素的长度
    if x % 2 == 0:
        max_item = Traingle(x)[int(x/2)]
    else:
        max_item = Traingle(x)[int((x+1)/2)]
    max_length = len(str(max_item))
    while n <= x :
        list = Traingle(n)
        string = ''
        tem = 0
        # 对每一列杨辉三角进行处理,转化成为字符串,拼接
        while tem < len(list):
            if tem == 0:
                string = str(list[tem]).center(max_length)
            else:
                string = string + ' ' + str(list[tem]).center(max_length)
            tem += 1
        # 打印处理,每一列的元素有N个,空格用N+1
        print(string.center((max_length+1)*x-1))
        n += 1

When the function is called, simply enter a parameter, a positive integer, can print the entire sequence of Pascal's triangle to complete a certain length and arranged in the logic display. For example, to print 20.

PrintTringle(20)

                                                       1                                                           
                                                    1     1                                                        
                                                 1     2     1                                                     
                                              1     3     3     1                                                  
                                           1     4     6     4     1                                               
                                        1     5     10    10    5     1                                            
                                     1     6     15    20    15    6     1                                         
                                  1     7     21    35    35    21    7     1                                      
                               1     8     28    56    70    56    28    8     1                                   
                            1     9     36    84   126   126    84    36    9     1                                
                         1     10    45   120   210   252   210   120    45    10    1                             
                      1     11    55   165   330   462   462   330   165    55    11    1                          
                   1     12    66   220   495   792   924   792   495   220    66    12    1                       
                1     13    78   286   715   1287  1716  1716  1287  715   286    78    13    1                    
             1     14    91   364   1001  2002  3003  3432  3003  2002  1001  364    91    14    1                 
          1     15   105   455   1365  3003  5005  6435  6435  5005  3003  1365  455   105    15    1              
       1     16   120   560   1820  4368  8008 11440 12870 11440  8008  4368  1820  560   120    16    1           
    1     17   136   680   2380  6188 12376 19448 24310 24310 19448 12376  6188  2380  680   136    17    1        
 1     18   153   816   3060  8568 18564 31824 43758 48620 43758 31824 18564  8568  3060  816   153    18    1     
1     19   171   969   3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628  3876  969   171    19    1  

Above revealed that the affected page layout, showing incomplete, in fact, in the terminal is normally displayed.

 

 

Guess you like

Origin www.cnblogs.com/bobo137950263/p/11074151.html