python solution for problems 18 and 67