Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part B Write a function that adds an arbitrary integer quantity to every element in a list. This is just like Part A, except instead
Part B Write a function that adds an arbitrary integer quantity to every element in a list. This is just like Part A, except instead of adding 1 to each element, you add some positive integer x to each one This function should: be named add_to_list take 2 arguments: the list, and the number return a list: the new list that contains the added elements In this function, you should loop through each element of the argument list, add the second argument to it, and then return the new list. For example, add_to_list([1, 2, 3], 1) should return [2, 3, 4], just like in Part A. add_to_list([1, 2, 3], 5) should return (6, 7, 8] . No imports! HINT: I would highly recommend constructing a new list, rather than updating the old one. ]: Graded Read Only -]: import numpy as np np.random.seed (2846) list1 = np.random.randint(-100, 100, 10) num1 = 15 actual1 = listi + num1 pred1 = add_to_list(listi.tolist(), num1) assert set (pred1) == set(actual1.tolist) ]: np.random.seed(68527) Read Only list2 = np.random.randint(-100, 100, 100) num2 = np.random.randint(0, 100) actual2 = list2 + num2 pred2 = add_to_list(list2.tolist(), num2) assert set (pred2) == set(actual2.tolist()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started