Sample list programs Write a function called negate(myList) which takes in a lis
ID: 3721278 • Letter: S
Question
Sample list programs Write a function called negate(myList) which takes in a list of numbers as a parameter and negates each value in the list. Since the function changes the list provided as a parameter it does not need to return anything Write a function called negativeCopy(myList), which takes in a list of numbers as a parameter and returns a newly created list, which is, contains the negative values of the original list. For example, For example, negativeCopy(l 1,-2, 3.5 4.2,) would return [-1, 2, -3.5,-4.2] Write a function called addList(myList, addend), which takes in a list of numbers and a single addend as parameters. The function returns a newly created list, which is each number in the original list plus, the addend. For example addList([1,2,3.5,4.2], 3) would return [4, 5,6.5, 7.2] Write a function called reverseList(myList), which takes in a list of items and returns a newly created list, which is the original list in reverse. For example reverseList( 1, 2, 3.5, 4.21) would return [4.2, 3.5, 2, 1 Write a function called removeltem(myList,item), which takes in a list and an itenm and returns a new list with each occurrence of the item removed from the original list. For example, removeltem([1, 2, "the", "test", 3, "is, "the", "best"], "the") would ret, 2, "test". 3, "is", "best"Explanation / Answer
print 'negating the same array'
array1 = [10,20,30,40,50]
def negate (array1):
for idx,val in enumerate(array1):
array1[idx] = -val
negate(array1)
for val in array1:
print val
print 'negating the array by creating a new list'
array1 = [10,20,30,40,50]
def negate (array1):
array2 = []
for val in array1:
array2.append(-val)
return array2
array2 = negate(array1)
for val in array2:
print val
print 'Adding a number to a list of numbers'
array1 = [10,20,30,40,50]
n = 5
def addList (array1,n):
array2 = []
for val in array1:
array2.append(val+n)
return array2
array2 = addList(array1,n)
for val in array2:
print val
print 'reversing a list'
array1 = [10,20,30,40,50]
n = 5
def reverseList (array1):
array2 = []
for val in array1:
array2 = [val]+array2
return array2
array2 = reverseList(array1)
for val in array2:
print val
print 'replacing a val in the list'
array1 = [10,20,30,40,50,'the']
item = 'the'
def reverseList (array1,item):
array2 = []
for val in array1:
if(val!=item):
array2.append(val)
return array2
array2 = reverseList(array1,item)
for val in array2:
print val
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.