I'd been working through a problem, to sort the elements in a descending order through a recursive approach, the code is as follows..
import operator
def do_stuff(elem_list):
if not elem_list:
return None
max_index , max_element = max(enumerate(elem_list) , key = operator.itemgetter(1))
elem_list[max_index],elem_list[0] = elem_list[0],elem_list[max_index]
return do_stuff(elem_list[1:])
p_list = [4,2,3,5,1]
do_stuff(p_list)
print(p_list)
Output -
[5, 2, 3, 4, 1]
And I can't seem to figure wherein lies the problem and why won't I get the desired output ?
elem_list[1:]is a completely separate list fromelem_list.l1 = [1, 2, 3]; l2 = l1[1:]in interactive mode and see what effect changing one list has on the other.