Question 28
Given the following function that takes in two string inputs:
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.
Answer
Combine each letter from 'bake' with each letter from 'noli'