Skip to content

Question 28

Given the following function that takes in two string inputs:

def comb(s, t):
    out = []
    for c in s:
        if c == ' ':
            break
        for d in t:
            if d in s:
                continue
            out.append(c + d)
    return out

What will the function call comb('bake cake', 'no lie') do?

Solution
c c == ' '? d d in s? out (init. val.: [])
'b' False 'n' 'n' in 'bake cake' (False) ['bn']
'o' 'o' in 'bake cake' (False) ['bn', 'bo']
' ' ' ' in 'bake cake' (True)
'l' 'l' in 'bake cake' (False) ['bn', 'bo', 'bl']
'i' 'i' in 'bake cake' (False) ['bn', 'bo', 'bl', 'bi']
'e' 'e' in 'bake cake' (True)

From here, we can already gather that each iteration from the outer for loop for c in s will pair the current value of c with 'n', 'o', 'l', and 'i'. Hence, it's a matter of matching each letter in 'bake cake' up until if c == ' ' is True. When this if statement's condition is met, the process breaks from the loop - this means that we do not end up doing this pairing process with 'cake' that is after a space ' ' character.

out = []
=> ['bn', 'bo', 'bl', 'bi']
=> ['bn', 'bo', 'bl', 'bi', 'an', 'ao', 'al', 'ai']
=> ['bn', 'bo', 'bl', 'bi', 'an', 'ao', 'al', 'ai', 'kn', 'ko', 'kl', 'ki']
=> ['bn', 'bo', 'bl', 'bi', 'an', 'ao', 'al', 'ai', 'kn', 'ko', 'kl', 'ki', 'en', 'eo', 'el', 'ei']
Answer

Combine each letter from 'bake' with each letter from 'noli'