You are given:
-
A list of strings
texts
(e.g., user comments), length
n
.
-
A list of floating-point scores
scores
of length
n
, where each score can be any real value, including
-∞
or
+∞
.
Design an algorithm/function that randomly returns one string from texts such that the probability of returning texts[i] is proportional to the score distribution.
Define the sampling rule explicitly as follows:
-
If all scores are finite, sample index
i
with probability
pi=∑j=0n−1escores[j]escores[i](softmax).
-
If one or more scores are
+∞
, return one of the
+∞
items uniformly at random.
-
If all scores are
-∞
(so all weights are zero), return any item uniformly at random.
Requirements:
-
Time complexity:
O(n)
per sample.
-
Must be numerically stable for large/small scores.
-
Clearly state how you handle corner cases (empty input, NaN scores if they appear, etc.).