Write a function that takes, as an argument, a list, and an integer, n. If the i
ID: 3757227 • Letter: W
Question
Write a function that takes, as an argument, a list, and an integer, n. If the integer is negative, the function should return a list containing two lists: the original list AND a list containing | n | zeros (recall that | n | means the absolute value of n). If the integer is 0, it should return the last element in the list. Otherwise, it should return the sum of the elements in the list. Name this function finalFunction(myList, n). For example, >>>finalFunction([1,2,3], -1) should return the list [[1,2,3], [0]]. As another example, >>>finalFunction([1,2,3], 0) should return the value 3. As a final example, >>>finalFunction([1,2,3], 5) should return the value 6. Use Python
Explanation / Answer
def finalFunction(myList, n): if n < 0: return [myList, [0] * abs(n)] elif n == 0: return myList[-1] else: total = 0 for num in myList: total += num return total print(finalFunction([1, 2, 3], -1)) print(finalFunction([1, 2, 3], 0)) print(finalFunction([1, 2, 3], 5))Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.