CS 1110 Prelim 1 March 7th, 2013 SOLUTIONS

Similar documents
CS 1110 Prelim 1 March 7th, 2013

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

CS 1110 Prelim 2 April 26th, 2016

CS Prelim 1 Review Fall 2013

CS Prelim 1 Review Fall 2012

CS 1110 Regular Prelim 1, March 14th, 2017

CS Prelim 1 Review Fall 2017

CS 1110 Prelim 1, March 2018

CS Prelim 1 Review Fall 2018

PREPARING FOR PRELIM 1

CS Programming I: Arrays

Announcements for this Lecture

CS 1110: Introduction to Computing Using Python Lists and Sequences

CS 1110 Final Exam May 9th, 2013 SOLUTIONS

CS Prelim 1 Review Fall 2016

CS 1110 Prelim 1 October 4th, 2012

CS Prelim 1 Review Fall 2013

ECSE 321 Assignment 2

CSE 341 Section Handout #6 Cheat Sheet

Last Name: UTION First Name: SOL Cornell NetID: CS1110. Solution: CS 1110 Prelim 2 April 26, 2016

CS 1110 Prelim 2 April 22, 2014

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

CS 1110 Prelim 2 November 14th, 2013

CS 1110 Prelim 1 October 17th, 2013

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

Types, Operators and Expressions

Excel Functions & Tables

CS 1110 Prelim 1 March 15th, 2016

Lecture 10: Lists and Sequences

CS 1110 Final, May 2017

CS 1110 Prelim 1 March 10, 2015

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year.

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

CS 1110 Prelim 2 November 13th, 2014

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

CS 1110 Final Exam, May 2018

CS 1110 Prelim 1 October 15th, 2015

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

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

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

Types, Operators and Expressions

Section 1.2: What is a Function? y = 4x

Intro. Scheme Basics. scm> 5 5. scm>

Asks for clarification of whether a GOP must communicate to a TOP that a generator is in manual mode (no AVR) during start up or shut down.

CS 1110 Final Exam Solutions May 15th, 2014

CS 1110 Prelim 2 November 6th, 2012

File Handling in C. EECS 2031 Fall October 27, 2014

Lecture 17: Classes (Chapter 15)

Acct. balance Acct. balance 50 25

Lecture 10: Boolean Expressions

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CMIS 102 Hands-On Lab

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

FREQUENTLY ASKED QUESTIONS

CS 1110: Introduction to Computing Using Python Loop Invariants

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Operator overloading: extra examples

Lecture 12. Lists (& Sequences)

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

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

Assignment 5: Due Friday April 8 at 6pm Clarification and typo correction, Sunday Apr 3 3:45pm

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

CS 1110, LAB 1: PYTHON EXPRESSIONS.

Lecture 4. Defining Functions

15-110: Principles of Computing, Spring 2018

CS 1110 Prelim 2 Solutions April 2018

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

Text Input and Conditionals

Lecture 2: Variables & Assignments

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Strings, Lists, and Sequences

XC Total Max Score Grader

SELECTION. (Chapter 2)

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

CS 1110 Final, December 16th, 2013

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Introduction to Computer Programming for Non-Majors

CS150 Sample Final. Name: Section: A / B

Lecture 7: Objects (Chapter 15) CS 1110 Introduction to Computing Using Python

ASYMPTOTIC COMPLEXITY

This report is based on sampled data. Jun 1 Jul 6 Aug 10 Sep 14 Oct 19 Nov 23 Dec 28 Feb 1 Mar 8 Apr 12 May 17 Ju

Lecture 1. Types, Expressions, & Variables

CS 101 Spring 2006 Final Exam Name: ID:

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Structure and Interpretation of Computer Programs

CS1 Lecture 3 Jan. 18, 2019

Structure and Interpretation of Computer Programs

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

UAE PUBLIC TRAINING CALENDAR

CS1110 Lab 1 (Jan 27-28, 2015)

Problem Solving for Intro to Computer Science

CS 1110 Prelim 2 November 12th, 2015

CS1 Lecture 3 Jan. 22, 2018

Structure and Interpretation of Computer Programs

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

Transcription:

Last Name: Ution First Name: Sol Cornell NetID: SU1110 Circle your lab: Tu 12:20 Tu 1:25 Tu 2:30 Tu 3:35 W 12:20 W 1:25 W 2:30 W 3:35 CS 1110 Prelim 1 March 7th, 2013 SOLUTIONS 1. When allowed to begin, write your last name, first name, and Cornell NetID at the top of each page, and circle your lab time on the top of this page. Every time a student doesn t do this, somewhere, a kitten weeps. More seriously, we did have an exam come apart during grading, so it is actually important to write your name on each page. Also, remember that if we need to figure out your lab section at the end of the grading session (roughly 2am this time around), our chances of putting it the wrong pile, and thus you not being able to find it when you get to lab, grow high. 2. [16 points] Match the shaded parts of the following Python program to the names below. In your answer, each letter should occur exactly once. I Assignment statement Q Name of a function being called C Name of a function being defined J/R Boolean expression D Parameter H/R Argument K String literal L Integer literal B List F List indexing G/P Function call expression P Method call expression E Docstring O Comment M Conditional expression A Name of global variable being created N Name of local variable being created F/H Reference to an attribute of an object A month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] B C D def date_time_str(dt, twelve_hour): """Return the date and time in the object <dt>, in the format MMM DD, YYYY HH:MM:SS Example: Mar 7, 2013 19:30:00 if the boolean <twelve_hour> is True, the time is given in 12-hour format with AM or PM appended. Example: Mar 7, 2013 7:30:00 PM """ # Format the date as a string F date_str = month_names[dt.month] + " " + str(dt.day) + ", " + str(dt.year) # Adjust the time for 12-hour clock if required G H hour = dt.hour I if twelve_hour: J K time_suffix = (" AM" if dt.hour < 12 else " PM") hour = (12 if hour == 0 else (hour if hour <= 12 else hour - 12)) else: N L M time_suffix = "" O # Format the time part of the string P time_str = ':'.join(map(str, [hour, dt.minute, dt.second])) # Assemble the result from the values computed so far return date_str + " " + time_str + time_suffix Q R print date_time_str(get_current_time(), True) E

3. [16 points] Two students were assigned to diagram the execution of the following code. You are their grader; please circle all errors and write in anything that is missing. You may wish to do this question by first drawing the relevant frames and objects yourself. In the first purported solution, the 25 should be a 22 (so the second solution got this right!) and the 18 should be 15. def f(x, y): c = 3*x y[0] = b + c + y[1] c = 0 def g(a, b): f(2, a) return b + a[0] b 4 c 5 x 25 g: 1/ 2/ a f: 1/ 2/ 3/ x b 7 2 y 0 1 4/ 18 5 list b = 4 c = 5 x = g([b,c], 7) b 4/ 15 g: 1 c 6/ 0 c 5/ 6/ 0 a b 7 x 22 0 1 b c list f: 1 x 2 y Page 2

4. [4 points] Here, we consider a simplified version of extracting information from a web page. Assume that variable x stores a string of the form <a href="string1">string2</a> where both string1 and string2 are strings that do not contain double quotes or angle brackets. The only space in the format shown above is after the first a, although string1 and string2 may themselves contain spaces. Example: if x were the string '<a href=" this "> that</a>', then string1 would be ' this ' and string2 would be ' that'. Write a sequence of one or more statements that result in variable s2 holding the string string2. # There are several possible solutions; here s one. i1 = x.index('>') i2 = x.rindex('<') s2 = x[i1+1:i2] 5. This question involves code for suggesting new NetIDs. Assume file last.py defines a type of object called LastUsed. These have two attributes: prefix suffix non-empty string of lowercase letters positive int and can be created by calls like this: last.lastused('djs', 98) (if last has been imported). File last.py also implements the function ind(lulist, p) with the following spec: def ind(lulist, p): """Returns: index in lulist of LastUsed object with prefix p (-1 if no such object) Preconds: lulist is a (possibly empty) list of LastUsed objects with distinct prefixes. p is a non-empty string of lowercase letters.""" (a) [8 points] Draw all objects and variables created by the following sequence of commands. (Don t draw any frames.) temp = [last.lastused('ljl', 2), last.lastused('srm',2)] has_srm = last.ind(temp, 'srm') Page 3

(b) [12 points] On the next page(s), complete file nets.py by following the helpful directions given in curly braces. Each such direction can require multiple lines to implement. For reference, here are some functions and the like you can use: x in lt Returns: True if x is in list lt, False otherwise. lt.append(x) Append object x to the end of list lt. lt.pop(i) Returns: item at position i in list lt, removing it from lt. If i is omitted, returns and removes the last item. lt.sort() Sort the items of lt, in place (the list is altered). # nets.py {Omit other authoring info.} def newid(fname, mname, lname, all_last): """Returns: NetID for new Cornellian named fname mname lname. For people with the same initials, gives out sequentially numbered NetIDs starting with the number 1. The new NetID is a string of this person's initials (first initial coming first) and the next available numerical suffix, according to all_last. The list all_last keeps track of which NetIDs have been used; it contains a LastUsed object for each set of initials that has been used in a NetID, with the highest number that has been given out so far. It is modified to account for the new NetID returned by this function. {Don t worry, we explain how to do this in the remarks below.} For instance, if all_last started out empty, and then the NetIDs abc1, foo1, and abc2 are generated, all_last should contain two LastUsed objects: one with prefix 'abc' and suffix 2, and one with prefix 'foo' and suffix 1. Preconditions: all arguments are strings containing only lowercase letters. The lengths of fname and lname are at least 1. The list all_last contains LastUsed objects indicating which NetIDs have been used.""" Page 4

# Many solutions were possible. # A common error was to try something like if inits in all last. The problem is # that all last is a list of LastUsed objects, not strings, and inits is a string. # slicing trick for mname==''; if-stmt/conditional expr. was expected inits = fname[0] + mname[0:1] + lname[0] i = last.ind(all last, inits) # inits is new iff i is -1 if i!= -1: all last[i].suffix = all last[i].suffix + 1 suf = all last[i].suffix else: all last.append(last.lastused(inits,1)) suf = 1 return inits + str(suf) 6. [8 points] Complete the body of testing procedure testnew for function newid from the previous problem. 1 You may make at most five calls to newid. Our grading will focus on the completeness of your test cases: they should cover the space of possible arguments with which newid could be called. To save time on this exam, do not directly check whether the argument list has been correctly modified; only directly check whether newid s return value is correct. import nets import cunittest2 def testnew(): """Test the newid fn in nets""" Many solutions were possible. lt = [] # test if correct when list is empty cunittest2.assert equals('srm1', nets.newid('sa', 'ra', 'max', lt)) # test old initials cunittest2.assert equals('srm2', nets.newid('sa', 'ra', 'max', lt)) # test new initials cunittest2.assert equals('ll1', nets.newid('la', '', 'lee', lt)) # test an old set of initials, and also a LastUsed not in position 0 cunittest2.assert equals('ll2', nets.newid('laci', '', 'lee', lt)) #test having a middle initial, same first and last initial cunittest2.assert equals('ljl1', nets.newid('li', 'joon', 'lee', lt)) print "All tests for newid passed" 1 Yes, for this exam we re doing the testing after the implementation. Tsk, tsk. Page 5