Answers: Functions#
Provided here are answers to the practice questions at the end of “Functions”.
Function Execution#
Function Execution Q1
square_default = square_all([2,3,4])
power_three = square_all([2,3,4], 3)
out_4 = square_all([2,2,2,2]) # this execution could differ
Function Execution Q2.
tempA = convert_temp(95)
tempB = convert_temp(17, 'C')
Function Execution Q3
joke_random = court_jester_jokes()
joke_nonrandom = court_jester_joke
User-Designed Functions#
UDFs Q1
# there are multiple approaches to string concatenation
def provide_info(name, year, school='UCSD'):
output = "Hi. I'm " + name + '. I am a ' + year + ' at ' + school + '.'
return output
UDFs Q2.
def count_int(col):
count = 0
for val in col:
if type(val) == int:
count += 1
return count
UDFs Q3.
def modify_string(input_string):
new_language = {'a': 'ӓ',
'e' : 'ɚ',
'i' : '¡',
'o' : 'ð',
'u' : 'û',
'A' : 'Ӓ',
'E' : 'ɛ',
'O' : 'Ó',
'U' : 'Ü'}
modified_name = ''
for letter in input_string:
if letter in new_language:
modified_name = modified_name + new_language[letter]
else:
modified_name = modified_name + letter
return modified_name
UDFs Q4.
def calculate_points(hand):
pointers = ['10', 'J', 'Q', 'K', 'A']
counter = 0
for card in hand:
if card in pointers:
counter += 1
return counter
UDFs Q5
# there are multiple approaches that work
def change_username(username):
if len(username) > 4 and username.isalnum() and 'cogs' not in username:
return False
else:
return True
UDFs Q6
default_brackets = {0.10 : (0, 9875),
0.12 : (9876, 40125),
0.22 : (40126, 85525),
0.24 : (85526, 163300),
0.32 : (163301, 207350),
0.35 : (207351, 518400)
}
def to_taxes(salary, tax_brackets=default_brackets):
for key in tax_brackets:
if tax_brackets[key][0] <= salary <= tax_brackets[key][1]:
output = key * salary
return output
UDFs Q7
def exclaim(str_a, str_b):
result = str_a + ' ' + str_b + '!'
return result
exclaim('Happy', 'Friday')
UDFs Q8
def return_bigger(num1, num2):
if num1 > num2:
return num1
elif num2 > num1:
return num2
elif num2 == num1:
return num1
UDFs Q9
def buy_or_save(round_1, round_2):
if round_1 == 'W' and round_2 == 'W':
return 'The team should save!'
elif round_1 == 'L' and round_2 == 'L':
return 'The team should buy!'
else:
return 'The team should half-buy!'
UDFs Q10
def matcha(color, texture):
if color == "vibrant green" and texture == "fine":
return "This is ceremonial-grade matcha!" #wording may vary
elif color == "dull green" and texture == "fine":
return "This is high-grade matcha!" #wording may vary
else:
return "This is low-grade matcha!" #wording may vary
UDFs Q11
def phone_time(screen_time):
if screen_time < 0 or type(screen_time) == str:
return "Invalid Input" #message may vary
elif screen_time < 3:
return "Good job for keeping a low screen time!" #message may vary
elif screen_time < 5:
return "It might be good to take a break." #message may vary
UDFs Q12
def reseller_markup(prices):
if len(prices) == 0:
return “No Labubu resellers”
difference = abs(prices[0] - prices[1])
if prices[0] < prices[1]:
return “The first Labubu reseller is ” + str(difference) + “ cheaper than the second reseller”
elif prices[0] > prices[1]:
return “The first Labubu reseller is ” + str(difference) + “ more expensive than the second reseller”
elif prices[0] == prices[1]:
return “They are the same price”
Debugging#
Debugging Q1.
def count_consonants(input_string):
out = {}
vowels = ['a', 'e', 'i', 'o', 'u']
for char in input_string:
char = char.lower()
if char not in vowels:
if char not in out:
out[char] = 1
else:
out[char] += 1
return out
Debugging Q2.
def new_encrypt(input_string):
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
reverse_alpha = 'ZYXWVUTSRQPONMLKJIHGFEDCBA'
output_string = ''
for char in input_string:
char = char.upper()
if char in alpha:
position = alpha.find(char)
output_string += reverse_alpha[position]
else:
output_string = None
break
return output_string
Debugging Q3
def growth_rate(this_year, last_year):
return ((this_year - last_year)/last_year) * 100