Dynamic problems solution in python