Solution to few puzzles in Python