Composition & Closures

Preview — 3 of 10 questions

What is the output of the following code?

javascript
from functools import reduce

pairs = [("a", 1), ("b", 2), ("a", 3)]
result = reduce(
    lambda acc, pair: {**acc, pair[0]: acc.get(pair[0], 0) + pair[1]},
    pairs,
    {}
)
print(result)
A{'a': 4, 'b': 2}
B{'a': 1, 'b': 2, 'a': 3}
CTypeError: unhashable type: 'dict'
D{'a': 3, 'b': 2}

What is the output of the following code?

javascript
from itertools import accumulate
import operator

nums = [1, 2, 3, 4]

print(list(accumulate(nums)))
print(list(accumulate(nums, operator.mul)))
A[10] then [24]
B[1, 3, 6, 10] then [1, 2, 6, 24]
C[1, 2, 3, 4] then [1, 2, 6, 24]
D[1, 3, 6, 10] then [1, 2, 3, 4]

What is the output of the following code?

javascript
from itertools import starmap

pairs = [(2, 3), (4, 5), (6, 2)]
result = list(starmap(pow, pairs))
print(result)
A[(2, 3), (4, 5), (6, 2)]
BTypeError: pow() missing required argument
C[5, 9, 8]
D[8, 1024, 36]

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.