Solved problem18 in python3