CSC 108 - W3 Worksheets
CSC 108 - W3 Worksheets
>>> is_right_triangle(3, 4, 5)
True
>>> is_right_triangle(2, 2, 4)
False
"""
if side1 ** 2 + side2 ** 2 == hypotenuse ** 2: a^2 + b^2 = c^2
return True
else:
return False
CSC108H Fall 2022 Worksheet: if statements
>>> ticket_price(7)
4.25
>>> ticket_price(21)
7.5
>>> ticket_price(101)
4.75
"""
if age >= 65:
return 4.75
elif 12 < age < 65:
return 7.50
else:
return 4.25
Each of the following functions is correctly implemented, but is more complex than it needs to be. Each function
body can be replaced with a single return statement. You can use comparison operators <, >, <=, and so on, as
well as boolean operators and, or, and not.
>>> can_vote(16)
False
>>> can_vote(21)
True
"""
>>> is_teenager(4)
False
>>> is_teenager(16)
True
>>> is_teenager(19)
False
"""
For which age range will this function return True? 13-18
Assuming the code above has been executed, complete the indices in the expression below that will pro-
duce the string 'LOL'. Use at least one negative index in your answer.
Assuming the code above has been executed, complete the table with the values that each variable refers
to.
Variable Value
phrase ‘big orange cat’
slice1 ‘big’ up to and not including 3rd index
slice2 ‘ cat’ from 4th character from the end to the end
slice3 ‘ oran’
Assuming the code above has been executed, circle the expression(s) that produce False.
(a) 'easy' in lyrics (b) str(len('mj')) in lyrics len(‘mj’) = 2, 2 is in lyrics
(c) 'cab' in lyrics (d) '' in lyrics would not be True if 2 was left
letters not in correct order as int and not converted to str
4. Consider this code:
s = 'Jacqueline'
You know that the slicing operation s[1:4] will produce the string 'acq'. The slicing operation has
an optional third parameter that determines the stride (or distance between characters) in the slice. For
example, the slicing operation s[::2] will produce the string 'Jculn', which has every other character
in 'Jacqueline', starting from the first character in the string, and up to the end of the string. Use a
negative stride to work backwards through a string.
(a) Write an expression that uses slicing on s to produce the string 'aqeie'.
s[1::2] starting from 1st index (2nd char), including every other letter
(b) Write an expression that uses slicing on s to produce the string 'enileuqcaJ'.
(c) Write an expression that uses slicing on s to produce the string 'eieqa'.
s[::-2] moving backwards, including every other letter
CSC108H Fall 2022 Worksheet: Code reading and docstrings
Complete the examples calls and then write a docstring description for each function. (If you get stuck, read
the flip-side of this page for hints.)
(a) def function_a(s1: str, s2: str) -> bool:
""" Return True if and only if s1 and s2 are the same string.
return s1 in s2 and s2 in s1 if both are found within each other, they are the same
(b) def function_b(s1: str, s2: str) -> bool:
""" Return True if and only if s1 and s2 have the same first
character and s1 and s2 have the same last character.
>>> function_b( ‘Sunflower’ , ‘Sewer’ ) S and r
True
>>> function_b( ‘Alphabet’ , ‘Spaghetti’ )
False
"""
1st AND last char must match
return s1[0] == s2[0] and s1[-1] == s2[-1]
(c) def function_c(s1: str, s2: str) -> bool:
""" Return True if and only if the length of s1 is less than or
equal to the length of s2.
>>> function_c( ‘Seed’ , ‘Apple’ ) len(s1) < len(s2) -> min of s1 and s2 == len(s1)
True
>>> function_c(‘Hippopotamus’, ‘Dragon’ ) s2 shorter than s1 → len(s2) != len(s1)
False
"""
the min lengths of s1 and s2 must be equal to the length of s1 → s1
return min(len(s1), len(s2)) == len(s1) must be the min or s2 and s1 must have same length
(d) def function_d(s1: str, s2: str) -> bool:
""" Return True if and only if the first and last characters in s1 are the
same, the first and last characters in s2 are the same, or both.
>>> function_d( ‘Dunk’ , ‘Dunked’ ) first char of s2 == last char of s2
True
first char of s1 != last char of s1
>>> function_d( ‘Dragon’ , ‘Towel’ ) first char of s2 != last char of s2
False
"""
(i) Match each docstring description to the function it describes. Note that “iff” stands for “if and only if”.
1. function b : Return True if and only if s1 and s2 have the same first character and
s1 and s2 have the same last character.
2. function e : Return True if and only if the number representing the length of s1
occurs in s2.
3. function d : Return True if and only if the first and last characters in s1 are the
same, the first and last characters in s2 are the same, or both.
4. function a : Return True if and only if s1 and s2 are the same string.
5. function c : Return True if and only if the length of s1 is less than or equal to
the length of s2.