Introduction to Programming in Python, Aug-Dec 2014 Assignment 1 Due Sunday, 31 August, 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. If you add code to your file test your function, make sure to *remove* this code when you submit your solution. The Python file you submit should have *only* the function definition. ---------------------------------------------------------------------- Problems 1. Filename: matched.py Write a Python function "matched(s)" that takes as input a string s and checks if the brackets "(" and ")" in s are matched: that is, every "(" has a matching ")" after it and every ")" has a matching "(" before it. Your function should ignore all other symbols that appear in s. Your function should return True if s has matched brackets and False if it does not. Here are some examples to show how your function should behave: >>> matched("zb%78") True >>> matched("(7)(a") False >>> matched("a)*(?") False >>> matched("((jkl)78(A)&l(8(dd(FJI:),):)?)") True 2. Filename: removeduplicates.py Write a Python function "removeduplicates(l)" that removes all duplicate values from l. Your function should retain the first copy of each distinct value --- in other words, you should preserve the order in which values appear in l. Duplicates could be simple values such as numbers or complex values such as strings and lists. You need to remove duplicates only at the top level of the list --- if a list contains another list as an element with duplicate values, the nested list can remain as it is. Your function should return a new list with duplicates removed. It not modify the input list l. Here are some examples to show how your function should behave: >>> xlist = [7,2,5,7,2,9] >>> newxlist = removeduplicates(xlist) >>> xlist [7,2,5,7,2,9] >>> newxlist [7,2,5,9] >>> xlist = [7,2,"hello",2,[5,5],"hello",9,[5,5]] >>> newxlist = removeduplicates(xlist) >>> xlist [7,2,"hello",2,[5,5],"hello",9,[5,5]] >>> newxlist [7,2,"hello",[5,5],9] 3. Filename: splitwith.py Write a Python function "splitwith(l,x)" that uses x as a separator to split the list l into sublists. Your function should return the list of sublists generated by splitting l in this way: all occurrences of x should be omitted. In other words, if your function returns the list of lists [l1,l2,...,lk] then it should be the case that l == l1 + [x] + l2 + [x] + ... + [x] + lk - If there is no occurrence of x in l, the value returned should be list consisting of a single inner list which is the entire input list l. - If x occurs at the first/last position of the list, one of the sublists (before/after x, respectively) will be the empty list. Here are some examples to show how your function should behave: >>> splitwith([7,2,5,7,[2,7],9],7) [[],[2,5],[[2,7],9]] >>> splitwith([7,2,5,7,[2,7],9],8) [[7,2,5,7,[2,7],9]] >>> splitwith([7,2,[8],8,[8],[[8],7],2,9],[8]) [[7,2],[8],[[[8],7],2,9]] ======================================================================