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

reverse.py (/coursework File Edit View Search Tools Documents Help import stdio

ID: 3741213 • Letter: R

Question

reverse.py (/coursework File Edit View Search Tools Documents Help import stdio import sys # Reverse th def reverse(a): Reverse the one-dinensional List a in place, ie. wi thout creating a new list # Iterate over half of the list a. Exchange element at i in a with the # element at len (a) -1-1. for i in: # Test rsesente lO NOT andlprinteadhesteversedrons standard input into a List def main): a stdio. readAlStrings() reverse(a) for v in al:-11: stdio.writef( SV) stdio.writeln(al-11) if name main)

Explanation / Answer

def reverse(a):

    # traverse through half the list

    for i in range(0, int( len(a) / 2 )):

   

        # swap the i th element from starting and ending

        temp = a[i]

        a[i] = a[ len(a) - i - 1 ]

        a[ len(a) - i - 1 ] = temp

       

a = [ 1 , 2 , 3 , 4 , 5 ]

reverse(a)

print(a)

Sample Output

[ 5 , 4 , 3 , 2 , 1 ]