Using python Using a default value that is complex: Watch out! relocate_evens: T
ID: 3852958 • Letter: U
Question
Using python
Using a default value that is complex: Watch out! relocate_evens: The first argument, data, contains integers. We will be removing any evens we find in data, and appending them to the second argument, new_home. A reference to the list which received any evens must be returned. When no second argument is given, a list of all the relocated evens should be created and returned. Careful! What default value do you want to use for new_home? What happens when we call the function multiple times in the same coding session? (try it out) Go ahead and use methods like insert(), pop(), append() Examples: > > > xs = [1, 2, 3, 4, 5] > > > relocate_evens(xs) [2, 4] > > > xs [1, 3, 5] > > > vals = [6, 7, 8, 9, 10] > > > sanctuary = [2, 4] > > > relocate_evens(vals, sanctuary) [2, 4, 6, 8, 10] > > > sanctuary # same as what relocate_evens returned [2, 4, 6, 8, 10] > > > vals # all evens have been relocated out of here. [7, 9]Explanation / Answer
def relocate_evens(a,b):
for i in a:
if(i%2==0):
b.append(i)
a.remove(i)
print(a)
print(b)
a=[1,3,5,6,7,8,9]
b=[]
try:
relocate_evens(a)
except:
relocate_evens(a,b)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.