Looking for feedback on weighted voting algorithm
Martin A. Brown
martin at linux-ip.net
Thu Apr 14 12:41:16 EDT 2016
Greetings Justin,
> score = sum_of_votes/num_of_votes
>votes = [(72, 4), (96, 3), (48, 2), (53, 1), (26, 4), (31, 3), (68, 2), (91, 1)]
>Specifically, I'm wondering if this is a good algorithm for
>weighted voting. Essentially a vote is weighted by the number of
>votes it counts as. I realize that this is an extremely simple
>algorithm, but I was wondering if anyone had suggestions on how to
>improve it.
I snipped most of your code. I don't see anything wrong with your
overall approach. I will make one suggestion: watch out for
DivisionByZero.
try:
score = sum_of_votes / num_of_votes
except ZeroDivisionError:
score = float('nan')
In your example data, all of the weights were integers, which means
that a simple mean function would work, as well, if you expanded the
votes to an alternate representation:
votes = [72, 72, 72, 72, 96, 96, 96, 48, 48, 53, 26, 26, 26, 26,
31, 31, 31, 68, 68, 91]
But, don't bother!
Your function can handle votes that have a float weight:
>>> weight([(4, 1.3), (1, 1),])
2.695652173913044
Have fun!
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Python-list
mailing list