Introduction to Programming in Python, Aug-Dec 2014 Assignment 2 Due Sunday, 14 September, 2014 ---------------------------------------------------------------------- Instructions: 1. For each question, write a separate Python program with the filename indicated in the question. Zip your solutions into a single archive (any name is OK for the zip file) and submit the zip file via moodle. If you don't name your files correctly, you risk losing marks. 2. If the question asks for a function, you *must* write a function in Python (i.e., def f(x): ... ) Ensure that the function you define has the name indicated in the question. Also ensure that your function returns the value asked for, rather than printing it. If you add code to your file to test your function, make sure you *remove* this code when you submit your solution. The Python file you submit should have *only* the function definition. ---------------------------------------------------------------------- Problems 1. Filename: listdiff.py Write a Python function "listdiff(l1,l2)" that takes two lists l1 and l2 as input and returns all values that are either in l1 or in l2 but not in both lists. You may assume that each input list is sorted in ascending order and has distinct values (i.e., no duplicates). You may also assume that the two lists have elements of the same type. Your output should also be in ascending order. Do *not* sort the output --- you should generate the output values in ascending order. Here are some examples to show how your function should behave: >>> listdiff([1,3,5],[1,2,4,6]) [2,3,4,5,6] >>> listdiff([2,5],[2,5]) [] >>> listdiff(list(range(0,7,2)),list(range(1,8,2))) [0,1,2,3,4,5,6,7] 2. Filename: matmult.py A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix 1 2 3 4 5 6 would be represented as [[1,2,3],[4,5,6]]. Write a Python function "matmult(m1,m2)" that takes as input two matrices using this row-wise representation and returns the matrix product m1*m2 using the same representation. You may assume that the input matrices are well-formed and have compatible dimensions. If you look around you will probably find some readymade libraries in Python for this task. Do *not* use such any built-in libraries. Write the function from scratch. Here are some examples to show how your function should behave: >>> matmult([[1,2],[3,4]],[[1,0],[0,1]]) [[1,2],[3,4]] >>> matmult([[1,2,3],[4,5,6]],[[1,4],[2,5],[3,6]]) [[14, 32], [32, 77]] 3. Filename: minout.py Write a function "minout(l)" that takes a list l of distinct natural numbers (i.e. integers from the set {0,1,2,...}) and returns the smallest natural number not present in l. In other words, if 0 is not in l, then "minout(l)" returns 0, else if 0 is in l but 1 is not in l, them "minout(l)" returns 1, etc. Note that l is NOT assumed to be sorted. Here are some examples to show how your function should behave: >>> minout([1,3,2,4,17]) 0 >>> minout([1,3,0,2,4]) 5 ======================================================================