QUESTION 2: (2.5 points Write the definition of a function named sortMyList(inpu
ID: 656032 • Letter: Q
Question
QUESTION 2: (2.5 points Write the definition of a function named sortMyList(inputList, isAsc0 given an input list (as list type) and ascending/descending option (as bool type) that returns a new sorted list. You are not allowed to use any sort related list methods or built-in functions. Duplicate items can remain in the sorted list, if there are any. E.g. MyList 13,5, 12,1,5], Tru should return [1,3,5,5,12]. sortMyList(3,5, 12, 1,50, False) should return [12,5,5,3,1]. Hint: This method can be implemented in many ways. One way is to use min or max() list function to access items of interest one by one and use appendO,extendo) list functions or operator.
Explanation / Answer
def compare(item1, item2):
return (fitness(item1) < fitness(item2))
def fitness(item):
return item[0]+item[1]+item[2]+item[3]+item[4]
sorted(mylist, is Asc)
or
sorted(mylist, key=fitness)
or
sorted(mylist, is Asc, key=fitness)
or
sorted(mylist, cmp=lambda x,y: Asc(x,y))
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.