The CTP Book

A book for teaching Computational Thinking and Programming skills to people with a background in the Humanities

View on GitHub

Understanding - Beginner, exercise 2

Text

Please consider the following function:

from collections import deque


def f(s1, s2):
    l = list()
    indexes = deque(range(len(s1)))
    while len(indexes) > 0:
        idx = indexes.popleft()
        if idx < len(s2):
            l.append(s2[idx])
        else:
            l.append(s1[idx])
    return "".join(l)

What is the value returned by executing it as follows: f("big", "brother")

Solution

"bro"

Additional material

The runnable Python file is available online. You can run it executing the command python exercise_2.py in a shell.