Midterm #2a Spring minutes DO NOT WRITE IN THIS AREA

Similar documents
Midterm #2a Spring minutes

Midterm #2a Fall minutes DO NOT WRITE IN THIS AREA

Midterm #2a Fall minutes

Midterm #1a Fall minutes

Midterm #1a Spring minutes

Midterm #1 Fall minutes

Midterm I Practice Problems

Structure and Interpretation of Computer Programs

15-110: Principles of Computing Sample Exam #1

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

15-110: Principles of Computing, Spring 2018

Fundamentals of Programming. Recursion - Part 2. October 30th, 2014

Structure and Interpretation of Computer Programs

CPSC 217 Midterm (Python 3 version)

Structure and Interpretation of Computer Programs Summer 2014 Midterm 1

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

CSE 160 Spring 2018: Final Exam

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

ANS:

CS 115 Exam 3, Spring 2015, Sections 1-4

CS 373: Combinatorial Algorithms, Spring 1999

Computer Science Foundation Exam

Structure and Interpretation of Computer Programs

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

Midterm I - Solution CS164, Spring 2014

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

CS116 - Module 5 - Accumulative Recursion

Structure and Interpretation of Computer Programs

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

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

CS 115 Exam 3, Spring 2014

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CSC 110 Final Exam. ID checked

Structure and Interpretation of Computer Programs

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

: Intro Programming for Scientists and Engineers Final Exam

The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind.

University of California, Berkeley College of Engineering

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

Structure and Interpretation of Computer Programs

CMPSCI 119 Spring 2015 Final Exam Friday, May 1, 2015 Solution Key

Structure and Interpretation of Computer Programs

CS171 Midterm Exam. October 29, Name:

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

Midterm Exam 1 Solutions

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

Structure and Interpretation of Computer Programs Fall 2015 Midterm 2

CS 115 Exam 2 (Section 1) Spring 2017 Thu. 03/31/2017

CSE wi: Practice Midterm

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

COS 126 Midterm 1 Written Exam Fall 2011

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

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

COT 3100 Spring 2010 Midterm 2

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

CMSC 201 Spring 2019 Lab 06 Lists

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

22C:16 CS:1210 Exam 2

Midterm Exam 2B Answer key

You may not share any information or materials with classmates during the exam and you may not use any electronic devices.

Structure and Interpretation of Computer Programs

Programming Training kiddo. Main Points: - Python Statements - Problems with selections.

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

EE 355 Lab 3 - Algorithms & Control Structures

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2016 Test 1

CS 1301 CS1 with Robots Summer 2007 Exam 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

15-110: Principles of Computing, Spring 2018

Structure and Interpretation of Computer Programs

Write code that simulates one roll of a 6-sided die and prints out the

CMSC 201 Spring 2017 Lab 05 Lists

Computer Science Foundation Exam

Midterm 1 Review Fall 2017 September 8, Functions. Instructions

Control Flow: Loop Statements

Practice Midterm 1. Problem Points Score TOTAL 50

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

CSE 190D Spring 2017 Final Exam Answers

Structure and Interpretation of Computer Programs

***PRACTICE EXAM 02 for*** CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Actual exam scheduled for: 11/18/2009

FINAL EXAM May 2017 CSC /02: Introduction to Computer Science

CSE 341 : Programming Languages Midterm, Spring 2015

CSE332 Summer 2010: Midterm Exam Sample Solutions

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE341 Spring 2016, Midterm Examination April 29, 2016

Signature: ECE 551 Midterm Exam

Fall 2017 Midterm Exam 1 October 19, You may not use any books, notes, or electronic devices during this exam.

Structure and Interpretation of Computer Programs

2016 Spring COP 3502 Final Exam. Date: 4/28/2016. Name:

CS 1301 Exam 1 Fall 2010

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CS32 Final Exam. E03, F15, Phill Conrad, UC Santa Barbara

Recursion CS GMU

CS 100 Spring 2016 Midterm 1

ITERATORS AND STREAMS 9

CMSC 201 Spring 2017 Lab 12 Recursion

CS1 Lecture 15 Feb. 18, 2019

Transcription:

15-112 Midterm #2a Spring 2016 80 minutes Name: Andrew ID: @andrew.cmu.edu Section: You may not use any books, notes, or electronic devices during this exam. You may not ask questions about the exam except for language clarifications. Show your work on the exam (not scratch paper) to receive credit. If you use scratch paper, you must submit it with your andrew id on it, and we will ignore it. All code samples run without crashing. Assume any imports are already included as required. When problems specify to use recursion, you must use it meaningfully. In such cases, you may use wrapper functions or add optional parameters if it helps. DO NOT WRITE IN THIS AREA Part 1 (CT) Part 2 (RC) Part 3 (DB) Part 4 (SA) Part 5 (FR) Part 6 (FR) Part 7 (FR) Part 8 (FR) Part 9/bonus Total 10 points 5 points 5 points 16 points 16 points 16 points 16 points 16 points 5 points bonus 100 points

1. [10 pts] Code Tracing Indicate what these will print: Statement(s): Prints: def ct1(l): if (len(l) % 3 < 2): return L else: k = len(l)//2 + 1 return [sum(l), ct1(l[:k])] L = [-1, 0, 1, 2] * 2 print(ct1(l)) # Hint: prints 5 integers overall # Be sure to show the nested # lists properly Statement(s): def ct2(n): def f(n): if (n < 10): return (n, n) else: (a, b) = f(n//10) d = n%10 if (d > a): a = d elif (d < b): b = d return (a, b) return f(n) print(ct2(3276)) # Hint: prints a tuple of # 2 integers Prints:

2. [5 pts] Reasoning about code For this function, find values of the parameters so that the function will return True. Place your answers in the box on the right of the function. def rc1(s): def f(s): if (len(s) < 2): return s else: return s[-1] + f(s[:-1]) t = "" while (s!= ""): t += s[0] s = f(s[1:]) return (t == "Yes!") s = 3. [5 pts] Debugging You are provided with an example from the course notes. This function has one bug added or one line deleted, however. Find and circle the bug or missing line in the code, being sure to circle just the code that has the bug and no additional code. Then, fill in the box below the function with code to replace the code you circled so this function works correctly. def printfiles(path): if (os.path.isdir(path) == False): # base case: not a folder, but a file, so print its path print(path) else: # recursive case: it's a folder for filename in os.listdir(path): printfiles(filename)

4. [16 pts] Short Answer Answer each of the following very briefly. A. When you write a new class in Python, how do you define how the == operator works over instances of that class? B. In the course notes, we provide a default hash method with this line: return hash(self.gethashables()) This may appear to be recursive, because our hash method is calling hash again. However, it is not recursive. Very briefly, why not? C. In the following, assume L is a list, and functions g and h are both defined already: def f(l): if (len(l) == 0): return g(l) else: return h(l[0]) + f(l[1:]) Rewrite the function f(l) so that it works the same, only without using any recursion. You may use iteration here.

D. Why does memoization speed up fib(n) so much more than fact(n)? E. If a level-0 Sierpinski Triangle is a solid black triangle, draw a level-2 Sierpinski Triangle. F. Say we start with a 4x5 board and fill the black squares as shown below (in the code from our notes, the black squares would be green, and the white squares would be cyan). Then we rightclick where the star is to start a floodfill from there. On the left, write the numeric labels for the depths that result in each cell after the floodfill completes. On the right, write the numeric labels for the ordinals. depths ordinals Hint #1: the numbers on the left are depths, so some labels may occur more than once. Hint #2: the numbers on the right are ordinals, so labels must occur only once each. Hint #3: the first label is 0, not 1 (so a 0 should be where the star is). Hint #4: Our floodfill code recursively tries to go up, then down, then left, then right.

5. [16 pts] Free Response: maketriangle(n) Using recursion meaningfully, and without using iteration or string multiplication, write the function maketriangle(n) that takes a positive integer n and returns a multi-line string that is a triangle of asterisks, where the first line contains n asterisks. Here is a sample test case for you: assert(maketriangle(3) == "***\n**\n*") Reminder: while you cannot use iteration, you may use recursive helper functions here if you wish.

6. [16 pts] Free Response: containsself(l) Background: in Python, it is possible for a list to contain itself. Not a copy of itself, but an actual alias to itself. For example: L = [ ] L.append(L) # we just added an alias of L to itself! print(l[0] is L) # True! With this in mind, write the function containsself(l) that takes a list L and returns True if L contains an alias to itself at any level (including sublists of L, sublists of sublists of L, etc), and False otherwise.

7. [16 pts] Free Response: Playlist and Song classes Write the Playlist and Song classes so the following test code works. You may not use the Struct class from the course notes, and you may not hardcode the specific test cases. song1 = Song("Happy", 10) assert((song1.title == "Happy") and (song1.rating == 10)) song2 = Song("Joyful") assert((song2.title == "Joyful") and (song2.rating == 6)) playlist1 = Playlist([song1, song2]) assert(playlist1.songs == [song1, song2]) assert(playlist1.averagerating() == 8) # average of 10 and 6 playlist2 = Playlist([song1]) assert(playlist2.songs == [song1]) assert(playlist2.averagerating() == 10) assert(not (playlist1 == playlist2)) playlist2.addsong(song2) assert(playlist2.songs == [song1, song2]) assert(playlist2.averagerating() == 8) assert(playlist1 == playlist2)

[this page is blank (for Playlist and Song classes)

8. [16 pts] Free Response: increasingpathcount(board) Background: we will say that a board is a square 2d list of integers. As with mazes, a solution is a path from the left-top to the right-bottom, only here we will only allow moves that are up, down, left and right (no diagonals). A solution is an "increasing path" (a coined term) if each value on the solution path is strictly larger than the one before it on that path. Consider this board: board = [[ 1, 3, 2, 4 ], [ 0, 4, 0, 3 ], [ 5, 6, 8, 9 ], [ 0, 7, 8, 9 ]] This board has exactly one increasing path: right to 3, down to 4, down to 6, down to 7, right to 8, right to 9. With this in mind, write the function increasingpathcount(board) that takes such a board and returns the number of increasing paths running from the left-top to right-bottom in that board. For the example board above, your function would return 1. Similarly, consider this board: board = [ [3, 5], [4, 7] ] For this board, your function would return 2 -- those paths being right,down and also down,right. For full credit, you need to use backtracking, so that you do not explore every possible path, but instead you backtrack once you know a subpath cannot lead to a solution. Important note: if you cannot get your function to properly return the counts, then for most-butnot-all of the credit you can instead return True if there are any solutions (that is, the count is positive) and False otherwise (that is, the count is 0).

[this page is blank (for increasingpathcount))

9. Bonus/Optional: Bonus/Optional: [2.5 pts] What will this print? def bonusct1(g=lambda a:a+2, a=3): def f(s,n=10): return "f("*n + str(s) + n*")" (s, f) = (f(f(f("a", 5))), g) return(eval(s)) print(bonusct1()) Bonus/Optional: [2.5 pts] What will this print? def bonusct2(x, y, L=None): class A(object): def init (s, n): s.n = n def eq (s, t): try: t = t.n; L.append(t); return (s.n==t or s==a(t//10)) except: return False L=L or []; return(a(x) == A(y) and L) print(bonusct2(5, 567), bonusct2(567, 5))