Dec 2011.pdf

20 Pages
116 Views
Unlock Document

Department
Computer Science
Course
CSC108H1
Professor
Dan Dolderman
Semester
Fall

Description
D N A P L E A S E H A N D I N H UNIVERSITY OF TORONTO Faculty of Arts and Science E S DECEMBER 2011 EXAMINATIONS A CSC 108 H1F E Instructors: Craig, Horton and Zingaro Duration | 3 hours P 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: / 8 # 2: / 4 This ▯nal 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 ▯nal examination is complete. # 5: / 8 Comments and docstrings are not required except where indic# 6:, al/ 4ugh they may help us mark your answers. They may also get you part marks if you can’t ▯gure out how to write the code. # 7: / 6 You do not need to put import statements in your answers. You may not use break or continue on this exam. # 8: / 9 If you use any space for rough work, indicate clearly what # 9:want/ 8rked. Assume all input is valid unless otherwise indicated; there is no need to error-check. # 10: / 7 # 11: / 6 TOTAL: /75 Page 1 of 20 Good Luck! cont’d::: December 2011 Final Examination CSC 108 H1F Question 1. [8 marks] Each subquestion below has a small piece of code that is supposed to print either ’BINGO’ or ’B I N G O’, but has a small part missing. For each one, add the missing part inside the box. Your solution must follow the instructions in the comment statement. Each subquestion is independent. Part (a) [1 mark] letters = [’B’,’I’,’N’,’G’,’O’] # Iterate over the list to print B I N G O on one line Part (b) [1 mark] games = [’Monopoly’, ’Tic Tac Toe’, ’Bridge’] # Replace ’Bridge’ with ’BINGO’ print games.pop() Part (c) [1 mark] games = [[’Bridge’, ’Poker’] , [’BINGO’, ’Euchre’], [’Monopoly’, ’Clue’]] # Print the appropriate element of this data structure Part (d) [1 mark] pets = {’cat’: ’Whiskers’, ’fish’: ’Nemo’, ’dog’: ’BINGO’} # Print the correct dictionary value Page 2 of 20 cont’d::: December 2011 Final Examination CSC 108 H1F Part (e) [1 mark] more_letters = ’XXBIXNGXOXX’ # Iterate over the string, but don’t print any of the X’s Part (f) [1 mark] # some_string is a string variable with a value that you don’t know some_string = raw_input(’enter something’) s = ’axBINxx’ + some_string + ’xGOmn’ # Extract the slices that you need from s and build slice print slice Part (g) [1 mark] letters = [’A’, ’B’, ’G’, ’I’, ’M’, ’Nope’, ’O’] # Using elements (or pieces of elements) from letters, build string result print result Part (h) [1 mark] x = -2 + 4 / 2 + 1 # Make a list with only one element that is ’BINGO’ and other elements with other values # so that the print statement prints the one ’BINGO’ element print my_list[x] Page 3 of 20 Student #: cont’d::: December 2011 Final Examination CSC 108 H1F Question 2. [4 marks] In the box beside each piece of code below, write its output. If it would generate an error, say so, and give the reason for the error. Part (a) [1 mark] L = [3, 2, 1, 0] for item in L: item = item + 1 print L Part (b) [1 mark] L = [10, 11, 12, 13] for item in L: L[item] = L[item] + 1 print L Part (c) [1 mark] L = [5, 6, 7, 8] for item in range(len(L)): L[item] = L[item] + 1 print L Part (d) [1 mark] L = [2, 4, 6, 8] for item in range(len(L)): item = item + 1 print L Page 4 of 20 cont’d::: December 2011 Final Examination CSC 108 H1F Question 3. [8 marks] In the box beside each piece of code below, write its output. If it would generate an error, say so, and give the reason for the error. Part (a) [2 marks] L1 = ["I", "like", "candy"] L2 = L1.append("somewhat") print L1 print L2 Part (b) [2 marks] n1 = 96 n2 = n1 n2 = n1 + 1 print n1 print n2 Part (c) [2 marks] x = [1, 2] L1 = [x, [8, 9]] L2 = L1[:] L2[0][1] = 999 print L1 print L2 Part (d) [2 marks] def f(s): s = s * 2 return s food = "pizza" f(food) print food print f(’cheese’) Page 5 of 20 Student #: cont’d::: December 2011 Final Examination CSC 108 H1F Question 4. [7 marks] This question involves a dictionary of the same format as the actor to movie dictionary that you used in A3. Part (a) [4 marks] Complete the function below according to its docstring. def largest_cast(actor_dict, movie1, movie2): ’’’Summary: Return which of two movies had the largest number of actors. Each key in dictionary actor_dict is the name of an actor (a string) and its value is a list of the movies in which that actor has performed. (Each movie name is a string also.) Return movie1 or movie2, whichever has the larger number of actors. If it is a tie, return either. ’’’ Page 6 of 20 cont’d::: December 2011 Final Examination CSC 108 H1F Part (b) [1 mark] Assume that the number of actors in the dictionary is n, and that each actor on average performs in m movies. How many string comparisons (checking if one string equals another) does your solution perform? Important Note: If you used the boolean expression x in L for some x on some list L, you should count this as performing len(L) comparisons. Part (c) [1 mark] Suppose that n and m are large and that you needed to do this operation (comparing the cast sizes of two movies) many many times. You could design your program to save time. Explain in one or two sentences how you would do this. Part (d) [1 mark] Explain brie y why this e▯ciency improvement only makes sense if you are comparing cast sizes more than once. Page 7 of 20 Student #: cont’d::: December 2011 Final Examination CSC 108 H1F Question 5. [8 marks] The parts of this question are completely independent of each other. You can solve one without solving the other. Part (a) [3 marks] Consider the following function: def min_value (L): ’’’L is a list of ints that are >= -1. Return the minimum value in L that is > -1. If L doesn’t have any value in it other than -1, return -1.’’’ Suppose that we want to test min_value. Describe three test cases that each test di▯erent \categories" of inputs. To describe each test case, give the list that you would pass to min_value, 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 three more. Value of L Return value Purpose of this test case [] -1 empty list Page 8 of 20 cont’d::: December 2011 Final Examination CSC 108 H1F Part (b) [5 marks] Now write the function according to its docstring speci▯cation. def min_value(L): ’’’L is a list of ints that are >= -1. Return the minimum value in L that is > -1. If all the values in L are -1, return -1.’’’ Page 9 of 20 Student #: cont’d::: December 2011 Final Examination CSC 108 H1F Question 6. [4 marks] Part (a) [2 marks] The Toronto Maple Leafs hockey team plays games on many days but not every day. The program below asks the user to say what happened on every single day, whether a game was played or not. The user either answers \a win", \a loss" or \no game". The program keeps asking for the next day’s results until it gets two consecutive days where the Leafs played and had the same result (either two wins on back-to-back days or two losses on back-to-back days). Fill in the missing while loop condition to make the program work as described above. previous = raw_input("First day’s game result? ") current = raw_input("Next day’s game result? ") while : previous = current current = raw_input("Next day’s game result? ") print ’Finally! Two days in a row with the same game result.’ Part (b) [2 marks] Consider the following for loop: for i in range(0,77): print i Re-write it to do the same thing using a while loop. Page 10 of 20 cont’d::: December 2011 Final Examination CSC 108 H1F Question 7. [6 marks] Consider the following function. def average_word_length (text): ’’’text is a str consisting only of words and spaces (no punctuation). text is guaranteed to have at least one word. There can be one or more than one whitespace character between pairs of words. Return the average length of all words in text, as a float.’’’ Part (a) [1 mark] What should the function return if given this string:I am the\n\n walrus"? Part (b) [5 marks] Write function average_word_length. You don’t need to repeat the docstring. Part of your mark will be for avoiding unnecessarily complicated techniques. def average_word_length (text): Page 11 of 20 Student #:
More Less

Related notes for CSC108H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit