Quiz 6(b) Thu 2 Oct 2008

  1. Let L be a list of items. Give Python code to reverse the order of the items in L. For example L = ['a', 4, 'u'] becomes ['u', 4, 'a']. You may assume the list is not empty. Do not make any other assumptions on the length of the list.

    Answer:

                 n = len(L)
                 for i in range(0,n/2):
                    (L[i],L[n-i-1]) = (L[n-i-1],L[i])
    
  2. Define a function minus which takes on input one or two numbers. The output of minus(x) is -x and minus(x,y) returns x-y.

    Answer:

              def minus ( x, *y ):
                 if len(y) == 0:
                    return -x
                 else:
                    return x - y[0]