Tracing nested function calls
Turkish names can contain spaces, punctuation, and special characters that need adjustment before the names are used as folder names. The functions in Listing 1 each perform 1 adjustment.
def trim_spaces(text: str) -> str:
"""Remove leading and trailing whitespace."""
result = text.strip()
print(f'"{result}"')
return result
def strip_punct(text: str) -> str:
"""Remove punctuation symbols."""
result = text.replace("!", "")
result = result.replace("?", "")
result = result.replace(".", "")
result = result.replace(",", "")
print(f'"{result}"')
return result
def to_lower(text: str) -> str:
"""Convert the text to lowercase."""
result = text.lower()
print(f'"{result}"')
return result
def to_regular(text: str) -> str:
"""Replace special characters."""
result = text.replace("ç", "c")
result = result.replace("Ç", "C")
result = result.replace("ğ", "g")
print(f'"{result}"')
return result
def add_underscores(text: str) -> str:
"""Replace spaces with underscores."""
result = text.replace(" ", "_")
print(f'"{result}"')
return resultThe functions in Listing 2 use the helpers in one nested expression. The exclamation mark and trailing spaces are part of the input.
"Çiğdem Yildiz! ".
def format_folder_name(text: str) -> str:
"""Prepare a name for folder format."""
return add_underscores(
to_regular(
to_lower(
strip_punct(
trim_spaces(text)
)
)
)
)
def test_format_folder_name() -> None:
name_text = "Çiğdem Yildiz! "
print(f'"{name_text}"')
print(f'"{format_folder_name(name_text)}"')
if __name__ == "__main__":
test_format_folder_name()Complete the following tasks without executing the code.
- Number the 5 helper-function calls in their execution order.
- Write all 7 lines printed by
test_format_folder_name, including the quotation marks and repeated final value. - For each helper call, record its input and returned value.
- Rewrite
format_folder_namewithout nested calls. Store each intermediate value in a clearly named local variable and return the final value. - Explain why the final folder name is printed twice.