University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

Similar documents
University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

Practice midterm exam

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

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

CSE 160 Winter 2017: Final Exam

CSE 160 Winter 2016: Final Exam

CSE 160 Winter 2016: Final Exam

CSE 160 Winter 2017: Final Exam

CSE 140 Winter 2014: Midterm Exam

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

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

CSE 160 Spring 2018: Final Exam

Midterm Exam 2B Answer key

MUTABLE LISTS AND DICTIONARIES 4

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

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

CS 1301 Exam 2 Fall 2010

Collections. Lists, Tuples, Sets, Dictionaries

CS Exam 2 Name: Your Grading TA: This exam has 7 pages including the title page. Please check to make sure all pages are included.

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

CS 2316 Exam 4 Fall 2011

EXAMINATION INSTRUCTIONS

Basic Python Revision Notes With help from Nitish Mittal

CS150 - Sample Final

61A Lecture 2. Friday, August 28, 2015

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

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Spring Semester 10 Exam #1 Dr. Dillon. (02/18)

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

UNIVERSITY OF TORONTO SCARBOROUGH. Fall 2015 EXAMINATIONS. CSC A20H Duration 3 hours. No Aids Allowed

CS 2316 Exam 4 Fall 2014

Exceptions & a Taste of Declarative Programming in SQL

CS 2316 Exam 1 Spring 2014

CSE wi Final Exam 3/12/18. Name UW ID#

CSE wi Midterm Exam 2/8/18. Name UW ID #

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

CS 2316 Exam 4 Fall 2011

THE AUSTRALIAN NATIONAL UNIVERSITY Final Examination November COMP1730 / COMP6730 Programming for Scientists

CS 1301 Exam 3 Spring 2014

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

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

Data Structures. Lists, Tuples, Sets, Dictionaries

First name (printed): a. DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Fundamentals of Programming (Python) Getting Started with Programming

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Compound Data Types 1

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

APT Session 2: Python

First cut, scope. Defining scope. Find the namespace. Passing argument to parameter. A function s namespace 6/13/2017. chapter 8.

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

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

Spring Semester 11 Exam #1 Dr. Dillon. (02/15)

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

Lecture 9: July 14, How to Think About Debugging

Lecture no

CPSC 217 Midterm (Python 3 version)

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

Sets and Dictionaries

CSCE 110: Programming I

EXAMINATION INSTRUCTIONS

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

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

MEIN 50010: Python Flow Control

Final Exam Version A

Exam #3, Form 3 A CSE 231 Fall 2015 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Sequences and iteration in Python

Programming for Engineers in Python

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

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

CSE413 Midterm. Question Max Points Total 100

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

Question 1. CSC 120H1 F Midterm Test Solutions Fall 2017

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

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Computer Science 217

Getting Started with Python

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

EXAMINATION INSTRUCTIONS

: Intro Programming for Scientists and Engineers Final Exam

2. Explain the difference between read(), readline(), and readlines(). Give an example of when you might use each.

CS110 Introduction to Computing Fall 2006 Midterm Exam

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Midterm Exam

Introduction to Python

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

CSE351 Winter 2016, Final Examination March 16, 2016

Compound Data Types 2

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE344 Midterm Exam Winter 2017

CSE 333 Midterm Exam July 24, Name UW ID#

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

CS Programming Languages: Python

EECS 270 Midterm Exam

CS 1110 Prelim 1 October 17th, 2013

[301] Objects/References. Tyler Caraza-Harter

CSE 333 Midterm Exam 5/10/13

Transcription:

University of Washington CSE 140 Introduction to Data Programming Winter 2013 Midterm exam February 6, 2013 Name: Solutions UW Net ID (username): This exam is closed book, closed notes. You have 50 minutes to complete it. It contains 18 questions and 14 pages (including this one), totaling 90 points. Before you start, please check your copy to make sure it is complete. Turn in all pages, together, when you are finished. Write your initials on the top of ALL pages (in case a page gets separated during test-taking or grading). Please write neatly; we cannot give credit for what we cannot read. Good luck! Page Max Score 2 6 3 6 4 9 5 6 6 6 7 10 8 5 9 5 10 3 11 12 12 8 13 6 14 6 Total 90

Initials: Solutions 1 SHORT ANSWER 1 Short answer 1. An immutable data structure is one that cannot change after being created. Give three reasons to use immutable data. Give reasons that are as different from one another as possible. Use no more than one sentence each. (a) Immutable objects can be put in a set or used as a key to a dictionary. (b) Immutable objects protect your data structures from being corrupted by a client of your code or by your own code. (c) Immutable objects are easier to reason about, since you don t have to keep track of what their current value is. (d) Use of an immutable object is documentation: it is a signal to other programmers that the object will not change. Here are some common incorrect answers: Use an immutable object to prevent change. (That is repeating the definition, not giving a reason.) Efficiency. (This could be an acceptable answer with more explanation, but we haven t dwelled on this in CSE 140.) Repeated use of the same value. (This is possible with a mutable structure: just don t change it.) More convenient to use. (In what way, or for what purpose?) 2

Initials: Solutions 2 EXECUTE PYTHON EXPRESSIONS 2 Execute Python expressions Execute each of the following expressions. If it executes without an error, then: value state the value that it evaluates to If it suffers an error during evaluation: error describe the error (in one phrase a brief explanation in your own words) frame state the name of the current environment frame: global or a function name operator state the operator or operation that caused the error, such as + or function invocation arguments state the values to which the operator was being applied Your answer will contain either part value, or parts error, frame, operator, and arguments. Hint: none of these is a syntax error. 2. len(set(1, 2, 3)) value error wrong number of arguments frame global operator function invocation arguments set function and 1 and 2 and 3 3. len(set([1, 2, 3])) value 3 error frame operator arguments 3

Initials: Solutions 2 EXECUTE PYTHON EXPRESSIONS 4. len(set([(1, 2), (3, 4)])) value 2 error frame operator arguments 5. len(set(([1, 2], [3, 4]))) value error placing mutable value in a set frame set operator set constructor arguments list 1 2 and list 3 4 6. len(set(((1, 2), (3, 4)))) value 2 error frame operator arguments 4

Initials: Solutions 2 EXECUTE PYTHON EXPRESSIONS The following questions assume these definitions: def double(x): return x + x def at42(f): return f(42) # The name "at42" is short for "apply_to_42" 7. at42(double) value 84 error frame operator arguments 8. at42(double(42)) value error invoking a non-function or int is not callable. frame at42 operator function invocation arguments 84 and 42 It is not correct to say 84 does not exist (it certainly does) nor that at42 expects a function (Python does no checking of argument types at function calls: the argument gets passed in, and the error happens within at42). 5

Initials: Solutions 2 EXECUTE PYTHON EXPRESSIONS 9. double(at42(42)) value error invoking a non-function or int is not callable frame at42 operator function invocation arguments 42 and 42 10. double(double)(42) value error cannot add two functions frame double operator + arguments function double and function double 6

Initials: Solutions 3 DRAW THE ENVIRONMENT 3 Draw the environment 11. Draw the entire environment, including all active environment frames and all user-defined variables, at the moment that the division operation is performed. def sum(input): result = 0 for i in input: result = result + i return result def length(input): return len(input) def mean(input): return float(sum(input)) / length(input) input = [5, 2, 2, 3] print sum(input), length(input), mean(input) Global variables: * sum ----> <function sum(input)> * length -> <function length(input)> * mean ---> <function mean(input)> * input --> [5, 2, 2, 3] ^ mean: * input ----* 7

Initials: Solutions 4 DEBUGGING 4 Debugging You have written the following program (in file printdicts.py), and you have successfully loaded it into the Python interpreter (say, by pressing F5 within IDLE while viewing printdicts.py, or alternately by typing from printdicts import *). print_many_dicts(dicts): """Print each dictionary in the input, which is a list of dictionaries.""" for dict in dicts: print_dict(dict) def print_dict(dict): """Print the given dictionary.""" print { for k in dict: print + str(k) + : + str(dict[k]) +, print } Here is an example of using it in the interpreter: >>> print_many_dicts( [ {1:2, 3:4}, {"abra":"cadabra", "hello":"world"} ] ) { 1: 2, 3: 4, } { hello: world, abra: cadabra, } 12. You type an expression to the interpreter, and you get the following output: >>>... you typed your expression here... { Traceback (most recent call last): File "<stdin>", line 1, in <module> File "printdicts.py", line 4, in print_many_dicts print_dict(dict) File "printdicts.py", line 10, in print_dict print + str(k) + : + str(dict[k]) +, TypeError: list indices must be integers, not str Write the smallest expression that you can that produces this exact output. print many dicts([["a"]]) 8

Initials: Solutions 4 DEBUGGING 13. After typing a different expression to the interpreter, you get the following output. The only difference from the previous problem is the last line of output. >>>... you typed your expression here... { Traceback (most recent call last): File "<stdin>", line 1, in <module> File "printdicts.py", line 4, in print_many_dicts print_dict(dict) File "printdicts.py", line 10, in print_dict print + str(k) + : + str(dict[k]) +, IndexError: list index out of range Write the smallest expression that you can that produces this exact output. print many dicts([[3]]) 9

Initials: Solutions 4 DEBUGGING 14. In no more than one sentence, explain the following Python error and how it could arise: TypeError: unhashable type: list A list is mutable, but you have tried to use it in a context where only immutable objects are permitted, such as in a set or as a dictionary key. It is wrong to state that you are trying to convert a list into a set (that s not a problem: for example, set([1,2])). It is wrong to state that you are trying to modify a list or anything else. 10

Initials: Solutions 5 WRITE PYTHON CODE 5 Write Python code 15. Write the body of the max even function. def max_even(lst): """Return the largest number in the input that is an even number. The input must be a list of integers. If there is no even number in the input, return None. Examples: max_even([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9]) => 6 max_even([3, -1001]) => None """ result = None for i in lst: i%2 == 0: if result == None or i > result: result = i return result Another solution: evens = [] for value in lst: if value % 2 == 0: evens.append(value) if len(evens) == 0: return None else: return max(evens) Yet another solution: evens = [] for value in lst: if value % 2 == 0: evens.append(value) max = None for value in evens: if max == None or value > max: max = value return max 11

Initials: Solutions 5 WRITE PYTHON CODE 16. Write the body of a function that prints each word in a given file, on its own line. For example, if you were to execute print words("gettysburg.txt") where file gettysburg.txt is as follows: Four score and seven years ago, our then the output would be Four score and seven years ago, our We have given you the first line of the solution. def print_words(filename): """Print each word of the given file, each on its own line.""" file = open(filename, "r") for line in file: for word in line.split(): print word Another solution: for word in file.read().split(): print word 12

Initials: Solutions 5 WRITE PYTHON CODE In the next two problems, suppose that you are given the following data structure. home_runs = [ ("Babe Ruth", 47, 1926), ("David Ortiz", 54, 2006), ("Albert Pujols", 47, 2009), ("Alex Rodriguez", 54, 2007), ("Babe Ruth", 49, 1930), ("Alex Rodriguez", 47, 2003), ("Babe Ruth", 54, 1920), ("Sammy Sosa", 49, 2002) ] 17. Write code that will print this list, sorted alphabetically by first name and (when the name is the same) by date. In particular, your code should print this data structure (though the formatting, such as line breaks, might be different): [( Albert Pujols, 47, 2009), ( Alex Rodriguez, 47, 2003), ( Alex Rodriguez, 54, 2007), ( Babe Ruth, 54, 1920), ( Babe Ruth, 47, 1926), ( Babe Ruth, 49, 1930), ( David Ortiz, 54, 2006), ( Sammy Sosa, 49, 2002)] Don t define a function just write statements that achieve the goal. Don t do anything special for formatting just use Python s print statement, applying it to a list of tuples. import operator home runs.sort(key=operator.itemgetter(0, 2)) print home runs 13

Initials: Solutions 5 WRITE PYTHON CODE 18. Write code that will print this list, sorted in decreasing order by number of home runs and (when the number of home runs is the same) by increasing date. In particular, your code should print this data structure (though the formatting, such as line breaks, might be different): [( Babe Ruth, 54, 1920), ( David Ortiz, 54, 2006), ( Alex Rodriguez, 54, 2007), ( Babe Ruth, 49, 1930), ( Sammy Sosa, 49, 2002), ( Babe Ruth, 47, 1926), ( Alex Rodriguez, 47, 2003), ( Albert Pujols, 47, 2009)] Don t define a function just write statements that achieve the goal. Don t do anything special for formatting just use Python s print statement, applying it to a list of tuples. import operator home runs.sort(key=operator.itemgetter(2)) home runs.sort(key=operator.itemgetter(1), reverse=true) print home runs 14