Solution to problem22 in python (by mihaela)