Weighted Keys
Start Timer
0:00:00
Given a dictionary with weights, write a function random_key
that returns a key at random with a probability proportional to the weights.
Example 1:
Input:
weights = {'A': 1, 'B': 2}
Output:
random_key(weights) -> return A 1/3 of the time, B 2/3 of the time
Example 2:
Input:
weights = {'A': 1, 'B': 1}
Output:
random_key(weights) -> return A 1/2 of the time, B 1/2 of the time
Recommended questions for you
Personalized based on your user activity, skill level, and preferences.
.
.
.
.
.
.
.
.
.
Comments