This quiz is open book and open notes, but do not use a computer.

Similar documents
Recall that strings and tuples are immutable datatypes, while lists are mutable datatypes. What does this mean?

The following content is provided under a Creative Commons license. Your support

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

This quiz is open book and open notes, but do not use a computer.

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Choice of C++ as Language

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

Introduction to Python

Lecture 5: Running Time Evaluation

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Algorithms and Programming

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Module 07: Efficiency

OBJECT ORIENTED PROGRAMMING

CSE373 Fall 2013, Midterm Examination October 18, 2013

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Lecture 2: Models of Computation

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Best-First Search Minimizing Space or Time. IDA* Save space, take more time

Theory and Algorithms Introduction: insertion sort, merge sort

Introduction to the Analysis of Algorithms. Algorithm

Data structure and algorithm in Python

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

>>> print( X ) [0,1,2,3] def mystery(l):??

INSTITUTE OF AERONAUTICAL ENGINEERING

Introduction to Algorithms 6.046J/18.401J/SMA5503

Introduction to Programming I

ASYMPTOTIC COMPLEXITY

MUTABLE LISTS AND DICTIONARIES 4

Introduction to Computer Science and Programming for Astronomers

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Beyond Blocks: Python Session #1

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

ECE250: Algorithms and Data Structures Midterm Review

Data Structures and Algorithms CSE 465

Structure and Interpretation of Computer Programs

Fundamentals of Programming. Week 5 - Lecture 2: Efficiency continued. Merge sort. Sets. Dictionaries.

CS150 Sample Final Solution

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

DECOMPOSITION, ABSTRACTION, FUNCTIONS

ASYMPTOTIC COMPLEXITY

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Lecture Agenda. Objects. But First... Immutable Types and Nesting. Immutable Types and Nesting

DM502 Programming A. Peter Schneider-Kamp.

6.00 Notes On Big-O Notation

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CS150 Sample Final. Name: Section: A / B

1.00 Lecture 4. Promotion

Introduction to Analysis of Algorithms

Computer Science Foundation Exam

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Midterm I Practice Problems

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

Introduction to Algorithms 6.046J/18.401J

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Data Structures and Algorithms. Part 2

Data Structures Lecture 3 Order Notation and Recursion

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CIS192 Python Programming. Robert Rand. August 27, 2015

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

CSI33 Data Structures

Lists How lists are like strings

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

CSC236H Lecture 5. October 17, 2018

COMP 161 Lecture Notes 16 Analyzing Search and Sort

1.204 Quiz 1. Spring Name. Exam guidelines:

ENGR 101 Engineering Design Workshop

Quiz 1 Practice Problems

Python: common syntax

CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

Search Lesson Outline

Munster Programming Training - Cycle 2

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

This quiz is open book and open notes, but do not use a computer (or cell phone!). You have 120 minutes.

Quiz 1 Practice Problems

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

CSC236 Week 5. Larry Zhang

Introduction to the Theory of Computation, Sipser, PWS, ISBN X, 1996

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Lecture 7. Memory in Python

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

DaMPL. Language Reference Manual. Henrique Grando

MEMOIZATION, RECURSIVE DATA, AND SETS

Algorithms for Bioinformatics

Recap from last Python lecture. Today: Revisit some objects. Update subsequence. What can sequences do? Update subsequence.

Transcription:

1. /15 2. /10 3. /10 4. /18 5. /8 6. /13 7. /15 8. /9 9. /1 10. /1 Total /100 This quiz is open book and open notes, but do not use a computer. Please write your name on the top of each page. Answer all questions in the boxes provided. 1) Are each of the following True or False (15 points) 1.1. In Python the values of a dict must be immutable. FALSE. The values of keys must be immutable. 1.2. There exist problems that cannot be solved in Python without using either iteration or recursion. TRUE. Otherwise, the complexity of programs would be bound by the length of the code. 1.3. Floating point arithmetic behaves exactly like normal arithmetic on real numbers. FALSE. Floating point provides only an approximation to real numbers. 1.4. On all inputs, a bisection search will run faster than a linear search. FALSE. It has a higher asymptotic complexity, but there can be inputs on which it will run more slowly. Consider, for example, searching for an element that happens to the first element of the list. 1.5. Let L be a list, each element of which is a list of ints. In Python, the assignment statement L[0][0] = 3 mutates the list L. FALSE, it mutates the list at L[0]. 1

2) What does the following code print? (10 points) T = (0.1, 0.1) x = 0.0 for i in range(len(t)): for j in T: x += i + j print x print i 0.1 0.2 1.3 2.4 1 2

3) What does the following code print? (10 points) def f(s): if len(s) <= 1: return s return f(f(s[1:])) + s[0] #Note double recursion print f('mat') print f('math') atm hatm 3

4) Implement the body of the function specified in the box. (18 points) def findall(wordlist, lstr): """assumes: wordlist is a list of words in lowercase. lstr is a str of lowercase letters. No letter occurs in lstr more than once returns: a list of all the words in wordlist that contain each of the letters in lstr exactly once and no letters not in lstr.""" def findall(wordlist, letters): result = [] letters = sorted(letters) for w in wordlist: w = sorted(w) if w == letters: result.append(w) return result 4

5) The following code does not meet its specification. Correct it. (8 points) def addvectors(v1, v2): """assumes v1 and v2 are lists of ints. Returns a list containing the pointwise sum of the elements in v1 and v2. For example, addvectors([4,5], [1,2,3]) returns [5,7,3],and addvectors([], []) returns []. Does not modify inputs.""" if len(v1) > len(v2): result = v1 other = v2 else: result = v2 other = v1 for i in range(len(other)): result[i] += other[i] return result insert the lines v1 = v1[:] v2 = v2[:] before the first line of executable code. 5

6) Consider the following code: def f(s, d): for k in d.keys(): d[k] = 0 for c in s: if c in d: d[c] += 1 else: d[c] = 0 return d def addup(d): result = 0 for k in d: result += d[k] return result d1 = {} d2 = d1 d1 = f('abbc', d1) print addup(d1) d2 = f('bbcaa', d2) print addup(d2) print f('', {}) print result 6.1) What does it print? (9 points) 1 5 {} 6.2) Does it terminate normally? Why or why not? (4 points) No, it terminates with a Error exception, because result will not be defined. 6

7) Consider the following code: def logbase2(n): """assumes that n is a positive int returns a float that approximates the log base 2 of n""" import math return math.log(n, 2) def f(n): """assumes n is an int""" if n < 1: return curdigit = int(logbase2(n)) ans = 'n = ' while curdigit >= 0: if n%(2**curdigit) < n: ans = ans + '1' n = n - 2**curDigit else: ans = ans + '0' curdigit -= 1 return ans for i in range(3): print f(i) 7.1) What does it print? (10 points) None n = 1 n = 10 7.2) Under the assumption that logbase2 is O(n), what is the order (use big Oh notation) of f? (5 points) O(n) 7

8) Next to each item in the left column write the letter labeling the item in the right column that best matches the item in the left column. No item in the right column should be used more than once. (9 points) Big O notation B a) induction Newton s method D b) upper bound Recursion A c) lower bound d) approximation e) expected running time f) exponential 9. Do you think that the lectures are too slow paced, too fast paced, about right? (1 point) 10. Do you think that the problem sets are too short, too long, about right? (1 point) 8

MIT OpenCourseWare http://ocw.mit.edu 6.00SC Introduction to Computer Science and Programming Spring 2011 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.