CSCA08H3 Midterm: CSCA08H3 Midterm 2012 Fall Solutions

169 views5 pages
16 Oct 2018
School
Course
Professor
CSCA08_Mid_2012F Solution
Question 1 part a)
Solution
7 9
Explanation
Since ints are immutable, when we initialize <y> to <x> plus 5, the value of <y>
is not reliant on that of <x> after that declaration.
We can change <x> afterwards, as we do to 7, and this will have no effect on
<y>. Thus leaving <y> as 4 + 5 9, and the second last line then sets <x> to 7.
Question 1 part b)
Solution
C
S
C
Explanation
This while-loop executes as long as our index variable <i> is less than the length
of the string <s> and the current letter that we are looking at (s[i]) is part of the
alphabet.
CSC are part of the alphabet and within the context of the string <s>, hence why
they are the first three letters printed, but then we hit the hyphen, which fails the latter
and check, and thus the while-loop exits.
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
Question 1 part c)
Solution
return age > 70 or has_vip_card
Explanation
We only return true if the age is greater than 70 or <has_vip_card> is True
Question 1 part d)
Solution
s1[3:5] + s2[0] + s1[3]
Explanation
s1[3:5] = ‘te’
s2[0] = ‘s’
s1[3] = ‘t’
‘te’ + ‘s’ + ‘t’ ‘test’
Question 2 part a)
Solution
def case_inversion (s):
‘’’ (str) -> str
Return a copy of <s> with the cases inverted.
>>> case_inversion (‘Hi’)
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Since ints are immutable, when we initialize to plus 5, the value of is not reliant on that of after that declaration. We can change afterwards, as we do to 7, and this will have no effect on. Thus leaving as 4 + 5 9, and the second last line then sets to 7. This while-loop executes as long as our index variable is less than the length of the string and the current letter that we are looking at (s[i]) is part of the alphabet. We only return true if the age is greater than 70 or is true. Te" + s" + t" test". Return a copy of with the cases inverted. We initialize a return string , this is how we know we return a string.