Github was the first tab that I opened after firing up Google Chrome. I was reading through my news feed nice and smooth until something interesting held my attention. Reading the news feed may not sound so cool but indeed it is quite exciting. Opening a dozen new projects in a dozen new Chrome tab just to find out that each one of them is awesome makes my day nothing but more exciting.
I found an interesting repository where I could not only show-off my programming skills (well, not really) but also learn a few things about Python.
What ever! That was my part of the story & let me introduce to your part real soon.
Lists in Python
Now I am not going to define
lists and neither am I going to introduce you to its basic. There are more than a gross articles out there explaining it. And it would be worthless doing it over and over again.
For those who don't know: 1 gross = 144, that's 12 dozens
And as I go on with my explanation I would also enjoy commenting on the Pythonic nature of Python.
As I was trying to implement a few (actually just two) code problems using Python, I encountered a few hurdles.
While trying to implement an anagram detector I came up with a few useful and interesting things.
Using lambda to create anonymous functions
One of the many attractions of Python is it’s ability to make every thing look so damn readable. Lambda expressions just adds to this ability.
As a quick fact, the concept of Lambda was borrowed from the Lisp language
For example, consider this small code snipet to extract a few elements from a list based on its first letter:
1 2 3
Lambda comes from the lambda calculus and is a cool way of writing anonymous functions.
You can also use it to write closures. Here is a quick example to greet a user.
1 2 3 4 5 6 7 8
In the example above the
greet function returns an anonymous function defined using the lambda expression. This anonymous function can then be assigned to an identifier with the only requirement of a
Reducing a list to a single element
Sometimes it is useful to reduce a list of elements to a single element based on a few rules. The problem statement for the anagram detector is as stated below:
Write a function that accepts two parameters, a parent and a child string. Determine how many times the child string - or an anagram of the child string - appears in the parent string.
There is an elegant method of solving this problem and can be easily coded in Python by the use of the
We start by assigning distinct prime numbers one for each alphabet. Then we create what is called a hash to identify a string and its anagram. The hashing is such that a string and all its anagrams will result in the same hash. This is done by multiplying together the prime number corresponding to each alphabet. Such a procedure results in a number which can only be formed by a different string if both of them have the same list of characters and frequency of occurrence (hence an anagram).
I replicated this idea in Python. I do not claim to have invented it.
This is how I implemented it in Python. Here I present the core of this small program.
Straight from the Python docs regarding the
It Applies function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
The function in our case is the anonymous lambda function which returns the multiplied result of the cumulated value and the prime number corresponding to the current alphabet. The third argument specifies the default value for the cumulated value to start with. Thus making
1 a reasonable choice for the same.
Note: the charMap is just a dictionary which maps each alphabet to its corresponding prime number
All this together with a lot of other cool expressions and functions add to the Pythonic nature of the Python programming language.