Archive

Archive for September, 2010

Creating and importing a module

September 30, 2010 Leave a comment

If you have some functions that you use often, you can collect them in a module.

mymodule.py:

def f1(n):
    return n + 1

How to use it (use-mymodule.py):

#!/usr/bin/env python

import mymodule

print mymodule.f1(5)      # => 6
print mymodule.__name__   # => mymodule (note that .py is missing)

It is also possible to add some testing code to a module:

mymodule.py:

#!/usr/bin/env python

def f1(n):
    return n + 1

if __name__ == "__main__":
    number = 1977
    print f1(number)   # => 1978

Now, you can still import it like in use-mymodule.py, or you can launch it as if it were a standalone script. In the latter case the test suite will be executed. If you import it, the test suite is not executed. A test suite is a good practice to be sure that the module is working as expected.

Categories: python Tags: , ,

Prison break

September 29, 2010 Leave a comment

Exercise

The big boss of Montreal Prison celebrates his 50th birthday next week. For this special occasion he came up with the idea to let some prisoners go. In the prison there are 600 cells, one person in each. The cells are numbered from 1 to 600. The prison guard should go through all cells and open the doors. Then he goes to the 2nd cell and flips the lock at every second cell (that is, close the door if it was open and open it if it was closed). Then go the 3rd cell and flip the lock of every third cell, etc. This procedure should be repeated with every cell. Question: who are those lucky guys who get release, i.e. which cells remain open at the end?

Example with 8 cells:

00000000        initialization, all cells are closed
11111111        step 1, flip every lock
10101010        step 2, flip every 2nd lock
10001110        step 3, flip every 3rd lock
10011111        step 4, flip every 4th lock
10010111        step 5, flip every 5th lock
10010011        step 6, flip every 6th lock
10010001        step 7, flip every 7th lock
10010000        step 8, flip every 8th lock

Read more…

Categories: python Tags: , , ,

Reverse an integer

September 29, 2010 3 comments

Exercise

Take an integer and reverse its digits. The result is also an integer. Example: 83657 becomes 75638.

Solution

#!/usr/bin/env python

def reverse_int(n):
    return int(str(n)[::-1])

n = 83657
print n                 # 83657
print reverse_int(n)    # 75638

Summary: convert the number to string, reverse the string, then convert it back to integer. Details: 83657 -> str(83657) returns "83657" which is a string -> reverse it, we get "75638" -> int("75638") converts it to an integer.

Notes

If you want to concatenate a string and an integer, first you need to convert the integer to string. Example:

n = 83657
print "The value of n is " + n          # error, won't work
print "The value of n is " + str(n)     # OK
Categories: python Tags: , ,

Fibonacci numbers

September 28, 2010 1 comment

Exercise

Now an easy one. Calculate the first N Fibonacci numbers, where F0 = 0, F1 = 1, …, Fn = Fn-1 + Fn-2. Write a function that receives N as a parameter and returns a list with the first N Fibonacci numbers. Example: fib(10) would produce [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]. Notice that F0 = 0 is included in the result.

Solution

#!/usr/bin/env python

# fibonacci.py

def fib(n):
    assert(n >= 0)
    li = []
    a, b = 0, 1
    for i in range(n+1):
        li.append(a)
        a, b = b, a+b
    return li
            
if __name__ == "__main__":
    print fib(10)

Here we solved the problem in an iterative way, but you can do it with recursive calls too.

Links
See http://www.scriptol.com/programming/fibonacci.php for a comprehensive list how to implement it in any language.

Categories: python Tags: ,

A to Z, then Z to A

September 28, 2010 Leave a comment

Challenge

Write a Python script which prints the alphabet from “a” to “z”. Reverse its source code as explained in the previous post, i.e. reverse the order of lines and reverse the order of characters in each line. This reversed script should now print the alphabet from “z” to “a”!

Demonstration:

$ python fromAtoZ.py                                        
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ ./reverse-file.py fromAtoZ.py >za.py
$ python za.py 
z y x w v u t s r q p o n m l k j i h g f e d c b a
$ ./reverse-file.py za.py >az.py
$ python az.py 
a b c d e f g h i j k l m n o p q r s t u v w x y z
$ diff fromAtoZ.py az.py 
$

You can find the script reverse-file.py in my previous post. As you can see, applying reverse-file.py twice, you get back the original file.

Note that this challenge is not specific to Python. It can be solved in C++ too, for instance.

The solution is below, but first try to solve it yourself.
Read more…

Categories: python Tags: , , ,

Reverse a file

September 27, 2010 3 comments

Exercise

Take a text file and reverse it the following ways: (1) reverse characters in each line, and (2) reverse the order of lines too. Let’s see an example:

Input:

#!/usr/bin/env python
print "Please, reverse me completely!"

Output:

"!yletelpmoc em esrever ,esaelP" tnirp
nohtyp vne/nib/rsu/!#

Read more…

Categories: python Tags: , ,

Autoflush

September 27, 2010 Leave a comment

Printing to the standard output is buffered. What to do if you want to see the output immediately?

import sys
import os

# reopen stdout file descriptor with write mode
# and 0 as the buffer size (unbuffered)
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

print "unbuffered text"

Credits

This tip is from http://www.gossamer-threads.com/lists/python/python/658167.

Update (20130206)
The solution above switches buffered mode off, but you can’t switch it back on because you lose the original sys.stdout file descriptor. I have a more sophisticated solution, available here (autoflush.py) as part of my jabbapylib library.

Usage #1:

autoflush(True)
# text that you want to print in unbuffered mode comes here
autoflush(False)    # back to normal

Usage #2:

# using a context manager
with AutoFlush():
    # unbuffered text comes here
    sys.stdout.write(...)

# here you are back to normal

Let’s not forget the simplest and most trivial solution either:

sys.stdout.write(...)
sys.stdout.flush()    # flush out immediately

StringBuilder functionality in Python

September 27, 2010 Leave a comment

Problem

You need to concatenate lots of string elements. Under Java we use a StringBuilder for this, but how to do that in Python?

Solution #1

Use a list, and join the elements of the list at the end. This is much more efficient than concatenating strings since strings are immutable objects, thus if you concatenate a string with another, the result is a NEW string object (the problem is the same with Java strings).

Example:

def g():
    sb = []
    for i in range(30):
        sb.append("abcdefg"[i%7])

    return ''.join(sb)

print g()   # abcdefgabcdefgabcdefgabcdefgab

Solution #2 (update 20120110)

Use a StringIO object and print to it. In short:

from cStringIO import StringIO

out = StringIO()
print >>out, 'arbitrary text'    # 'out' behaves like a file
return out.getvalue()

First 15 digits of PI

September 27, 2010 2 comments

Look at this verse:

How I want a drink
alcoholic of course
After the heavy lectures
involving complex functions

Take the length of the words and you get the first 15 digits of PI. Here is the proof:

import sys
import math

s = """
How I want a drink
alcoholic of course
After the heavy lectures
involving complex functions
"""

print [len(w) for w in s.split()]
print math.pi

Output:

[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
3.14159265359   # the last digit is rounded here

Read more on PI at http://www.eveandersson.com/pi/.

And if you didn’t know, here is the PI song :)

 
Update (20110317)

You can approximate the value of PI with 355/113. The first 6 decimal places are the same. It’s quite easy to memorize it: visualize 113355, split into two (113 and 355), then do the division.

>>> import math
>>> math.pi
3.1415926535897931
>>> 355/113.
3.1415929203539825

Ref.: Kee Nethery at python-list.

Categories: python Tags: , ,

Play sound

September 26, 2010 1 comment

In Python, for playing sound there are several options (see http://stackoverflow.com/search?q=python+play+sound). However, under Ubuntu I didn’t have much success with portable solutions :( I got most luck with wx.Sound('sound.wav') which used to work for a while but then it stopped… So I’ve decided to just call mplayer to do the job.

#!/usr/bin/env python

import os

SOUND = 'Gong.wav'

command = 'mplayer %s 1>/dev/null 2>&1' % SOUND
os.system(command)

Not too elegant but at least it works…

Notes

If you want to launch mplayer in the background without any verbosity, here is how to do that:

mplayer music.mp3 &>/dev/null </dev/null &

Update (20101016): I forgot to mention that with the help of mplayer, you can play videos too. Just pass an AVI instead of an MP3, for instance.

Categories: python Tags: , , , , ,