

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to various programming exercises from a python tutorial, including questions related to legal/illegal code insertions, exception handling, command-line arguments, network programming, and file searching. Students can use this document as a reference to check their own solutions and understand the concepts.
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Name:
Directions: Work only on this sheet (on both sides, if needed); do not turn in any supplemen- tary sheets of paper. There is actually plenty of room for your answers, as long as you organize yourself BEFORE starting writing. In order to get full credit, SHOW YOUR WORK.
(a) : b.idnum = 1 after line 23
(b) : w = a.name + b.name after line 23
(c) : b = 12 after line 27 (not indented)
../b ../c/b
Here is the code:
import os, sys
def checkthisdir(targetfile,dr,flst): if ____________________: print os.path.join(____________________)
def findfile(dtroot,targetfile): os.path.walk(________________________)
def getwords(fi): wrds = map(________________________) return reduce(________________)
For example, if called on the file
a b cd xyz
uu vv
the value returned should be [’a’, ’b’, ’cd’, ’xyz’, ’uu’, ’vv’].
(Pdb) import bpl (Pdb) bpl.bpc(debugger) /a/b/tf.py 2 /a/b/tftest.py 1
Fill in the blanks in bpl.py:
def bpc(dbg): for f in ____________________: print f, ____________________
Solutions: 1.a Legal. Classes are implemented as dictionaries, and thus can be added to at any time. 1.b Legal. This is just string concatenation. 1.c Legal. The variable b is just a reference, i.e. pointer. You can use it to point to whatever type you wish.
cmdlnargs = sys.argv[1:]
import os, sys
def checkthisdir(targetfile,dr,flst): if targetfile in flst: print os.path.join(dr,targetfile)
def findfile(dtroot,targetfile): os.path.walk(dtroot,checkthisdir,targetfile)
def getwords(for instance): wrds = map(lambda u:u.split(),fi) return reduce(lambda x,y: x+y,wrds)
def bpc(dbg): for f in dbg.breaks.keys(): print f, len(dbg.breaks[f])