progress

~/problems / Randomness / Probability and expected value

Surprise scoops: expected flavours tried

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy ~15 min

Each day you buy a "surprise scoop": the stand picks flavour i with probability weights[i] / sum(weights), independently. After days days, how many different flavours do you expect to have tasted?

Write expected_flavours(weights: list[int], days: int) -> Fraction, the exact expectation.

expected_flavours([1, 1], 1)       # Fraction(1, 1)     one scoop, one flavour
expected_flavours([1, 1], 2)       # Fraction(3, 2)     same flavour twice with prob 1/2
expected_flavours([1, 3], 2)       # Fraction(11, 8)
expected_flavours([5, 2, 9], 0)    # Fraction(0, 1)

Up to 30 flavours (weights 1..100) and 500 days, so listing every scoop sequence won't finish.

⭐ Bonus: tracking which set of flavours you've tasted passes for a few flavours; a speed test with 30 flavours and 500 days earns a star with a direct formula.

Show hint

the count is a sum over flavours of "tasted i at least once?", and expectations add even for dependent events. Flavour i is never picked with probability (1 - p_i) ** days.

Topic: Probability and expected value. Linearity of expectation, conditioning, Markov-chain equations; answers as fractions.

0:00
Ctrl ' run · Ctrl ↵ submit
esc