CS 331 Midterm Exam 1

Similar documents
CS 331 Summer 2017 Final Exam

CS 331 Midterm Exam 2

CS 331/401 Summer 2018 Midterm Exam

CS 331 Midterm Exam 2

CS 331 Midterm Exam 2

CSI33 Data Structures

CS 331 Fall 2017 Midterm Exam 2

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

PIC 16: Midterm. Part 1. In this part, you should not worry about why your class has the name that it does.

Structure and Interpretation of Computer Programs

CSI33 Data Structures

CS61A Lecture 36. Soumya Basu UC Berkeley April 15, 2013

Python itertools. Stéphane Vialette. October 22, LIGM, Université Paris-Est Marne-la-Vallée

Module 08: Searching and Sorting Algorithms

Extended Introduction to Computer Science CS1001.py

Guido van Rossum 9th LASER summer school, Sept. 2012

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

CS 1110 Prelim 2 April 21, 2015

Why efficiency of algorithm matters? CSC148 Intro. to Computer Science. Comparison of growth of functions. Why efficiency of algorithm matters?

CS 1110 Prelim 2 April 21, 2015 GRADING GUIDE

Iterators & Generators

Practice midterm exam

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

Programming Paradigms

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

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

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Figure 1: A complete binary tree.

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

Module 07: Efficiency

CIS192 Python Programming

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

PIC 16: Iterators and Generators

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

Iterators and Generators

OOP, Nonlocal, Trees, LLs, Growth Spring 2019 Guerrilla Section 3: March 16, 2019 Solutions 1 OOP. Questions

dnaseq.py Introduction to Algorithms Recitation 9b October 12, 2011

LECTURE 6 Python Basics Part 5

Structure and Flow. CS 3270 Chapter 5

TREES AND ORDERS OF GROWTH 7

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm 1 CSC148H1F L0201 (Liu)

Lecture no

pygtrie Release Jul 03, 2017

MEMOIZATION, RECURSIVE DATA, AND SETS

Python: Functions and Generators. Giuseppe Attardi

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Final Exam Solutions. Structure and Interpretation of Computer Programs. Fall 2011 /12 /16 /12 /18 /10 /12 /80 INSTRUCTIONS

Constraint Systems. Lab 06 - Customizing Search

Student Number: Lab day:

Structure and Interpretation of Computer Programs

Programming with Python

How async and await ended up in Python. EuroPython 2018, Edinburgh

Structure and Interpretation of Computer Programs

Execu&on Control Structures

Advanced topics, part 2

CPSC 217 Midterm (Python 3 version)

Due to the number of people in the room, you must stay for the entire exam.

ITERATORS AND GENERATORS 10

Fall 2017 December 4, 2017

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

Structure and Interpretation of Computer Programs

Hashing. So what we do instead is to store in each slot of the array a linked list of (key, record) - pairs, as in Fig. 1. Figure 1: Chaining

Python and Bioinformatics. Pierre Parutto

PyTrie Documentation. Release 0.3. George Sakkis

1. Efficiency of Algorithms. Programming and Algorithms II Degree in Bioinformatics Fall 2018

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Python iterators and generators

CS1 Lecture 22 Mar. 6, 2019

DSC 201: Data Analysis & Visualization

ITERATORS, GENERATORS, GENERIC FUNCTIONS

Computer Science 1001.py. Lecture 11: Recursion and Recursive Functions

CSI33 Data Structures

CS 1110 Final, December 16th, 2013

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

Algorithms and Programming

EXAMINATION INSTRUCTIONS

CS 1110 Prelim 2 November 14th, 2013

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

TUPLES AND RECURSIVE LISTS 5

CIS192 Python Programming

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

Midterm 2 Review Fall 2017 October 13, Lists & Tree Recursion. Instructions

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Python Iterators. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 19, 2011

CS1 Lecture 27 Mar. 26, 2018

Lecture 11: Recursion (2) - Binary search, Quicksort, Mergesort

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

About Variables in Python F E B 1 1 T H

DSC 201: Data Analysis & Visualization

ITERATORS, GENERATORS, GENERIC FUNCTIONS

Iterators and Generators

Transcription:

CS 331 Midterm Exam 1 Friday, October 6 th, 2016 Please bubble your answers in on the provided answer sheet. Also be sure to write and bubble in your student ID number (without the leading A ). 1. What are the contents of l2 after the following code is executed? l1 = ['one', 'two', 'three'] l2 = [(l1[i], l1[i+1]) for i in range(len(l1)-1)] (a) [] (b) [('one', three )] (c) [( two, one ), ( three, two )] (d) [('one', 'two'), ('two', 'three')] 2. What is the value of a after the following code is executed? a = 1 b = 2 for _ in range(5): a, b = b, 2*b (a) 1 (b) 25 (c) 32 (d) 64 3. What is the value of the expression dct[ fish ] after the following code is executed? words = ['one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish'] dct = {} for i in range(len(words)): w = words[i] if w in dct: dct[w][0] += 1 dct[w][1] = i else: dct[w] = [1, i] (a) [7, 4] (b) [4, 7] (c) [4, 1] (d) [9, 4] 1 of 13

4. For this and the next problem, consider the following function definition: def foo(a, b=5, c=10): return a+b+c What is the return value of the call foo(1, 2, 3)? (a) 1 (b) 6 (c) 5 (d) 16 5. What is the return value of the call foo(2, 3)? (a) 15 (b) 5 (c) 2 (d) 8 6. Consider the following function definition: def bar(*x): res = x[0] for x in x[1:]: res += x return res What is the return value of the call bar([1, 2], [4, 5], [9])? (a) 14 (b) [1, 4, 9] (c) [1, 2, 4, 5, 9] (d) [14, 7] 2 of 13

7. Consider the following class definition and subsequent code: class Foo: def init (self, name): global count self.identity = name + str(count) count += 1 count = 0 f1 = Foo('widget') f2 = Foo('sprocket') What is the value of the tuple (f1.identity, f2.identity)? (a) ( widget, sprocket ) (b) ( widgetzero, sprocketone ) (c) ( sprocket1, sprocket2 ) (d) ('widget0', 'sprocket1') 8. Consider the following class definition and subsequent code: class Bar: def init (self): self.total = 0 def getitem (self, key): self.total -= key return self.total def setitem (self, key, val): self.total += val def len (self): return self.total b = Bar() b[0] = 20 b[10] = 30 val = b[10] What is the value of the expression len(b)? (a) 40 (b) 50 (c) 60 (d) 10 3 of 13

9. Given that iterable is an iterable object, which of the following emulates the behavior of a for loop to iterate over its contents? (a) it = iterable while True: i = iter(it) x = next(i) # do something with x if not i: break (b) it = iter(iterable) while True: try: x = next(it) # do something with x except StopIteration: break (c) it = next(iterable) while True: try: x = iter(it) # do something with x except StopIteration: break (d) it = iter(iterable) while True: else: x = next(it) # do something with x raise StopIteration 4 of 13

10. Consider the following class definition and subsequent code: class MyIter: def init (self, x, y): self.x = x self.y = y def iter (self): return self def next (self): if self.x > self.y: raise StopIteration else: ret = self.x self.x *= 2 return ret l = [] for x in MyIter(3, 50): l.append(x) What are the contents of the list l? (a) [3, 50] (b) [3, 6, 12, 24, 48] (c) [53] (d) [6, 12, 18, 24, 30, 36, 42, 48] 5 of 13

11. What is the worst-case run-time complexity of locating and retrieving the element in middle position (by index) of an array-backed list of N elements? (a) O(1) (b) O(log N) (c) O(N) (d) O(N 2 ) 12. What is the worst-case run-time complexity of counting the number of times a given value occurs in an unsorted, array-backed list of N elements? (a) O(1) (b) O(log N) (c) O(N) (d) O(N 2 ) 13. What is the worst-case run-time complexity of using binary search to determine whether a given value exists in a sorted, array-backed list of N elements? (a) O(1) (b) O(log N) (c) O(N) (d) O(N 2 ) 14. What is the worst-case run-time complexity of extending an array-backed list with the contents of another list containing N elements? (a) O(1) (b) O(log N) (c) O(N) (d) O(N 2 ) 15. What is the worst-case run-time complexity of removing an arbitrary element from an array-backed list of N elements? (a) O(1) (b) O(log N) (c) O(N) (d) O(N 2 ) 6 of 13

16. Which of the plots best depicts the worst-case run-time complexity of the following function? def f(n): res = 0 while n > 0: res += n n = n // 10 return res 7 of 13

17. Which of the plots best depicts the worst-case run-time complexity of the following function? def f(lst): # `lst` is a Python list n = len(lst) - 1 while n >= 0: for i in range(len(lst)): if i!= n: lst[i] *= lst[n] n -= 1 return lst 8 of 13

18. Which of the plots best depicts the worst-case run-time complexity of the following function? def f(lst): # `lst` is a Python list res = 0 step = len(lst) // 10 for i in range(0, len(lst), step): res += lst[i] return res 9 of 13

19. Which of the plots best depicts the worst-case run-time complexity of the following function? def f(n): res = 0 for i in range(n, 0, -1): res += i return res 10 of 13

20. Which snippet correctly completes the implementation of insertion sort on a list? def insertion_sort(lst): for i in range(1, len(lst)): (a) for j in range(i, 0, -1): if lst[j-1] > lst[j]: lst[j-1], lst[j] = lst[j], lst[j-1] (b) for j in range(i, 1, -1): if lst[j-1] > lst[j+1]: (c) for j in range(i): lst[j-1], lst[j+1] = lst[j+1], lst[j-1] if lst[i] > lst[j]: lst[i] = lst[j] (d) for j in range(1, i+1): if lst[j] > lst[i]: lst[j] = lst[i] 21. Which snippet correctly completes the implementation of delitem in an array-backed list (assuming a valid index 0)? def delitem (self, idx): for i in : self.data[i-1] = self.data[i] del self.data[len(self.data)-1] (a) range(idx) (b) range(len(self.data)) (c) range(idx+1, len(self.data)) (d) range(len(self.data), idx, -1) 11 of 13

22. Which snippet correctly completes the implementation of iter in an array-backed list? def iter (self): (a) for x in self: yield x (b) while iter(self): yield next(self) raise StopIteration (c) for i in range(len(self)): return self.data[i] (d) for i in range(len(self)): yield self.data[i] 23. Which snippet correctly implements pop in an array-backed list, given a working delitem method and that self.data is a ConstrainedList (as in lab)? def pop(self, idx=-1): return val (a) val = self.data.pop(idx) (b) val = self[idx] self.remove(val) (c) val = self.data[idx] del self.data[idx] (d) val = self[idx] del self[idx] 12 of 13

24. Which snippet correctly implements splice in an array-backed list, which inserts the contents of the provided list argument (lst) into the array list starting at position idx? def splice(self, idx, lst): m = len(self) n = len(lst) for _ in range(n): self.data.append(none) E.g., calling splice with idx=2 and lst=['one', 'two', 'three'] on an ArrayList which currently contains [1, 2, 3, 4, 5] should result in [1, 2, 'one', 'two', 'three', 3, 4, 5] (a) for i in range(m+n, m, -1): self.data[i-1] = self.data[i-n-1] for i in range(n): self.data[idx+i] = lst[i] (b) for i in range(m, idx, -1): self.data[i+n-1] = self.data[i-1] for i in range(n): self.data[idx+i] = lst[i] (c) for i in range(idx, m+n): self.data[m+n-1] = self.data[m-1] for i in range(idx, idx+n): self.data[i] = lst[i] (d) for i in range(idx, m+n): self.data[m+n-i-1] = self.data[m-i-1] for i in range(idx, m): self.data[i] = lst[i] 13 of 13