{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

20101_t2_solution - CSC 108H1 S Question 1 Test 2 Solutions...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC 108H1 S Test 2 — Solutions Winter 2010 Question 1. [3 marks] For each of the following snippets of code, write the output that it would produce, or write “Error” if it would cause an error. Code Output (or “Error”) d1 = { [1, 2]: "a", [3]: "z" } print d1.keys() d2 = { 1: 2, 5: 9, -3: 4 } ans = 1 for k in d2: ans = ans * k print ans d3 = { "hello": "goodbye", "fee": "fi" } print d3["hello"].upper()[5] + "am" Solution: Error (Keys must be immutable and lists are not immutable.) -15 Yam (with a capital “Y”) Student #: Page 1 of 5 cont’d . . .
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CSC 108H1 S Test 2 — Solutions Winter 2010 Question 2. [5 marks] Complete the following function according to its docstring description. For the purpose of this question, a word is defined to be a string that is delineated by white space (blanks, tabs and newline characters). You must not store more than one line of the file at a time. Answers that do will receive a mark of zero . def word_count(filename): ’’’filename is a string that is the name of a file. Open that file, read it, and return the number of words in it.’’’
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern