0% found this document useful (0 votes)
62 views6 pages

CSC 108 - W3 Worksheets

Here are the matches between the docstrings and functions: (a) def function_a(s1: str, s2: str) -> bool: """Return True iff s1 and s2 are the same string.""" (b) def function_b(s1: str, s2: str) -> bool: """Return True iff s1 and s2 have the same first character and s1 and s2 have the same last character.""" (c) def function_c(s1: str, s2: str) -> bool: """Return True iff the length of s1 is less than or equal to the length of s2.""" (d) def

Uploaded by

rebeccaw.0927
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views6 pages

CSC 108 - W3 Worksheets

Here are the matches between the docstrings and functions: (a) def function_a(s1: str, s2: str) -> bool: """Return True iff s1 and s2 are the same string.""" (b) def function_b(s1: str, s2: str) -> bool: """Return True iff s1 and s2 have the same first character and s1 and s2 have the same last character.""" (c) def function_c(s1: str, s2: str) -> bool: """Return True iff the length of s1 is less than or equal to the length of s2.""" (d) def

Uploaded by

rebeccaw.0927
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSC108H Fall 2022 Worksheet: Functions and Booleans

Complete the following functions.

1. def same_abs(num1: float, num2: float) -> bool:


"""Return True if and only if num1 and num2 have the same absolute value.

>>> same_abs(-3.2, 3.2)


True
>>> same_abs(3.0, 3.5)
False
"""
if abs(num1) == abs(num2):
return ‘True’
else:
return ‘False’

2. def different_types(obj1: object, obj2: object) -> bool:


"""Return True if and only if obj1 and obj2 are of different types.

>>> different_types(3, '3')


True
>>> different_types(108.0, 3.14)
False
"""
if type(obj1) != type(obj2) != checks for if values
return True are NOT equal
else:
return False

3. An extra exercise to try at home.

def is_right_triangle(side1: int, side2: int, hypotenuse: int) -> bool:


"""Return whether a triangle with sides of length side1, side2 and
hypotenuse is a right triangle.

>>> 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

Complete the following functions.

1. def earlier_name(name1: str, name2: str) -> str:


"""Return the name, name1 or name2, that comes first alphabetically.

>>> earlier_name('Jen', 'Paul')


'Jen'
>>> earlier_name('Colin', 'Colin')
'Colin'
"""
if name1 < name2:
return name1
else:
return name2

2. def ticket_price(age: int) -> float:


"""Return the ticket price for a person who is age years old.
Seniors 65 and over pay 4.75, kids 12 and under pay 4.25 and
everyone else pays 7.50.

Precondition: age > 0

>>> 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

3. def format_name(first: str, last: str) -> str:


"""Return the first and last names as a single string, in the form:
last, first
Mononymous persons (those with no last name) should have their name
returned without a comma.

>>> format_name('Cherilyn', 'Sarkisian')


'Sarkisian, Cherilyn'
>>> format_name('Cher', '')
'Cher'
"""
if last == ‘’:
return first
else:
return last + ‘, ‘ + first
CSC108H Fall 2022 Worksheet: No if required

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.

1. def can_vote(age: int) -> bool:


"""Return True if and only if age is legal voting age of at least 18 years.

>>> can_vote(16)
False
>>> can_vote(21)
True
"""

if age < 18:


return False
else:
return True

Complete the new single-line function body in the box below:

return age > 18 will check if age > 18 is True or False

2. def is_teenager(age: int) -> bool:


"""Return True if and only if age is a teenager between 13 and 18 inclusive.

>>> is_teenager(4)
False
>>> is_teenager(16)
True
>>> is_teenager(19)
False
"""

if age < 13:


return False
else:
if age > 18:
return False
else:
return True

For which age range will this function return True? 13-18

Complete the new single-line function body in the box below:

return 13 <= age <= 18


CSC108H Fall 2022 Worksheet: String Operations

1. Consider this code:

phrase = 'Laughing Out Loud'

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.

phrase[ 0 ] + phrase[ 9 ] + phrase[ -4 ]

2. Consider this code:


0 1 2 3 4 5 6 7 8 9 10 12
phrase = 'big orange cat'
11 13
slice1 = phrase[:3]
slice2 = phrase[-4:]
slice3 = phrase[3:8]

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’

3. Consider this code:

lyrics = 'abc easy as 123'

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'.

s[::-1] moving backwards, including all letters

(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.

>>> function_a( ‘Dog’ , ‘Dog’ )


True
>>> function_a( ‘Dog’ , ‘Cat’ )
False
"""

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
"""

return s1[0] == s1[-1] or s2[0] == s2[-1]


(e) def function_e(s1: str, s2: str) -> bool:
""" Return True if and only if the number representing the length of s1 occurs in s2.

>>> function_e( ‘Abc’ , ‘Easy as 123’ ) len(s1) = 3, ‘3’ found in s2


True
>>> function_e( ‘Colour’ , ‘Bathtub’ ) len(s1) = 6, ‘6’ not found in s2
False
"""

return str(len(s1)) in s2 len(s1) is an int → convert to str → if


number str in s2 then return True
CSC108H Fall 2022 Worksheet: Code reading and docstrings

(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.

(ii) Which function(s) also need a precondition? Add the precondition(s).

You might also like