DOC PREVIEW
MIT 6 00 - Problem Set #7

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MIT OpenCourseWare http://ocw.mit.edu6.00 Introduction to Computer Science and ProgrammingFall 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.6.00 Problem Set 7 Out: Friday, October 17 Due: Tuesday, October 21 This problem set is designed to help you solidify your understanding of some material that we have covered in lecture, but not emphasized on the programming problems. You should do it, but do NOT hand it in. 1) What is the computational complexity of fact0? Explain your answer. def fact0(i): assert type(i) == int and i >= 0 if i == 0 or i == 1: return 1 return i * fact0(i-1) 2) What is the computational complexity of fact1? Explain your answer. def fact1(i): assert type(i) == int and i >= 0 res = 1 while i > 1: res = res * i i -= 1 return res 3) What is the computational complexity of makeSet? Explain your answer. def makeSet(s): assert type(s) == str res = '' for c in s: if not c in res: res = res + c return res 4) What is the computational complexity of intersect? Explain your answer. def intersect(s1, s2): assert type(s1) == str and type(s2) == str s1 = makeSet(s1) s2 = makeSet(s2) res = '' for e in s1: if e in s2: res = res + e return res5) Present a hand simulation of the code below. Describe the value to which each identifier is bound after each step of the computation. Note that “s1” and “s2” exist in more than one scope. def swap0(s1, s2): assert type(s1) == list and type(s2) == list tmp = s1[:] s1 = s2[:] s2 = tmp return s1 = [1] s2 = [2] swap0(s1, s2) print s1, s2 6) Present a hand simulation of the following code: def swap1(s1, s2): assert type(s1) == list and type(s2) == list return s2, s1 s1 = [1] s2 = [2] s1, s2 = swap1(s1, s2) print s1, s2 7) Present a hand simulation of the following code: def rev(s): assert type(s) == list for i in range(len(s)/2): tmp = s[i] s[i] = s[-(i+1)] s[-(i+1)] = tmp s = [1,2,3] rev(s) print


View Full Document

MIT 6 00 - Problem Set #7

Download Problem Set #7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Problem Set #7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Problem Set #7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?