CS 1110 Regular Prelim 1, March 14th, 2017

Similar documents
Acct. balance Acct. balance 50 25

CS 1110 Final, May 2017

CS 1110 Prelim 1, March 2018

CS 1110 Final Exam, May 2018

CS 1110 Prelim 2 April 22, 2014

CS 1110 Prelim 2 April 26th, 2016

CS 1110 Prelim 1 March 15th, 2016

CS 1110 Prelim 1 March 7th, 2013

CS 1110 Prelim 1 October 4th, 2012

CS 1110 Prelim 1 March 10, 2015

CS 1110 Final Exam May 9th, 2013 SOLUTIONS

CS 1110 Prelim 1 October 17th, 2013

CS 1110 Prelim 2 November 14th, 2013

CS 1110 Prelim 2 November 6th, 2012

CS 1110 Prelim 2 November 12th, 2015

CS 1110 Prelim 1 October 15th, 2015

CS 1110 Prelim 2 November 12th, 2015

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

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

CS 1110 Final, December 16th, 2013

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

Last Name: First Name: Cornell NetID, all caps: CS 1110 Regular Prelim 1 Solutions March 2018

CS 1110 Final, December 16th, 2013

CS 1110 Prelim 2 November 13th, 2014

CS 1110 Prelim 2 November 10th, 2016

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

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

CS 1110 Prelim 2 April 21, 2015

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

CS 1110 Prelim 1 October 12th, 2017

Lecture 4. Defining Functions

CS 4620 Midterm, March 21, 2017

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

CS Prelim 1 Review Fall 2017

Lecture 4. Defining Functions

Do not turn this page until you have received the signal to start.

CS Prelim 1 Review Fall 2016

COS 126 General Computer Science Spring Written Exam 1

CS 1110 Prelim 1 March 7th, 2013 SOLUTIONS

CS Prelim 1 Review Fall 2018

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Student Number: Instructor: Brian Harrington

Lecture 5. Defining Functions

CS Prelim 1 Review Fall 2013

Lecture 4. Defining Functions

Total Score /1 /20 /41 /15 /23 Grader

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Final exam. CS 2110, December 15, 2016, 9:00AM

Overview of List Syntax

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

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

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

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

PREPARING FOR PRELIM 1

Final Examination, Semester 1, 2015 COMP10001 Foundations of Computing

CS 1301 Exam 1 Fall 2010

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Do not turn this page until you have received the signal to start.

Do not turn this page until you have received the signal to start.

CS Prelim 1 Review Fall 2012

XC Total Max Score Grader

CS100J Prelim 1 22 February 2007

Preliminary Examination I Computer Science 312, Cornell University 6 March 2003

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Notices. Test rules. Page 1 of 8. CS 1112 Spring 2018 Test 2

Data Structures and Algorithms 2018

CS Prelim 1 Review Fall 2013

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

Prelim 1. CS 2110, March 15, 2016, 7:30 PM Total Question Name True False. Short Answer

Duration: 90 minutes. Page 1 of 14 Q2: /13 Q3: /13 TOTAL: /38. Bonus /1

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Do not turn this page until you have received the signal to start.

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CS 1044 Program 6 Summer I dimension ??????

CSE 413 Final Exam. June 7, 2011

Total Score /15 /20 /30 /10 /5 /20 Grader

CS 1110 Final Solutions May 2017

COS 126 General Computer Science Spring Written Exam 2

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

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

Structure and Interpretation of Computer Programs

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

Statement of integrity: I did not, and will not, violate the rules of academic integrity on this exam.

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

Total Score /15 /20 /30 /10 /5 /20 Grader

CS Prelim 2 Review Fall 2018

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Python I. Some material adapted from Upenn cmpe391 slides and other sources

AP CS Fall Semester Final

CSE 303 Midterm Exam

Transcription:

Last Name: First Name: Cornell NetID, all caps: CS 1110 Regular Prelim 1, March 14th, 2017 This 90-minute exam has 9 questions worth a total of 104 points. You may tear the pages apart; we have a stapler at the front of the room. The second page of this exam gives you the specifications for some useful functions and methods. You will be expected to write Python code on this exam. We recommend that you draw vertical lines to make your indentation clear, as follows: def foo(): if something: do something do more things do something last It is a violation of the Academic Integrity Code to look at any exam other than your own, to look at any other reference material, or to otherwise give or receive unauthorized help. We also ask that you not discuss this exam with students who are scheduled to take a later makeup. Academic Integrity is expected of all students of Cornell University at all times, whether in the presence or absence of members of the faculty. Understanding this, I declare I shall not give, use or receive unauthorized aid in this examination. Signature: Date

For reference: s.find(substr) Returns: index of first occurrence of string substr in string s (-1 if not found) s.strip() Returns: copy of string s where all whitespace has been removed from the beginning and the end of s. Whitespace not at the ends is preserved. s.split(sep) Returns: a list of the words in string s, using sep as the word delimiter (whitespace if sep not given) s.join(slist) Returns: a string that is the concatenation of the strings in list slist separated by string s s[i:j] Returns: if i and j are non-negative indices and i j-1, a new string containing the characters in s from index i to index j-1, or the substring of s starting at i if j len(s) lt.append(item) lt.remove(item) lt.index(item) lt[i:j] lt.pop(i) map(func, lt) Adds item to the end of list lt Removes the first occurrence of item from list lt. Returns: index of first occurrence of item in list lt; raises an error if item is not found. (There s no find for lists.) Returns: A new list[lt[i], lt[i+1],..., lt[j-1]] under ordinary circumstances. Returns [] if i and j are not both sensible indices Returns: element of list lt at index i and also removes that element from the list. Returns: A list obtained by applying function func to each element in list lt and concatenating all of the results. Question Points Score 1 17 2 16 3 10 4 16 5 7 6 17 7 10 8 10 9 1 Total: 104 Page 2

1. Object Diagramming and Terminology. (a) [8 points] Suppose there is a class Acct defined in file a2.py where Acct objects have a balance attribute. Consider the following code and associated memory diagrams. Eight items in the diagram have been labeled (A) through (H). Match the vocabulary terms below the diagram with a labeled example by writing a letter next to each term. 1 from a2 import * 2 def swap(source, to): 3 """Swap balance of Acct <source> and balance of Acct <to>""" 4 tmp = source.balance 5 source.balance = to.balance 6 to.balance = tmp 7 8 a1 = Acct(50) 9 a2 = Acct(25) 10 swap(a1,a2) A a1 a2 id1 id2 D id1 E Acct balance 25 50 id2 F Acct balance 50 25 B C swap 4 5 6 G source id1 to id2 tmp 50 H RETURN None Local Variable: Global Variable: Parameter: Attribute: Object: Call Frame: Heap Space: Global Space: Page 3

(b) [9 points] Consider the following 6 lines entered in Python interactive mode. Diagram all variables and objects created at the end of execution. Do not diagram any call frames. >>> x = 1 >>> y = x >>> x = 2 >>> p = [1,2] >>> q = p >>> p[1] = 5 Page 4

2. String processing, testing. In the US, 10-digit telephone numbers are typically represented in one of the two following styles: Parenthetical : (555) 666-1110 Dashed : 555-666-1110 There is no whitespace in a Dashed phone number: they are all exactly 12 characters long. There is only one space in a Parenthetical phone number, and it is after the ) ; they are all exactly 14 characters long, counting the space. (a) [10 points] Implement the following function according to its specification. def phone_to_paren(s): """ Returns: a string representing the phone number s in Parenthetical form. Precondition: s is a non-empty string that *would* be a valid Dashed phone number EXCEPT that it possibly has spaces around the dashes. Examples: '555-666-1110' '555-666 - 1110' '555-666-1110'... all yield the same result, '(555) 666-1110' """ Page 5

(b) [6 points] Consider the following function specification. def area_code(s): """ Returns: an int representing the area code (first three digits) of a telephone number in string s. Precondition: s is a non-empty string that *would* be a valid Dashed or Parenthetical phone number, EXCEPT it possibly has spaces around the dashes.""" Write three conceptually distinct test cases for this function in the table below. Test case #1 Input and expected output: Rationale: Test case #2 Input and expected output: Rationale: Test case #3 Input and expected output: Rationale: Page 6

3. [10 points] Time for Objects! The Time class is defined so that Time objects have two attributes, hours and minutes. Write the body for the function below so that it implements its specification. You may assume it is being defined in the same file as the definition for Time, so that you can create a new Time object with, say, 1 hour 30 minutes via the call Time(1,30). def mirror_time(t): """Modifies Time object t to be its "mirror image" on the clock. Does NOT create a new object; does not return a value. To get a "mirror image" time: 1. If the hours are 12, then the hours should stay the same. Otherwise, take 12 and subtract the hours to get the new hours. 2. If the minutes are 0, then the minutes should stay the same. Otherwise, take 60 and subtract the minutes to get the new minutes. Examples: 10:10 -> 2:50 12:00 -> 12:00 4:30 -> 8:30 Precondition: t is a Time object with hours <= 12, minutes <= 59 """. Page 7

4. [16 points] Banking on objects. Suppose you are working on a file that defines two classes: Acct: Acct objects have a float attribute, balance Person: Person objects have two attributes: partner, which is either a Person object or None bank_acct, which is an Acct object. Inside the same file that defines the classes Acct and Person is also the following function header and specification. Write the body of the function so that it implements its specification. def groupem(p1, p2): """ 1. Creates a new Acct whose balance is the sum of the balances of p1 and p2's bank_acct objects; 2. Changes the balances of both p1's bank_acct and p2's bank_acct to 0 3. Changes both p1's bank_acct and p2's bank_acct to the new Acct 4. Makes the partner of p1 be p2 and the partner of p2 be p1. Preconditions: p1 and p2 are Persons whose partners are both None.""" # hint: if p1 is a Person, p1.bank_acct is an Acct. So you can write # (p1.bank_acct).balance, or even p1.bank_acct.balance Page 8

5. [7 points] The import of import. Suppose file andersen.py defines a function is_the_one that takes a string as input, performs some computation, and returns a Boolean. And, suppose file lee.py also defines a function is_the_one that takes a string as input, but performs some possibly different computation, and returns a Boolean. Finally, suppose you are writing code in a third file matrix.py, which is currently empty. Write code to be placed in the third file that: stores in variable neo the result of calling the andersen.py version of is_the_one on the string "Thomas"; and stores in variable oracle the result of calling the lee.py version of is_the_one on the string "Thomas" 6. For-loop analysis. Consider the following function header and specification: def vowel_in_common(s1,s2): """Let the vowels be defined as a, e, i, o, u. If there is some vowel v contained in both s1 and s2, return a list of the index of the first v in s1 and the index of the first v in s2. If there is more than one such vowel v, v = the alphabetically first one. If s1 and s2 have no vowel in common, return the list [-1, -1]. Preconditions: s1 and s2 are non-empty strings. Examples: vowel_in_common("brad", "angelina") -> [2, 0] vowel_in_common("romeo", "romeo") -> [3, 3] vowel_in_common("dan", "phil") -> [-1, -1] """ Page 9

Here is one proposed implementation of vowel in common. It may or may not be correct. for v in ['a', 'e', 'i', 'o', 'u']: i1 = s1.find(v) i2 = s2.find(v) if i1!= -1 and i2!= -1: return [i1, i2] else: return [-1, -1] (a) [6 points] Suppose someone makes the call indices = vowel in common("brad", "angelina"), What is the value of local variable v just before the function returns? What is the return value of the call? (b) [4 points] Suppose someone makes the call indices = vowel in common("romeo", "romeo"), What is the value of local variable v just before the function returns? What is the return value of the call? Page 10

(c) [7 points] Here is an alternative proposed implementation of vowel in common. It may or may not be correct. found = "" # first vowel found in both for v in ['a', 'e', 'i', 'o', 'u']: if v in s1 and v in s2 and found == "": found = v if v == "": return [-1, -1] else: return [s1.index(v), s2.index(v)] Suppose someone makes the call indices = vowel in common("brad", "angelina"), For the alternative implementation, what is the value of local variable v just before the function returns? What is the value of local variable found just before the function returns? What is the return value of the call? Page 11

7. [10 points] Lists. The Towers of Hanoi is a famous math puzzle that involves moving circular disks (with a hole in the middle) from one tower to another. There are three towers: left, middle, and right, and each disk has a unique size. The goal is to move all of the disks from the left tower to the right tower without putting a larger disk on top of a smaller disk. 1 2 3 4 left middle right The towers can be represented in Python as lists of integers. Each disk has its own unique associated integer. The first element in the list represents the bottom of the tower, and the last element represents the top. For example, the above setup could be represented as: left = [4, 3, 2, 1] middle = [] right = [] Below is the specification of a function move to move disks (integers) from one list to another. For example, after executing the following code: left = [4, 3, 2, 1] middle = [] move(left, middle) left should contain [4, 3, 2] and middle should contain [1]. def move(from_tower, to_tower): '''Tries to move a disk from from_tower to to_tower. More specifically, * if to_tower is empty, or if to_tower is not empty and its last element is larger than the last element of from_tower, then removes the integer at the end of from_tower and appends it to the end of to_tower. * otherwise, does nothing. Precondition: from_tower and to_tower are lists of integers representing disks. from_tower has at least one disk. to_tower may be an empty list. Procedure, not a fruitful function, so no return.''' Implement this function on the next page. Page 12

# Put your code for function move below. Page 13

8. [10 points] String Processing. Write this function s body so that it implements its specification. def glue(name1, stop1, name2, start2): """Returns: -1 if stop1 is not a valid index for name1 or if start2 is not a valid index for name2 Otherwise, returns a new string formed by concatenating: the substring of name1 starting at index 0 and ending at index stop1 with the substring of name2 starting at index start2 and going to the end Preconditions: name1 and name2 are non-empty strings of lowercase letters stop1 and start2 are nonnegative ints Examples: glue("jules", 1, "vincent", 3) -> "jucent" glue("jules", 4, "vincent", 4) -> "julesent" glue("jules", 2, "vincent", 0) -> "julvincent", glue("jules", 5, "vincent", 4) -> -1 glue("jules", 1, "vincent", 100) -> -1 """ 9. [1 point] Write your last name, first name, and Cornell NetID at the top of each page. Did you write your name and netid on each page, and re-read all specs, and check your code works against test cases? Page 14