# # Name: CS 111 Course Staff # # lab3btask3.py # # negate_evens([2, 3, 5, 8]) # # * what is the solution? # [-2, 3, 5, -8] # # * what is the next smaller subproblem? # negate_evens([3, 5, 8]) # # * what is the solution to that subproblem? # [3, 5, -8] # # * how can we use the solution to the subproblem to construct the # solution to the original problem? # return [-1 * 2] + [3, 5, -8] # # negate_evens([1, -4, -7, 6]) # # * what is the solution? # [1, 4, -7, -6] # # * what is the next smaller subproblem? # negate_evens([-4, -7, 6]) # # * what is the solution to that subproblem? # [4, -7, -6] # # * how can we use the solution to the subproblem to construct the # solution to the original problem? # return [1] + [4, -7, -6] def negate_evens(vals): """ uses recursion to return a version of the list vals in which all even number have been multiplied by -1 input: vals is an arbitrary list of integers """ if vals == []: return [] else: rest_negated = negate_evens(vals[1:]) if vals[0] % 2 == 0: return [-1 * vals[0]] + rest_negated else: return [vals[0]] + rest_negated