Midterm #1 Fall minutes

Similar documents
Midterm #2a Spring minutes

Midterm I Practice Problems

Midterm #1a Spring minutes

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

Midterm #2a Fall minutes

Midterm #1a Fall minutes

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

: Intro Programming for Scientists and Engineers Final Exam

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

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

Midterm Exam 2B Answer key

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Fall 2016 Midterm 2

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

Fundamentals of Programming. Functions Redux. Event-based Programming. File and Web IO. November 4th, 2014

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

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

CS 111X - Spring Final Exam - KEY

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

CS171 Midterm Exam. October 29, Name:

CS 111X - Fall Test 1

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

COMP102: Test 2: Part B Model Solutions

Computer Science Foundation Exam

Functionally Modular. Self-Review Questions

Spring 2017 CS 1110/1111 Exam 1

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Summer 2015 Midterm 1

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

Structure and Interpretation of Computer Programs Fall 2015 Midterm 1

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

CSE 131 Introduction to Computer Science Fall Exam II

CISC 1600 Lecture 3.1 Introduction to Processing

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

CMPSCI 119 Fall 2018 Thursday, November 29, 2018 Midterm #3 Solution Key Professor William T. Verts

CS 1110 Prelim 1 October 15th, 2015

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Computer Science Foundation Exam

Structure and Interpretation of Computer Programs

ECE264 Fall 2013 Exam 1, September 24, 2013

CS 1713 Introduction to Programming II

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

Types, lists & functions

Structure and Interpretation of Computer Programs

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Practical Questions CSCA48 Winter 2018 Week 11

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

CS 1110 Final, December 9th, Question Points Score Total: 100

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

Structure and Interpretation of Computer Programs

15-110: Principles of Computing, Spring 2018

Spring 2017 CS 1110/1111 Exam 3

CS 1110 Final, December 17th, Question Points Score Total: 100

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

SEMESTER 1, 2011 EXAMINATIONS. CITS1200 Java Programming FAMILY NAME: GIVEN NAMES:

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, Name: Andrew ID: Section:

Structure and Interpretation of Computer Programs

Overview of List Syntax

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Compsci 101 Fall 2015 Exam 1 Rubric. Problem 1 (24 points)

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

CMPSCI 119 Fall 2018 Wednesday, November 14, 2018 Midterm #2 Solution Key Professor William T. Verts

ECE264 Spring 2013 Exam 1, February 14, 2013

Structure and Interpretation of Computer Programs

Control Flow: Loop Statements

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Largest Online Community of VU Students

Question 1. December 2009 Final Examination Marking Scheme CSC 108 H1F

Structure and Interpretation of Computer Programs

CS Prelim 2 Review Fall 2018

Software Development Pseudocode

Computer Science & Engineering 150A Problem Solving Using Computers

CS 1713 Introduction to Programming II

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

Midterm Exam 2A Principles of Computing Fall November 10, 2014

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Structure and Interpretation of Computer Programs Spring 2016 Test 1

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

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

CS 115 Exam 3, Spring 2014

Transcription:

15-112 Midterm #1 Fall 2014 80 minutes Name: Andrew ID: @andrew.cmu.edu Section: INSTRUCTIONS 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! DO NOT WRITE IN THIS AREA Part 1 (T/F) Part 2 (SA) Part 3 (CT) Part 4 (RC) Part 5 (FR) Part 6 (FR) Part 7 (FR) Part 8 (FR) Part 9/bonus Total 5 points 15 points 15 points 8 points 12 points 12 points 15 points 18 points 5 points bonus 100 points

1. [5 pts; 1 pt each] True or False (circle one): A. TRUE or FALSE: If a function f(l) takes a list L and returns None and has no other side- effects (such as printing or drawing), we would expect f to destructively modify the list L, though it may not do so. B. TRUE of FALSE: If ((x == y)!= (x is y)) is False, then x is an alias of y. C. TRUE or FALSE: When using Polya step two, we write the test cases we will use to check our solution. D. TRUE or FALSE: If x is a key in some dictionary d, then x must be hashable (that is, hash(x) must return some integer value). E. TRUE or FALSE: In Python, functions can be used as arguments to other functions. 2. [15 pts; 3 pts each] Very Short Answers: Answer each of the following with only a few words or numbers. Really, do not write long answers! A. In solving wordsearch, we wrote a function which called a second function which in turn called a third function. What was the main purpose of the second (not the outermost, and not the innermost) function we wrote? B. Assuming selectionsort sorts largest numbers first, give an example of a list where bubblesort and selectionsort would each make the same 3 swaps in the same order to fully sort the list.

C. Assuming n>0, what is the worst- case big- oh runtime of f(n) in terms of n: Hint: it may help to think about the case where n is some power of 2. def f(n): k = 1 while (k < n): (n, k) = (n/2, k*2) # think carefully about what n equals now! for x in xrange(n): for y in xrange(n): k += abs(x - y) for z in xrange(n): k += abs(x + z) return (k < 123) D. Say that a function f(n) first creates a list L of n random numbers, then runs selectionsort on a copy of L, and then runs mergesort on another copy of L. If f(10**6) takes 5 seconds, about how long would you expect f(10**7) to take? E. State one advantage and one disadvantage of sets when compared to lists.

3. [15 pts; 3 pts each] Code Tracing. Indicate what each will print or draw: Statement(s): Prints or Draws: a = [(x%5)**2 for x in xrange(8,12)] print a, b = [str(val) for val in a] print [int(x) for x in sorted(b)] s = ("abcde" * 2).replace("e", "b") while (s.startswith("b") or s.endswith("b")): print len(s), s = s[1:- 1] print s def t3(a): (b, c) = (a, copy.copy(a)) a[0] = 3 b[0] = 4 c[0] = 5 print (a[0], b[0], c[0]), a = c a[0] = 6 b[0] = 7 c[0] = 8 print (a[0], b[0], c[0]), a = [1, 2] t3(a) print a[0]

Code Tracing continued. Statement(s): Prints or Draws: a = [range(1), range(2)] # not rectangular! (b, c) = (copy.copy(a), copy.deepcopy(a)) try: a[1] = c[1] a[0][0] = 3 b[0][0] = 4 c[1][0] = 5 c[2][0] = 6 except: c[1] = 7 print a, b, c # Note: the rectangle is for a reference. # Draw it wherever (in the space provided # on the right, that is) and at whatever # scale you want, but then everything # after it will be drawn inside it and # to its scale. canvas.create_rectangle(100, 100, 300, 300) canvas.create_arc(100, 200, 200, 300, start = 45, extent = 90) canvas.create_arc(100, 200, 200, 300, start = 225, extent = 90) for i in xrange(5): (y1, y2) = (100+50*i, 300-50*i) canvas.create_line(200, y1, 300, y2)

4. [8 pts; 4 pts each] Reasoning about code For each of the functions f, find values of the parameters so that f will return True. Place your answers in the box on the right of each function. def r1(n, row): a = [ ([0] * n) for r in xrange(n) ] counter = 1 # note: start at 1 not 0! for c in xrange(n): # note: col first for r in xrange(n): a[r][c] = counter counter += 1 return (a[row] == [3, 7, 11, 15]) n = row = def r2(l): s, t = (set(), "") for i in xrange(len(l)): w = L[i] assert((type(w) == str) and (len(w) == i)) for c in w: s.add(c) t += c # Hint: the next line assigns to s1 all # the chars in s, in sorted order s1 = "".join(sorted(s)) return ((s1 == string.ascii_lowercase[1:4]) and (t == s1 * 2)) L =

5. [12 pts] Free Response: nthroomyprime(n) We will say that a positive integer p is a roomy prime (a coined term) if p is prime and p is not within 15 (inclusive) of the previous roomy prime number. So 2 is a roomy prime, but 3, 5, 7, 11, 13, and 17 are not (they are all too close to 2), and so 19 is the next roomy prime. So we see: nthroomyprime(0) returns 2 nthroomyprime(1) returns 19 nthroomyprime(2) returns 37 With this in mind, write the function nthroomyprime(n) that takes a non- negative int n and returns the nth roomy prime number. You should assume that isprime(n) is already written for you.

6. [12 pts] Free Response: rowsetmap(a) Write the function rowsetmap(a) that takes a non- empty rectangular 2d list a, which you may assume contains only integer values, and returns a dictionary mapping each value in the 2d list to a set of indexes of the rows in which that value occurs in the 2d list. For example, here is a test case: a = [[2, 4, 6], [5, 5, 6]] assert(rowsetmap(a) == { 2:set([0]), 4:set([0]), 5:set([1]), 6:set([0, 1]) })

7. [15 pts] Free Response: beststudentandavg(gradebook) For this problem, a gradebook is a multiline string where each row contains a student s name (one word, all lowercase) followed by one or more comma- separated integer grades. A gradebook always contains at least one student, and each row always contains at least one grade. Gradebooks can also contain blank lines and lines starting with the # character, which should be ignored. With this in mind, write the function beststudentandavg(gradebook), that takes a gradebook and finds the student with the best average (ignoring the case where there is a tie) and returns a tuple of that student s name and his/her average (computed with integer division). For example, here is a test case: gradebook = """ # ignore blank lines and lines starting with #'s wilma,91,93 fred,80,85,90,95,100 betty,88 """ assert(beststudentandavg(gradebook) == ("wilma", 92)) Note: you most likely will want to use both s.split(",") and s.splitlines() in your solution.

8. [18 pts] Free Response: clickindot Here you will write a simple animation where a dot (a blue circle of radius 10) starts in the center of a 300x300 canvas. The dot sweeps left- to- right, so with each timerfired event, it moves to the right 5 pixels, and each time it completely exits the canvas to the right, it re- appears again on the left. If the user clicks in the dot, the game is over, the dot stops moving, and You win! is displayed. No message is displayed if the user misses the dot. That s it. Here, you need to write init, onmousepressed, ontimerfired, and redrawall. You can assume everything else is already written for you. You may abbreviate canvas.data as c.d.

[this space intentionally left (mostly) blank, in case you need more room ] Bonus/Optional: [2.5 pts] What will this print? def bonus1(s): r1 = r2 = r3 = 0 for d in [ord(c)- ord('a') for c in s.replace(" ","").lower()]: (r1, r2) = (2*r1+d if (d==int(bool(d))) else r1, r2+(d>1)*d) while (r1 > r2): (r1, r3) = (r1- r2, r3+1) return r3 print bonus1("abcb ABdBA") Bonus/Optional: [2.5 pts] What will this print? def bonus2(a,b): def r(a, f, s=0): for val in a: s = f(s,val) return s return r(range(a,b) + range(a- 1,b+1), lambda x,y: x^y) print bonus2(7, 130)