solved problem 18 in python