Khalid Mammadov

Pascal’s Triangle in Python

Overview

In this article I am putting together a code that calculates Pascal's Triangle in Python code. I am going to use Python 3.8's new Combinatorics function that was added into standard library under the math module.

Pascal's triangle consists from triangular array of the binomial coefficients and looks as below:

                 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

So every number is a sum of top two number just above that number. But to get every number binomial coefficient must computed and it's done by calculating number of Combinations for specific place by using combination formula giving row number as number of elements and column no as number of choices.

I have already compiled the code and it can be accessed in below address:

https://github.com/khalidmammadov/python_code/tree/master/pascal_triangle

The main function that calculates triangle takes line number as an argument and builds a list of list where every list within top list corresponds to the line of the triangle i.e.

[['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']]

and this is done by below functions:

def bin_expans(row, col):
    """
        Calculate binomial coefficient
    """
    nCr = math.comb(row, col)
    return nCr


def build_triangle(rows = 10):
    """
        Calculate triangle
    """
    triangle = [[str(bin_expans(row, col))
                    for col in range(row+1)]
                        for row in range(rows)]
    return triangle

This new function from math module makes the calculation easy and without using additional packages.

The full source code is available at the URL mentioned above. Additionally the code defines pretty printing function and does version check and implement unit test for the calculations.

Execution

If you clone the repository and ensure you have python 3.8 installed in /usr/bin/python3.8 directory (for POSIX) or change the Makefile accordingly, you can execute the code by running below command:

make unittest
make run

Conclusion

This simple code demonstrate powerful and ever-expanding features of Python that we can use and contribute to.

I hope you enjoyed reading or checking it out and wish you every success in Python journey!

And yes please do comment if you wish so.