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.

Listing 1: Python helper functions that prepare text for use as a folder name.
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 result

The functions in Listing 2 use the helpers in one nested expression. The exclamation mark and trailing spaces are part of the input.

Listing 2: Python code that applies the nested calls to "Ç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.

  1. Number the 5 helper-function calls in their execution order.
  2. Write all 7 lines printed by test_format_folder_name, including the quotation marks and repeated final value.
  3. For each helper call, record its input and returned value.
  4. Rewrite format_folder_name without nested calls. Store each intermediate value in a clearly named local variable and return the final value.
  5. Explain why the final folder name is printed twice.