Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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))
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote