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

Description: Given a list of numbers modify it so that you exchange contiguous e

ID: 3547024 • Letter: D

Question

Description:

Given a list of numbers modify it so that you exchange contiguous elements one by one, starting to exchange the first element wiht the second element, in general exchanghing the element in position k with the element in positionk+1 until reaching the previous to last position (which gets exchanged with the last element).

The exchange however should not be done always, but only if the element in the lower position is higher than its neighbour in the contiguous higher position. Assume that the list has at least two elements.

An example:

Explanation / Answer

def ex_list(listOrig):

i=0;

while(i<len(listOrig)-1):

curr=listOrig[i];

next=listOrig[i+1];

if curr>next:

listOrig[i]=next;

listOrig[i+1]=curr;

else:

listOrig[i]=curr;

i=i+1;

return listOrig;


list = [30,20,10,40];

print ex_list(list);


#######################SAMPLE OUTPUT#######################


C:UserssmallipedhiDesktopPython>List1.py

[20, 10, 30, 40]


C:UserssmallipedhiDesktopPython>