Solved problem16 in python3