
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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 supplementary sheets of paper. There is actually plenty of room for your an- swers, as long as you organize yourself BEFORE starting writing. When appropriate, SHOW YOUR WORK.
def find1(adj,i): # find 1-hop neighbors of i, excluding i
nverts = ____________________________ nghbrs = [] for j in range(nverts): if _________________________________________:
return nghbrs
python SplitIn2.py f n f1 f
It splits the file f into files f1 and f2, where f1 consists of the first n bytes of f and f2 is the remainder. Fill the blanks. Make sure it is platform-independent.
import sys, os
def main(): f = open(_______________________________) n = int(sys.argv[2]) f1 = open(_______________________________) f2 = open(_______________________________) firstn = f.read(_______________________________)
f1.close() nbytesremaining = _____________________________ - n therest = _____________________________ f2.write(therest) f2.close()
if name == ’main’: main()
all1s = n[1] # a row of 1s x = nall1s # n rows of 1s
(a) (10) The above code fails to meet its goal. Show how to fix this by modifying the first statement.
(b) (10) Explain why is the above code, even after fixing, probably is not a good idea.
>>> dsort({’abc’:12,’sailing’:’away’,’aa’:15}) [[’aa’, 15], [’abc’, 12], [’sailing’, ’away’]]
Fill in the blanks:
def dsort(d): dk = _________________________________ dk.sort() return map(__________________________________)
def rgmx(x): xandi = ___________(x,____________________) return max(xandi)
Solutions:
len(adj) adk[i][j] == 1 and i != j nghbrs.append(j)
sys.argv[1],’rb’ sys.argv[3],’w’ sys.argv[4],’w’ n f1.write(firstn) os.path.getsize(f.name) f.read(nbytesremaining)
A better way would be to bypass computing nbytesre- maining and then simply call f.read(). 3.a
add1s = [n*[1]]
3.b The problem is that you would get n copies of the object pointed to by the reference all1s. If you then changed one element of the matrix, the whole column would change.
d.keys() lambda z: [z,d[z]],dk
zip range(len(x))