Checking if two strings are anagrams in Python (Also for checking if a list has duplicates)

Python has two really nifty datatypes namely strings and lists that can handle a variety of tasks. For eg: if you want to check if two strings are anagrams.

Convert the string to lists, an iterable can be converted into list by list()

Sort both lists and simply check if they are equal.. Done !

To check if a list has duplicates, sorting the list comes handy again. We can simply sort the list and then check if consecutive elements are equal if so, we can directly say that we have duplicates! Easy wasn’t it? 🙂

An interesting method to do this I came across online was to make a letter histogram. Given two words, we want to determine if they are anagrams.. Lets make an int array of length 26(corresponding to each letter of the alphabet) all initialised to zero. Traverse through the first string, increment each corresponding integer variable, next traverse through the next list and go on decrementing the corresponding integer values.. If finally all elements are zero, we say we have anagrams, else we don’t.. Easy right? 🙂


Local and global variables in Python

Variables defined within a function are local to that function and cannot be accessed outside the function. Local variables are very loyal to their parent function and live and die with them. Now suppose I define a variable num1=0  outside functions, it is global. A global variable can be accessed within a function but cannot be modified, unless we declare the variable to be ‘global’ by explicitly mentioning so within the function. eg:

global num1
num1 = 5

Variables local to a function cannot be accessed outside a function and will throw an error if we try to do so. Global variables are an easy way to manage handlers without a lot of programming overhead.
Anytime we want to assign a global variable within a function, we must declare it as global in the function. If we try to reference a global variable within a function, without declaring it as global (a mistake obviously), we get an error. However, if we just assign a global variable without declaring it as global (again a mistake ofcourse), Py assumes it is just a local variable and doesn’t throw an error, but the result will be wrong.

Python 1

I am trying to learn Python and I think sharing what I learn on my blog will not only serve as a quick reference for me in future but will also help those who are always on the lookout to find and learn fun things. So here goes my first post, a summary of a few very simple things I have learnt.

Event driven programming: This means your program will not run forever, rather it will run for sometime and then wait for an ‘event’ to occur like a mouse click or a text input or a button press and so on. The program flow then passes to a ‘handler’ specific to that event who knows what to do in case its event is fired.
Lets look at an example, this is from a module called simplegui. Yes Python functions are housed in these nifty little packages called ‘modules’. I think that’s really very cool.

So I import simplegui module saying

import simplegui

Then I have 3 timer functions in this module namely:

mytimer = simplegui.timer_create(delay in ms,myhandler) which will create and return a timer object

mytimer.start() which will start the timer

mytimer.stop() which will stop the timer.

So if I define a function like

def myhandler():
print "Hello Sir"

and I create and start a timer object, I will have “Hello Sir” printed at regular intervals ! BTW printing something like “Hello Sir” is just a trivial example, I can make anything to trigger at regular timings with the power of timers in my hands. Mwahaha !

A few notes: When a handler runs, no other the event handler can be run, so if another event occurs while a handler is running, the event just gets into the event queue and waits to be pulled out of the queue after the current handler finishes executing. So the queue does the trick.