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

This must be in Python!!! As this is an Exercise, you can read any and all mater

ID: 3596415 • 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.

Tasks

Implement each of these four functions. Returning multiple values: tupling up the results.

• def rank3(x,y,z, ascending=True): Given three integers, return them in order in a tuple of length three. An optional fourth argument (ascending) states whether output should be sorted ascending (argument is True) or descending (argument is False).

o Examples: § rank3(5, 3, 4) (3,4,5) § rank3(5, 3, 4, False) (5,4,3) § rank3(6, 8, 6) (6,6,8) Allowing defaults, modifying a list in-place:

• def remove(val, xs, limit=None): Remove multiple copies of val from xs (directly modify the list value that xs refers to). You may only remove up to the first limit occurrences of val. If limit==3, and xs had ten copies of val in it, then you'd only remove the first three and leave the last seven in place. When limit==None, there's truly no limit (and we remove all occurrences of val). Return None, because the work happened in-place. Negative or zero limit: no removals.

o hint: if you need to traverse a list from front to back, but you don't always want to go to the next index location, while loops can be very useful – we don't have to step forward each iteration.

o Note – some test cases are multiple lines (which we've avoided so far). You might need to notice the line number of a failed test, and go look at the code of that particular testing function, to see what's being attempted. This is a good habit to get into for projects.

o Examples: § >>> xs = [1,2,1,3,1,4,1,5,1] >>> remove(1,xs) >>> xs [2, 3, 4, 5] § >>> xs = [1,2,1,3,1,4,1,5,1] >>> remove(1,xs,3) >>> xs [2, 3, 4, 1, 5, 1]

Explanation / Answer

def rank3(x,y,z, ascending=True):
values = (x, y, z)
return tuple(sorted(values, reverse=(not ascending)))

def remove(val, xs, limit=None):
remCount = 0
i = 0
while True:
if val != xs[i]:
i = i + 1
elif (limit == None or remCount < limit):
xs.pop(i)
remCount += 1
else:
i = i +1
if i >= len(xs):
break

# copy pastable code: https://paste.ee/p/DGqy6

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