DESCRIPTION Students: Please keep in mind the OMSI rules. Save your files often, make sure OMSI fills your entire screen at all times, etc. Remember that clicking CopyQtoA will copy the entire question box to the answer box. *********************************************************************** * * * Don't forget to FREQUENTLY save and submit your work, even work * * in progress. The network may get busy at the end of the exam * * period, making it difficult to submit then. The server will shut * * down at the appointed time, and NO further work will be accepted. * * * ********************************************************************** In questions involving code which will PARTIALLY be given to you in the question specs, you need to add new lines. There may not be information given as to where the lines should be inserted. Do not modify the lines already there unless instructed to do so. If a question includes test code, make sure to include it in your submission. IF YOU FINISH EARLY, please leave the exam room. If you stay, close your laptop and sit quietly. QUESTION -ext .py -run 'python omsi_answer1.py' The iterator class cq on p.121 may be somewhat inefficient, due to all that rearranging of the list self.q. (Note that although we used cq as a circular queue of clients, it was generally applicable.) Here you will modify it to a new class, cqIdxed, that simply makes use of a list index. The index starts at 0, increasing by 1 on each iteration, until it goes past the end of the list, upon which it reverts to 0. Complete the code: class cqIdxed: def __init__(self,q): self.q = q self.idx = 0 self.lenQ = len(self.q) def __iter__(self): return self myQ = cqIdxed([5,12,13]) i = 0 for qElement in myQ: print qElement # prints 5,12,13,5,12 i += 1 if i >= 5: break QUESTION -ext .py -run 'python omsi_answer2.py' Here you will write a class collDirs, "collection of directories", which will be an iterator for processing each directory in the collection. If an element of the collection turns out to not be a directory, the code will print an error message and raise a StopIteration exception. class dirColl(list): def __init__(self,dirList): self.dirList = dirList self.numDirs = len(dirList) self.i = 0 # indexes the collection def __iter__(self): return self import os try: os.mkdir('x') except: pass try: os.mkdir('y') except: pass try: f = open('z','w') f.writelines('123\n') f.close() except: pass dc = dirColl(['x','y']) for d in dc: print d # prints 'x', 'y' dc1 = dirColl(['x','z','y']) for d in dc1: print d # prints 'x', then 'z is not a directory'