If you have some functions that you use often, you can collect them in a module.
def f1(n): return n + 1
How to use it (
#!/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:
#!/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.
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
Take an integer and reverse its digits. The result is also an integer. Example: 83657 becomes 75638.
#!/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" which is a string -> reverse it, we get
int("75638") converts it to an integer.
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
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.
#!/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.
See http://www.scriptol.com/programming/fibonacci.php for a comprehensive list how to implement it in any language.
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”!
$ 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.
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:
#!/usr/bin/env python print "Please, reverse me completely!"
"!yletelpmoc em esrever ,esaelP" tnirp nohtyp vne/nib/rsu/!#
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"
This tip is from http://www.gossamer-threads.com/lists/python/python/658167.
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.
autoflush(True) # text that you want to print in unbuffered mode comes here autoflush(False) # back to normal
# 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