Zigzag problem solved in Python