Python solution for first problem