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: 3547025 • 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):
   ret = []
   n = listOrig[0]
   for i in range(1, len(listOrig)):
       if n > listOrig[i]:
           ret.append(listOrig[i])
       else:
           ret.append(n)
           n = listOrig[i]
   ret.append(n)
   return ret
  
print(ex_list([30,20,10,40]))
print(ex_list([30,20,10,40,50,10]))


//proper indentation: http://ideone.com/pZIQtQ

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