make python solution for problem 5 generic