← Quiz: Production and Cost: all 10 questions
Quiz: Production and Cost · Question 8 of 10
What does this print? (code)
What does this print?
class TokenBucket:
def __init__(self, rate, capacity):
self.rate, self.capacity = rate, capacity
self.tokens, self.updated = float(capacity), 0.0
def allow(self, now):
self.tokens = min(self.capacity, self.tokens + (now - self.updated) * self.rate)
self.updated = now
if self.tokens >= 1:
self.tokens -= 1
return True
return False
bucket = TokenBucket(rate=1, capacity=2)
print([bucket.allow(t) for t in (0, 0, 0, 1)])
- A.[True, True, True, True]
- B.[True, False, False, True]
- C.[True, True, False, True]
- D.[True, True, False, False]
Answer & explanation
Correct answer: [True, True, False, True]
A burst of 2 is allowed, the third request waits, and one second later a new token is available.
Can you get the rest right without peeking?
Take the timed Quiz: Production and Cost quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →