PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Similar documents
PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSC148 Fall 2017 Ramp Up Session Reference

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Question 1. Part (a) Part (b) December 2013 Final Examination Marking Scheme CSC 108 H1F. [13 marks] [4 marks] Consider this program:

UNIVERSITY OF TORONTO SCARBOROUGH. Wnter 2016 EXAMINATIONS. CSC A20H Duration 2 hours 45 mins. No Aids Allowed

UNIVERSITY OF TORONTO SCARBOROUGH. December 2017 EXAMINATIONS. CSCA20H3 Duration 3 hours. Examination Aids: Instructor: Bretscher

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

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

CSC 108H1 F 2009 Test 1 Duration 35 minutes Aids allowed: none. Student Number:

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

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

Student Number: Instructor: Brian Harrington

UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2014 EXAMINATIONS. CSC 108 H1S Instructors: Campbell and Papadopoulou.

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

CSC A20H3 S 2011 Test 1 Duration 90 minutes Aids allowed: none. Student Number:

CSC 108H1 F 2010 Test 1 Duration 45 minutes Aids allowed: none. Student Number:

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Short Python function/method descriptions:

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

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

UNIVERSITY OF TORONTO Faculty of Arts and Science DECEMBER 2017 EXAMINATIONS. CSC 108 H1F Instructor(s): Campbell, Fairgrieve, and Smith

Short Python function/method descriptions:

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

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.

Question 1. Part (a) Part (b) Part (c) April 2015 Final Examination Solutions CSC 108 H1S. [16 marks] [3 marks]

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

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

Strings are actually 'objects' Strings

Using Objects and Images in Python

Sequences and iteration in Python

UNIVERSITY OF TORONTO Faculty of Arts and Science

Spring 2017 CS 1110/1111 Exam 1

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

CIS192 Python Programming

Python Programming: Lecture 2 Data Types

>>> print( X ) [0,1,2,3] def mystery(l):??

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Subject: Computer Science

PYTHON MOCK TEST PYTHON MOCK TEST III

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

CS 1301 Exam 3 Fall 2010

Midterm I Practice Problems

CS 1110 Prelim 1 October 15th, 2015

CS150 Sample Final Solution

ICS 31 UC IRVINE. ############## YOUR UCINET #### K E Y #### ############## Second Midterm YOUR NAME YOUR STUDENT ID (8 DIGITS)

CS 1110 Prelim 2 April 26th, 2016

Decorate, sort, undecorate pattern

COLLEGE OF ENGINEERING, NASHIK-4

CPD for GCSE Computing: Practical Sheet 6 February 14

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

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

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

Exercise: The basics - variables and types

Your Name: Your TA's Name: 1 / 10 CS 1301 CS1 with Robots Fall 2008 Exam 3

Scripting Languages. Python basics

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

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

Python Day 3 11/28/16

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

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

Collections. Lists, Tuples, Sets, Dictionaries

Python: Short Overview and Recap

Module 3: Strings and Input/Output

Babu Madhav Institute of Information Technology, UTU 2015

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

CS150 Sample Final. Name: Section: A / B

String Processing CS 1111 Introduction to Programming Fall 2018

Full file at

******************** YOUR UCINET ID TIME MWF AT: 8A P ****** K E Y ****** TA S NAME ******************** Second Midterm

CSc 110, Spring 2017 Lecture 39: searching

Key Differences Between Python and Java

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CS 1110 Prelim 2 November 14th, 2013

CSC 207H Fall L Java Quiz Duration 25 minutes Aids allowed: none

EASY

Script language: Python Data structures

UNIVERSITY OF TORONTO Faculty of Arts and Science

Exercise 1.1 Hello world

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

CS1114 Spring 2015 Test ONE ANSWER KEY. page 1 of 8 pages (counting the cover sheet)

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

Midterm #1 Fall minutes

22C:16 CS:1210 Exam 2

15-110: Principles of Computing Sample Exam #1

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Module 07: Efficiency

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

Transcription:

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2012 EXAMINATIONS CSC 108 H1S Instructors: Campbell Duration 3 hours PLEASE HAND IN Examination Aids: None Student Number: Family Name(s): Given Name(s): Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully. # 1: / 4 # 2: / 4 This final examination paper consists of 11 questions on 20 pages (including this one). When you receive the signal to start, please make sure that your copy of the final examination is complete. Comments and docstrings are not required except where indicated, although they may help us mark your answers. They may also get you part marks if you can t figure out how to write the code. You do not need to put import statements in your answers. You may not use break or continue on this exam. If you use any space for rough work, indicate clearly what you want marked. Assume all input is valid unless otherwise indicated; there is no need to error-check. # 3: / 4 # 4: / 8 # 5: / 8 # 6: / 4 # 7: / 8 # 8: /12 # 9: /12 # 10: / 8 # 11: / 4 TOTAL: /76 Page 1 of 20 Good Luck! cont d...

Question 1. [4 marks] For each block of code in this question, write its output in the box below it. If it would generate an error, say so, and give the reason for the error. Part (a) [1 mark] D = {} D[ apple ] = 4 D[ apple ] = 8 print D Part (b) [1 mark] D = {2 : [ d ], 0 : [ c ]} D[0].append( a ) print D[0] Part (c) [1 mark] D = {4 : 3, 3 : 2, 2 : 1} for x in D.values(): print D[x] Part (d) [1 mark] D1 = { a : 1, b : 2, c : 3} D2 = { b : 2, c : 3, a : 1} print D1 == D2 Page 2 of 20 cont d...

Question 2. [4 marks] For each block of code in this question, write its output in the box below it. If it would generate an error, say so, and give the reason for the error. Part (a) [1 mark] L1 = [ yesterday, today ] L2 = L1.append( tomorrow ) print L2 Part (b) [1 mark] L = [[ on, off ], \ [ go, slow, stop ], \ [ up ]] print L[1][2][3] Part (c) [1 mark] s1 = hello s2 = s1 s2 = s2 + -bye print s1 Part (d) [1 mark] L1 = [ hello ] L2 = L1 L2[0] = bye print L1 Page 3 of 20 Student #: cont d...

Question 3. [4 marks] Part (a) [3 marks] Write a good docstring for this function. def mystery(a, b): i = 0 while b!= "" and i < len(a): if a[i] in b: b = b.replace(a[i], "") i += 1 return b == "" Part (b) [1 mark] Provide sample arguments for a call to function mystery(a, b) and give the value returned. Value of a Value of b Return value Page 4 of 20 cont d...

Question 4. [8 marks] Complete the following functions according to their docstring descriptions. Part (a) [4 marks] Do not use the str method swapcase. def our_swapcase(s): (str) -> str Return a copy of the string s with uppercase letters converted to lowercase, and lowercase letters converted to uppercase. Part (b) [4 marks] def length_dict(word_list): (list of strs) -> dict of {int : list of strs} Return a dictionary in which each key is a word length and each value is a list of the words from word_list with that length. Only the lengths of the words that appear in word_list should be included as keys in the dictionary. Page 5 of 20 Student #: cont d...

Question 5. [8 marks] The media module has a function called media.copy that takes a Picture and returns a new Picture that is a copy of the original. Implement your own version of media.copy without using media.copy. You will be marked for efficiency, as well as for correctness. def our_copy(pic): (Picture) -> Picture Return a new picture that is a copy of pic. Page 6 of 20 cont d...

Question 6. [4 marks] Recall the following function from Exercise 3: def get_letter_counts(letter_dict): (dict of {str : list of strs}) -> dict of {str : int} In letter_dict, each key is a single lowercase letter and each value is a list of lowercase words that start with that letter. Based on letter_dict, return a new dictionary in which each key is a single lowercase letter and each value is the number of lowercase words that start with that letter. Suppose that we want to test get_letter_counts. Describe four test cases that each test different categories of inputs. To describe each test case, give the dict that you would pass to get_letter_counts, the return value you expect, and the purpose of the test case. Do not write any code. We have given you one test case as an example; add four more. Value of letter_dict Return value Purpose of this test case {} {} empty dictionary Page 7 of 20 Student #: cont d...

Question 7. [8 marks] A address file contains one street address per line, such as: 100 Main Street 23 Spring Park Road 2012 Sunny Lane 4 Martin Luther King Drive An address list is a list of lists of strs, where each inner list is a 3-element list of street number, name, and type. For example, the address list for the file above is: [[ 100, Main, Street ], [ 23, Spring Park, Road ], \ [ 2012, Sunny, Lane ], [ 4, Martin Luther King, Drive ]] In the file, a single space separates the number, name and type. The format of each address is: the street number is an integer the street name is one or more words (with words separated by a single space) the street type is one word Complete the following funtion according to its docstring description. def load_addresses(numbers_file): (file) -> list of lists of strs Return an "address list" based on the contents of the given "address file". Page 8 of 20 cont d...

Question 8. [12 marks] In this question, you will implement part of a word search game. A word search board is a rectangular board of lowercase letters. For example: r b t x m k e a t x d s x b a t o p q y v x d l A word search file contains a word search board. For example: rbtxmk eatxds xbatop qyvxdl A word search list (a list of strs) represents a word search board. For example: [ rbtxmk, eatxds, xbatop, qyvxdl ] The object of this game is to identify words (sequences of characters) that appear in the board. A word can appear from top to bottom in a column, for example baby or spl: r b t x m k e a t x d s x b a t o p q y v x d l A word can also appear from left to right in a row, for example eat or vx : r b t x m k e a t x d s x b a t o p q y v x d l In the examples shown above, spl and vxd are considered words on the board, even though they are not actual English words. For the purpose of this program, a word in the word search board is any sequence of 1 or more character(s) that appears in a row (from left to right) or column (from top to bottom) of the word search board, regardless of whether it is an actual English word. We will not consider words that appear on a diagonal. The next two pages contain starter code for a program that continually prompts the user to guess a word, until they guess a word that appears on the word search board. The functions and main on the next two pages are in the same module. Complete the missing code according to the docstring descriptions and inline comments. Avoid duplicate code by calling functions from this module as helpers. You should do this even if the helpers are incomplete/incorrect. Each function will be marked as though any helper it relies on works correctly. Page 9 of 20 Student #: cont d...

def load_board(ws_file): (file) -> list of strs ws_file is a "word search file". Each line of the file contains lowercase letters and all lines have the same length. Return a "word search list" based on the contents of ws_file. def display_board(ws_board): (list of strs) -> NoneType Display the contents of the "word search board" ws_board. for row in ws_board: print row def in_list(l, word): (list of strs, str) -> bool Return True iff word appears in (is a substring of) at least one item in L. for item in L: if word in item: return True return False def in_row(ws_board, word): (list of strs, str) -> bool Return True iff word appears in at least one row of "word search board" ws_board. Page 10 of 20 cont d...

def in_column(ws_board, word): (list of strs, str) -> bool Return True iff word appears in at least one column of "word search board" ws_board. def is_found(ws_board, word): (list of strs, str) -> bool Return True iff word appears in at least one row or column of "word search board" ws_board. if name == " main ": ws_board = load_board("words.txt") display_board(ws_board) # Continually prompt the user to enter a word, until the user enters a # word that appears in the "word search board" ws_board. print "You found a word! The word %s appears in the word search board." % (word) Page 11 of 20 Student #: cont d...

Question 9. [12 marks] This question involves working with dictionaries of the following types: book to people dictionary key: a book (a str), value: a list of people who have read that book (a list of strs) example: { Treasure Island : [ Joanna, Jonathan, Hui, Ali ],\ Wuthering Heights : [ Hui, Monia, Ali ],\ Middlemarch : [ Vlad, Ali ]} person to books dictionary key: a person (a str), value: a list of books that person has read (a list of strs) example: { Vlad : [ Middlemarch ], Monia : [ Wuthering Heights ],\ Ali : [ Treasure Island, Wuthering Heights, Middlemarch ],\ Hui : [ Treasure Island, Wuthering Heights ],\ Joanna : [ Treasure Island ], Jonathan : [ Treasure Island ]} Complete the following functions according to their docstring descriptions. Part (a) [5 marks] def invert_book_to_people(book_to_people): (dict of {str: list of strs}) -> dict of {str: list of strs} book_to_people is a "book to people dictionary". Return a "person to books dictionary" based on book_to_people. Page 12 of 20 cont d...

Part (b) [7 marks] This part of the question involves another type of dictionary that is used to make recommendations. recommendations dictionary for book B key: a book (a str) that people who read book B have also read, value: the number of people (an int) who read both this book (the key) and book B example: for Wuthering Heights { Treasure Island : 2, Middlemarch : 1} def make_recommendations(book_to_people, book): (dict of {str: list of strs}) -> dict of {str: int} book_to_people is a "book to people dictionary" and book is a book title that occurs as a key in book_to_people. Return a new "recommendations dictionary" for book. Page 13 of 20 Student #: cont d...

Question 10. [8 marks] Throughout this question, assume that we are sorting lists into non-descending order. Do not guess. There is a one-mark deduction for incorrect answers. Part (a) [2 marks] We are partly through sorting a list, and have completed 3 passes through the data. The list currently contains [4, 6, 7, 8, 5, 9, 7, 15]. Which sorting technique are we definitely not using? Circle one. bubble sort insertion sort Part (b) [2 marks] Suppose you have a list of 100 distinct numbers in order from largest to smallest, in other words, backwards to the order we want. Which sorting technique would require the most comparisons? Circle one. insertion sort selection sort they would require an equal number Part (c) [2 marks] Suppose you have a list of 100 distinct numbers in order from largest to smallest, in other words, backwards to the order we want. Which sorting technique would require the most swaps? Circle one. insertion sort selection sort they would require an equal number Part (d) [2 marks] Consider the following lists: L1 = [9, 8, 7, 6, 5, 4, 3, 2, 1] and L2 = [8, 1, 3, 6, 9, 7, 4, 2, 5] Which list would cause insertion sort to do more swaps? Circle one. L1 L2 they would require an equal number Page 14 of 20 cont d...

Question 11. [4 marks] Do not guess. There is a half-mark deduction for incorrect answers. Part (a) [1 mark] for i in range(len(l)): L[i] = L[i] ** 2 Let n be the size of the list L. Which of the following most accurately describes how the runtime of this function grow as n grows? Circle one. (a) It grows linearly, like n does. (c) It grows less than linearly. (b) It grows quadratically, like n 2 does. (d) It grows more than quadratically. Part (b) [1 mark] i = 0 while i!= len(l): L[i] = L[i] ** 2 i += 1 Let n be the size of the list L. Which of the following most accurately describes how the runtime of this function grow as n grows? Circle one. (a) It grows linearly, like n does. (c) It grows less than linearly. (b) It grows quadratically, like n 2 does. (d) It grows more than quadratically. Part (c) [1 mark] for x in range(len(l)): for y in range(len(l[0])): if y == y1: L[x][y1] = L[x][y1] ** 2 Let n be the size of the list L. Which of the following most accurately describes how the runtime of this function grow as n grows? Circle one. (a) It grows linearly, like n does. (c) It grows less than linearly. (b) It grows quadratically, like n 2 does. (d) It grows more than quadratically. Part (d) [1 mark] for x in range(len(l)): L[x][y1] = L[x][y1] ** 2 Let n be the size of the list L. Which of the following most accurately describes how the runtime of this function grow as n grows? Circle one. (a) It grows linearly, like n does. (c) It grows less than linearly. (b) It grows quadratically, like n 2 does. (d) It grows more than quadratically. Page 15 of 20 Student #: cont d...

Total Marks = 76 Page 16 of 20 cont d...

[Use the space below for rough work. This page will not be marked, unless you clearly indicate the part of your work that you want us to mark.] Page 17 of 20 Student #: cont d...

Short Python function/method descriptions: builtins : len(x) -> int Return the length of the list, tuple, dict, or string x. max(l) -> object Return the largest value in L. min(l) -> object Return the smallest value in L. open(name[, mode]) -> file Open a file. Legal modes are "r" (read), "w" (write), and "a" (append). range([start], stop, [step]) -> list of ints Return a list containing the integers starting with start and ending with stop - 1 with step specifying the amount to increment (or decrement). If start is not specified, the list starts at 0. If step is not specified, the values are incremented by 1. raw_input([prompt]) -> str Read a string from standard input. The trailing newline is stripped. dict: D[k] --> object Return the value associated with the key k in D. k in d --> bool Return True if k is a key in D and False otherwise. D.get(k) -> object Return D[k] if k in D, otherwise return None. D.keys() -> list of objects Return the keys of D. D.values() -> list of objects Return the values associated with the keys of D. D.items() -> list of (key, value) pairs Return the (key, value) pairs of D, as 2-tuples. file (also called a "reader"): F.close() --> NoneType Close the file. F.read([size]) -> read at most size bytes, returned as a str If the size argument is negative or omitted, read until EOF (End of File) is reached. F.readline([size]) -> next line from the file, as a str. Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line may be returned then). Return an empty string at EOF. float: float(x) -> float Convert a string or number to a floating point number, if possible. int: int(x) -> int Convert a string or number to an integer, if possible. A floating point argument will be truncated towards zero. Page 18 of 20 cont d...

list: x in L --> bool Return True if x is in L and False otherwise. L.append(x) --> NoneType Append x to the end of the list L. L.index(value) -> int Return the lowest index of value in L. L.insert(index, x) --> NoneType Insert x at position index. L.pop() --> object Remove and return the last item from L. L.remove(value) --> NoneType Remove the first occurrence of value from L. L.reverse() --> NoneType Reverse the elements of L. L.sort() --> NoneType Sort the list L in ascending order. str: x in s --> bool Return True if x is in s and False otherwise. str(x) -> str Convert an object into its string representation, if possible. S.count(sub[, start[, end]]) -> int Return the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation. S.find(sub[,i]) -> int Return the lowest index in S (starting at S[i], if i is given) where the string sub is found or -1 if sub does not occur in S. S.index(sub) -> int Like find but raises an exception if sub does not occur in S. S.isdigit() -> bool Return True if all characters in S are digits and False otherwise. S.lower() -> str Return a copy of the string S converted to lowercase. S.lstrip([chars]) -> str Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. S.replace(old, new) -> str Return a copy of string S with all occurrences of the string old replaced with the string new. S.rstrip([chars]) -> str Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. S.split([sep]) -> list of strs Return a list of the words in S, using string sep as the separator and any whitespace string if sep is not specified. S.strip() -> str Return a copy of S with leading and trailing whitespace removed. S.upper() -> str Return a copy of the string S converted to uppercase. Page 19 of 20 Student #: cont d...

media: copy(picture) -> Picture Return a copy of the Picture. create_picture(int, int) --> Picture Given a width and a height, return a Picture with that width and height. All pixels are white. get_blue(pixel) --> int Return the blue value of the given Pixel. get_color(pixel) --> Color Return the Color object with the given Pixel s RGB values. get_green(pixel) --> int Return the green value of the given Pixel. get_pixel(picture, int, int) --> Pixel Given x and y coordinates, return the Pixel at (x, y) in the given Picture. get_red(pixel) --> int Return the red value of the given Pixel. get_x(pixel) --> int Return the x coordinate of the given Pixel. get_y(pixel) --> int Return the y coordinate of the given Pixel. set_blue(pixel, int) --> NoneType Set the blue value of the given Pixel to the given int value. set_color(pixel, Color) --> NoneType Set the RGB values of the given Pixel to those of the given Color. set_green(pixel, int) --> NoneType Set the green value of the given Pixel to the given int value. set_red(pixel, int) --> NoneType Set the red value of the given Pixel to the given int value. Page 20 of 20 End of Final Examination