Solved problem24 in python