Accept three integers return them in order in a tuple of len

Accept 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). def rank3(x, y, z, ascending=True): return \"not implemented yet\" # Task 2: Allowing defaults, modifying a list in-place. # Remove multiple copies of val from xs. Note: # - Directly modify the list value that xs refers to. # - Return None, because the work happened in-place. # - You may only remove up to the first limit occurrences of val. # - When limit==None, there\'s truly no limit and we remove all # occurrences of val. # - Negative or zero limit: no removals. # def remove (val, xs, llmit=None): return \"not implemented yet\"

Solution

Task 1)

def rank3(x,y,z,ascending=True):
   l = [x,y,z]               # add numbers to list.
   l.sort(reverse = not ascending) # call sort method passing a param based on ascending true or false
   return tuple(l);       # convert list to tuple and return.
print rank3(1,3,2)

# sample output

# (1,2,3)

Task 2)

def remove(val,xs,limit=None):
   \'Recursive solution. Base condition is limit should be greater than zero and if val is in xs remove in xs and call the remove function with params val,xs and reducing limit by 1\'
   if(not limit):
       return
   else:
       if(val in xs):
           xs.remove(val)
           remove(val,xs,limit-1)
       else:
           return
xs = [1,2,1,1]
val = 1
remove(val,xs,2)
print xs

# sample output

# [2, 1]

 Accept three integers, return them in order in a tuple of length three. # An optional fourth argument (ascending) states whether output should be # sorted asce

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site