Assigning a score to variable number of items

1k Views Asked by At

Forgive my lack of formal match knowledge - I'm a programmer and think there's probably an existing formula for my current problem, but am having trouble finding it or coming up with one on my own.

I have an array of numbers that always start at 0 and end at n. I'm trying to figure out how to assign a score to each item from the array, with the caveat that the max score has to be x, and that max score is assigned to the highest number in the input array.

For example,

If I have an array [0, 1, 2, 3, 4, 5], and a max score threshold of 10, then the highest number in the array (5 in this case) would assigned a score of 10, and everything else would be assigned some step amount lower than that.

Another example,

If I have 1 item in the array, that item would receive a score of 10.

If I have 2 items in the array, then the last item would receive a score of 10, and the next lowest one would receive a score of (insert formula here to calculate score). This should be able to scale out to support about 10 items in the array, while staying above 0.

I suppose the challenge will be deciding how much weight each item in the array gets assigned, based on how many items there are in the array. This is the part I am struggling with.

1

There are 1 best solutions below

1
On BEST ANSWER

If you just want scores that are positive, then you can sort the list in reverse order, give the max score to the first one, then have each score be max_score/count less than the previous. So the score of each one will be (max value)(1-percentage of items that are larger than it), or equivalently (max value)(proportion of items less than or equal to it). In other words, if 30% of items are larger than x, then x will get a score of 70% of the max value. In Python, (max value)(1-percentage of items that are larger than it) would be the following:

array2 = array1.copy()
array2.sort(reverse=True)
count = len(array2)
scores={array2[index]:max_score*(1-index/count) for index in range(count)}

Or you can give each one a score based on how far it is between the largest and smallest value. So if the distance from the smallest to x is 70% of the distance from the smallest to the largest, then x gets a score equal to 70% of the max score (with some fudging to make the smallest element get a positive score).

smallest = min(array1)-1 #without the -1, the smallest item would get a score of zero
largest = max(array1)
range = largest-smallest
scores = {item:max_score*(item-smallest)/range for item in array1}