Generating Pascal’s/Pingala’s Triangle using Python


There are plenty of articles on the Pascal’s or Pingala’s triangle on the internet, so I wish not to revisit the explanation and the history about the triangle. Rather this blog post explains about one of the simplest way to produce or generate the triangle in Pythonic way. It talks about how one can generate the triangle using some of the Python concepts like, list comprehension, Python’s zip(), Formatting functionalities etc.

Pascal’s /Pingala’s Triangle

For more interesting facts on Pascals triangle. Please read the below web pages.

Pascal’s Triangle

The Pascal Triangle and Pingala’s problem

Binomial Triangle Computer Binary System

The Coding:

To see the code, please visit my GitHub pages. The below section explains the code. The code starts with the python class which has two method, one is to collect the user input ‘get_level‘ and another is to generate the triangle ‘generate_pascal_traingle‘ along with a constructor which initializes the variables.

The constructor initializes the ‘row’ and ‘static_list’ which is list element with the value ‘0’ and ‘1’ respectively. It also initializes the level variable to ‘0’.

Code Snippet

The ‘get_level’ is pretty simple method which collects the user input ‘level’ till where we need to generate the triangle. The ‘get_level’ method takes the user input and assigns it to the variable ‘level’

The ‘generate_pascal_traingle’ uses some of the Python concepts like, list comprehension, print formatting functions and zip() functionality. The method starts with the for loop which runs through the level mentioned by the user input. To understand the ‘_’ significance in the for loop, please visit the Quora page

Row generation logic

The line #20 generates the printing string, it uses string join and list comprehension functionality to generate the individual elements (numbers) from the genrated list and joins as single string with one white space.

The line #21 prints the string generated in the line # 16 with the Python formatting. The ‘width’ variable which is used in the formatting will sets the ‘width’ based on the user input ‘level’. I also used constant to increse the width by some more characters.

The line #22 will generate the each row for the triangle. Here we use the list comprehension and zip() functionality to generate the triangle row. The zip function takes the two list and generates the tuple by taking the individual elements from both the list. This is explained graphically in the below diagram.

After this, the for loop continues to print the generated row (line # 20) until the input level is reached.

This is one of the simplest way to generate the Pascal / Pingala triangle.

Happy Coding!

Python- First Class Functions

Conceptually, “first class functions” is a feature of a programming language. Several programming language like Lisp, Haskell, Scala, Julia, Python, JavaScript, Ruby, Perl, Go etc. supports the “First Class Function”. In a nutshell, all functions in Python are first class functions. When we say first class function in any programming language, the language treats the function as any other variable, it can be passed and manipulated similarly how we treat other kind of objects (In Python all are treated as objects) like integer, string.

Properties First Class Functions

  • Allows to store the function in a variable.
  • Allows to pass the function as a parameter to another function.
  • Allows to return the function from a function.
  • Allows to store then in data structures such as list, dictionaries and tuple.
Stores the function as variable:

This feature can be illustrated with the below snippet of code.

# File name :
# Function Definition
def wish_the_world(message):
    return "Hello, there, {0}".format(message)

# Call the function with a message
print(wish_the_world("Good Morning!"))

# Store function as variable
wish_good_afternoon = wish_the_world

# printing, will print the memory location of function. 

# Calling the function which is stored as variable
print(wish_good_afternoon("Good Afternoon!"))

Here is the result of the above python code.

Hello, there, Good Morning!
<function wish_the_world at 0x7f698c6536e0>
Hello, there, Good Afternoon!

Let me explain the above code. We have started with the defining a function called ‘wish_the_world’ which takes an argument called the ‘message’. The function returns the message ‘Hello, there’ with the given argument. In the next line, we call the function with message ‘_Good Morning!_’, this displays the message in the output as ‘Hello, there, Good Morning!

Following line, we assign the function ‘wish_the_world’ to a variable. Here we are able to store the function as variable. Now, when we try to print the variable, the interpreter will displays ‘<function wish_the_world at 0x7f698c6536e0>’ which says the variable ‘wish_good_afternoon’ is pointing to the function ‘wish_the_world’. There is also a small information about the memory location where the function is currently residing at machine memory.

If you want to call the function which is stored as variable, you have to call with with ‘()’. In the next line we have called the function ‘wish_good_afternoon(“Good Afternoon!”)’ with a message.

Pass the function as argument to another function:

Below is small code snippet which shows the function passed as argument to another function.

# File Name :
# Multiply by 2
def multiply_by_2(x):
    return 2*x
# Function which takes first argument as function.

def multiplication_table(fun, multiple, arg_list):
    for item in arg_list:
        print("{0} x {1} = {2}".format(multiple, item, fun(item)))

# Calling the function to display the multiplication table
multiplication_table(multiply_by_2, 2, range(1,6))

When we run the above code, here is the result.

MyShell-$ python
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10

Here, we have a function ‘multiply_by_2’. The functionality of this function is to multiply the given numbers by 2 and returns the results. We have another function to display the multiplication table, which takes 3 arguments. The first argument is a function, the second one is multiple and the last is the list of numbers. When we call the function ‘multiplication_table’ with arguments ‘multiplication_table(multiply_by_2, 2, range(1,6))‘, for each number in numbers list, the function ‘multiply_by_2’ is called and it will display the multiplication table for 2.
One can see that the function ‘multiply_by_2’ is passed to the function ‘multiplication_table’

Return the function from function:

Here is the illustration of the return the function from a function.

# File Name:
# Log Levels
log_levels = {1:"DEBUG", 2:"INFO", 3:"WARNING", 4:"CRITICAL", 5:"ERROR"}

def logger(log_level):
    def wrap_log(message):
        print("{0}::{1}".format(log_level, message))
    return wrap_log

# defining individaul log functions.
log_debug = logger(log_levels[1])
log_info = logger(log_levels[2])
log_warning = logger(log_levels[3])
log_critical = logger(log_levels[4])
log_error = logger(log_levels[5])

# Based on log levels, functions can be called by  passing the releant message.
log_debug("This is debug log.")
log_info("This is information log.")
log_warning("This is warning log.")
log_critical("This is critical log.")
log_error("This is error log")

When we run the above snippet, this is the result

DEBUG::This is debug log.
INFO::This is information log.
WARNING::This is warning log.
CRITICAL::This is critical log.
ERROR::This is error log

In the above code, we have defined a dictionary ‘log_levels’ which contain the log levels such as

We have defined a function called ‘logger’, which takes an argument log level. Inside the function ‘logger’, we another inner function ‘wrap_log’, which takes message as argument and prints the both log level as well as the message. In the main function ‘logger’, we are returning the ‘wrap_log’ function without calling it. (Note that we have not used ‘()’ while returning the function)

Next, we have defined the different log functions based on the log levels, by passing log_levels to the each function. When we run the ‘log_error = logger(log_levels[5])’, the interpreter calls the outer function (‘logger’) with log level ‘ERROR’. Inside the function ‘logger’, we are defining a wrapper function ‘wrap_log’, which takes an argument ‘message’. The print in the wrapper function prints the log level and the message to the console. Please note the return statement in the function logger, we are just returning the function as variable reference not calling the function.

Based on the logging levels, the particular log function can be called. As an example, in case of any error, we can call the function ‘log_error(“This is error log”)’ by passing the error message to the function. This will calls the wrapper function ‘wrap_logger’ with the error message and prints the ‘ERROR::This is error log’ in the terminal.

Functions stored in the data structures:

This code snippet illustrates, function stores in data structures with following code implementation.

# File Name:
# General function to store in 
def function_in_ds(ds,message):
    print("Data Structure is '{0}'. Message:{1}".format(ds, message))

# Storing the 'function_ib_ds' in data structures
my_list = [function_in_ds]
my_tuple = [function_in_ds]
my_dict = {1:function_in_ds}

# Calling the functions
my_list[0]("List","Good Morning!")
my_tuple[0]("Tuple","Good Afternoon!")
my_dict[1]("Dictionary", "Good Evening!")

When we run the above code, it will print the below results.

Data Structure is 'List'. Message:Good Morning!
Data Structure is 'Tuple'. Message:Good Afternoon!
Data Structure is 'Dictionary'. Message:Good Evening!

The above illustration is simple. We have function ‘function_in_ds’, which takes two arguments (data structure name and message). and prints them to the console. As mentioned in the code, we are assigning the function to the list, tuple, dictionary as the function reference. While calling, we can call the reference of list tuple and dictionary with the respective arguments.

Thin line between “First Class Functions” and “Higher Order Functions”

While going through the references from the web for deeper understanding on these topics, you may come across the term ‘Higher order Functions’. There is thin difference between “First Class Functions” and “Higher Order Functions.”

First Class Functions Higher Order Functions
Concept only limited with functions in programming language. More in general sense, like it can be applied in mathematics.
ITreated as variable, can be assigned to variable or passed as argument. It receives another function as an argument or returns First-order a new function or both.
First class function has capabilities of Higher Order Function. Higher Order Function does not have the capabilities of First Class Function.

Trek in Ranipuram

Located 750m above the sea level on the belt of Western Ghats in the Kasaragod Dist of Kerala, Ranipuram is a small hill station ideal for trecking and adventure sports. It is also called as Ooty of Kerala (Wikipedia). This belt is home for one of the Shola forests which is found in western ghats section of South India. The hillstation is well connected with roads from Kasaragod, Kanhengad and Mangalore.

How to Reach:

The hillstation is located at Panathady village in Hosadurga taluk of Kasaragod district. One can go from Kasargod or Kanhengad. From Kasaragod and Kanhengad it is approximately 55 Km and 48 Km respectively.
Near by Airport is Mangalore
Near by Railway Station is Kasaragod and Kanhengad.
Regular buses are available from Kasaragod and Kanhengad.

The Trek:

The trek begins at the KTDC Cottage. This is the typical forest route. One has to walk in the forest for apprximately half an hour to one hour. The ambiance of the forest trail is awesome. It is covered by large trees of different diversity. As and when you reach top of the hills the tree will disappear and the beautiful stretch of grassland will appear. From here the part of the way/climb is assisted by cut steps. As and when you reach top of the hill, you will get beautiful stretch of the vally. The total track time is approximately 2 hours.

For more information about the bio-diversity, floura and founa and other details, please visit Wiki


Boats sets for fishing

Catch the Fish

A view of fishing activity

Sunset @ Mercara

Sun set at Madikeri Town

Refreshing Moments

Dubare Elephant Camp

Coorg- Mandalapatti

A view from Mandalapatti Hills in Coorg

Finest Evening

After the sun set at Tanniru Bavi beach

Sunset and Ferry

A ferry captured during sunset at Ullal Beach