Better solution Problem 1 python

make a new general solution for the first problem in Python