A Little Bit About Lists in Python

Published on:

As usual 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.

What

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:

Extract elements from a list based on its first letter
1
2
3
items = ['geeklist', 'playfair', 'heroku', 'hallucination', 'hulu', 'hotmail', 'dojo', 'zen', 'enigma']
filter(lambda item: item[0] in ['h', 'e'], items)
['heroku', 'hallucination', 'hulu', 'hotmail', 'enigma']

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.

Creating closures for greeting users
1
2
3
4
5
6
7
8
def greet(greeting):
  return lambda name: greeting + ' ' + name

greetNamaste = greet('Namaste')
greetNamaste('Sam')
'Namaste Sam'
greetNamaste('Goldberg')
'Namaste Goldberg'

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 name argument.

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 reduce method.

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.

Function for generating hash based on prime numbers
1
2
def hashString(str):
  return reduce(lambda memo, char: memo * charMap[char], list(str), 1)

Straight from the Python docs regarding the reduce function.

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.

Some Time Saving Tech Tips

Published on:

Most of the tips may already be known to people using computers for quite some time. Have a quick look if you can find something new.

Almost all of us use some sort of internet enabled device. So, its important that we spend our time doing the important task at hand rather than wasting time using the device to get it done.

Now since our busy life demands us to save our time, here I provide some shortcuts and smart techniques to get things done fast.

Most of these tips are not known to computer newbies and hence focused on them.

Computers

Selecting text

Don’t waste time in clicking and then dragging the mouse to select a word or a paragraph. Instead use double click and triple click.

Use double-click to select a word and triple-click to select a paragraph.

While typing, don’t keep pressing the backspace key to delete a piece of text. Rather use the combination of Shift + Ctrl + Left Arrow keys to select a piece of text in increments of a word and delete it using the Backspace key.

If you want to replace a piece of text, select it and then start typing the new text rather then deleting the selected text and then typing the new text. This will automatically replace the selected text with the one you type.

Instead of dragging to select a piece of text, double click and then drag to select text in increments of one word.

Navigating text

Rather then using the left and the right key to navigate text, use the Ctrl + Left/Right keys to traverse in increments of one word.

To reach the end or the start of a line use the end and the home keys respectively, rather than pressing the left or the right key till you reach the start or the end.

Page Scrolling

Use the Space and the Shift + Space keys to scroll down and scroll up respectively one page at a time. Don’t use the scroll button on your mouse, its a waste of time.

Others

When filling forms use the tab key to switch to the next form field. Using the mouse to do so is a terrible waste of time.

When selecting an option from a list of options don’t use your mouse to expand it and then select your option (not if you don’t know the options), rather use tab to focus it and then press the first letter on the keyboard until you get your desired option selected.

Want to know what the weather report has to tell the temperature in your locality, just search for weather <city> on google to find it out.

Just encountered a word not known to you, just search for define <word> on google to find out what it means.

Mobile Phones

If you wish to call a person you just called, just press the call button and the number of the person you just called will be entered in the dial pad. Just press the call button again to call. (Some mobile phones, may call the person the first time you press the call button)

While typing a piece of text if you wish to end the sentence, don’t type . and then a space. Rather double tap the space button quickly to solve the purpose.

I hope these tech tips will save a lot of your valueable time.

Source : TED talk on 10 time saving tech tips