Check your understanding of the previous reading: how values flow between loop functions, why we compose loops, and which compositions make sense.
Given the program below,
messages = ["go", "fly", "a", "kite"]
def join_strings(messages: list[str]) -> str:
result = ""
for message in messages:
result = result + ", " + message
return result
def map_title_case(messages: list[str]) -> list[str]:
result = []
for message in messages:
result.append(message.title())
return result
# ____ <-- What goes here?
print(messages)
Which of the following lines of code should be put in the blank to make the strings Title Cased and joined together with commas?
Select every option that applies, then check.
messages still holds the original lowercase list, so print(messages) shows it unchanged.map_title_case returns goes straight into join_strings, and the joined string is stored back in messages before it is printed.messages parameter, so calling them without an argument raises a TypeError.messages is never updated, so the original list is printed.print(messages) still shows the original list.messages first holds the title-cased list, then the joined string.messages parameters are local to each function; the global messages only changes when you assign a function's result to it.What are advantages and reasons to compose loops?
Select every option that applies, then check.
Given the following code to calculate the average of a list of numbers, identify the correct statement.
def average(numbers: list[int]) -> float:
if not numbers:
return None
for number in numbers:
return sum(numbers) / count(numbers)
Select one answer, then check.
sum and count already loop over the list. The for loop returns during its first pass, so it adds nothing: return sum(numbers) / count(numbers) on its own is enough.sum and count consume a whole list; number is a single element of it.if not numbers: is exactly how to guard against an empty list.sum(numbers) / count(numbers); neither function consumes the other's result.Consider the following functions' names, parameters, and return values.
def convert_grade(grade: int) -> str:
pass
def map_convert_grades(grades: list[int]) -> list[str]:
pass
def remove_failed_grades(text_grades: list[str]) -> list[str]:
pass
def get_passing_text_grades(grades: list[int]) -> list[str]:
pass
Which of the following statements would be good compositions?
Select every option that applies, then check.
map_convert_grades applies the Map pattern: it calls convert_grade on each grade in the list.remove_failed_grades, which consumes and produces a list of strings.map_convert_grades: remove_failed_grades(map_convert_grades(grades)).convert_grade handles a single grade, while the main function works on whole lists, so it reaches convert_grade through map_convert_grades.remove_failed_grades consumes text grades (strings), but convert_grade consumes an integer.map_convert_grades should only convert every grade; removing failed grades is a separate step.get_passing_text_grades is the main function that calls map_convert_grades; calling it back would go round in circles.convert_grade works on one integer grade; it has no use for the main function, which consumes a whole list.