Solved problem23 in python