LongPal problem in Python