Python Programming Exercise Solutions - Prof. Norman S. Matloff, Exams of Computer Science

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-0n6
koofers-user-0n6 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
1. (15) Which of the following would be legal/illegal if in-
serted into the program tfe.py in Sec. 5.1 of our Python
tutorial PLN? Write “legal” or “illegal.” Each is indepen-
dent of the others, i.e. you are not being asked whether it
would be legal to insert more than one of them together.
(a) : b.idnum = 1 after line 23
(b) : w = a.name + b.name after line 23
(c) : b = 12 after line 27 (not indented)
2. (10) Suppose an exception occurs which our program
does not handle using try...except. The name of the de-
fault function which is called at that time is .
3. (10) Write a single line of code which produces cmdl-
nargs, a list consisting of all the command-line argu-
ments excluding the name of the program being run.
(Note: Code like if x == y: z = 8 counts as two lines,
as if the second part were on a new line.)
4. (10) In our Python network programming PLN, we
didn’t call an analog of the function gethostbyname()
seen in our network tutorial PLN, because that function’s
actions are included in the Python function .
Similarly, the information returned by getpeername()
in the network tutorial is included in what is returned by
the Python function .
5. (15) Fill in the blanks in the following pair of func-
tions which find all files of a given name (regardless of
whether they be ordinary files, directories or links) in a
given directory tree. Each file is reported in terms of the
path relative to dtroot. Example: The tree starts at
/z/a, which contains an ordinary file band a directory
c; the latter contains a directory b; the program is run
from within c, with ‘..’ as dtroot and ‘b’ as targetfile.
Then the output will be
../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(________________________)
6. (20) Fill in the blanks in the following lines of code,
which returns a list of all words in a text file. It is pre-
sumed that the file has already been opened by the calling
program, with fibeing assigned the result of open(), but
no other operations have been performed on the file.
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’].
7. (20) Suppose you are debugging a multi-module pro-
gram by using pdbw.py. You wish to know how many
breakpoints you’ve set in each module. You will be able
to do so using a module bpl.py. For example, in debug-
ging our example of tf.py and tftest.py in Sec. 9.1.1 of
our Python tutorial PLN, we might have:
(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 bis just a reference, i.e. p ointer.
You can use it to point to whatever type you wish.
2. sys. excepthook (); half credit for
sys.excepthook()
3.
cmdlnargs = sys.argv[1:]
4. socket.connect();socket.accept()
5.
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)
1
pf2

Partial preview of the text

Download Python Programming Exercise Solutions - Prof. Norman S. Matloff and more Exams Computer Science in PDF only on Docsity!

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.

  1. (15) Which of the following would be legal/illegal if in- serted into the program tfe.py in Sec. 5.1 of our Python tutorial PLN? Write “legal” or “illegal.” Each is indepen- dent of the others, i.e. you are not being asked whether it would be legal to insert more than one of them together.

(a) : b.idnum = 1 after line 23

(b) : w = a.name + b.name after line 23

(c) : b = 12 after line 27 (not indented)

  1. (10) Suppose an exception occurs which our program does not handle using try...except. The name of the de- fault function which is called at that time is.
  2. (10) Write a single line of code which produces cmdl- nargs, a list consisting of all the command-line argu- ments excluding the name of the program being run. (Note: Code like if x == y: z = 8 counts as two lines, as if the second part were on a new line.)
  3. (10) In our Python network programming PLN, we didn’t call an analog of the function gethostbyname() seen in our network tutorial PLN, because that function’s actions are included in the Python function. Similarly, the information returned by getpeername() in the network tutorial is included in what is returned by the Python function.
  4. (15) Fill in the blanks in the following pair of func- tions which find all files of a given name (regardless of whether they be ordinary files, directories or links) in a given directory tree. Each file is reported in terms of the path relative to dtroot. Example: The tree starts at /z/a, which contains an ordinary file b and a directory c; the latter contains a directory b; the program is run from within c, with ‘..’ as dtroot and ‘b’ as targetfile. Then the output will be

../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(________________________)

  1. (20) Fill in the blanks in the following lines of code, which returns a list of all words in a text file. It is pre- sumed that the file has already been opened by the calling program, with fi being assigned the result of open(), but no other operations have been performed on the file.

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’].

  1. (20) Suppose you are debugging a multi-module pro- gram by using pdbw.py. You wish to know how many breakpoints you’ve set in each module. You will be able to do so using a module bpl.py. For example, in debug- ging our example of tf.py and tftest.py in Sec. 9.1.1 of our Python tutorial PLN, we might have:

(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.

  1. sys. excepthook (); half credit for sys.excepthook()

cmdlnargs = sys.argv[1:]

  1. socket.connect(); socket.accept()

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])