This must be in Python!!! As this is an Exercise, you can read any and all mater
ID: 3596420 • Letter: T
Question
This must be in Python!!!
As this is an Exercise, you can read any and all materials, ask us questions, talk with other students, and learn however you best learn in order to solve the task. Just create your own solution from those experiences, and turn in your work. We can define functions to name a block of code, and then feed it inputs and get an output. Functions also allow us to give default values for parameters, which is useful but needs special attention. We will write functions that are more than just side-effect-free input-output factories.
Notes
• Don't call input() or print() anywhere.
• We need you to figure out the "signature" (top line) of some functions, so we don't explicitly show you the parameters list those times. Part of the goal is learning how to choose when to use default parameters, and what default values to use.
• You're also learning when to modify the original or not; test cases should target this fact.
Guess the Signature
Designing a function's signature is an important skill. We haven't gotten a lot of practice yet, as we absolutely need your functions to work with our testers, and that has so far meant that we tell you exactly how to interface with the testing scripts. But now, we will describe the signature, and you have to get it correct based upon the needs of the function (how it's supposed to behave).
Tasks
• filter_chars: Write the definition for the filter_chars function. It accepts a message as the first argument (call it msg), and an optional argument called keeps as the second argument. Dig through msg, and any character in msg that is also present in keeps gets kept; all others are ignored/skipped. Return this possibly-shorter string.
o Strings can't be modified, so we can't modify the message. But we can create a new string that only contains the parts we wanted to keep.
o When keeps isn't provided, all upper and lowercase letters should be kept.
o Hint: the in keyword is your friend.
o Examples: § filter_chars("hi there! :)") 'hithere' § filter_chars("hi there! :)", "ether") 'hthere' § filter_chars("hi there! :)", "ie:") 'iee:' § filter_chars("hi there! :)","1234567890") '' 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.
o 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)
o Go ahead and use methods like .insert(), .pop(), .append()
o 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 filter_chars(msg,keeps=None):
result = ""
for c in msg:
if keeps:
if c in keeps:
result += c
else:
if c.isalpha():
result += c
return result
def relocate_evens(data, new_home=None):
new_list = []
i = 0
while True:
if data[i] % 2 == 0:
new_list.append(data[i])
data.pop(i)
else:
i = i+1
if i >= len(data):
break
if not new_home:
return new_list
new_home.extend(new_list)
return new_home
# copy pastable code link: https://paste.ee/p/TNfX5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.