saintly has asked for the wisdom of the Perl Monks concerning the following question:
| Sorting Method | Advantages | Problems |
|---|---|---|
| Method1: Average |
Very simple. Requires no processing if this is all the data you have. Minor variations:
|
|
| Method2: Median |
|
|
| Method3: Use average, but eliminate entries with few votes. |
|
|
| Method4: 'Magic' Score |
|
|
Feel free to also criticize my code. Statistics::Basic implements StdDev, Mean & Median for you, and I expect to use it in the final scoring system(s). I super-searched for titles with 'vote'/'voting' and 'score'/'scoring', but found no hits. If this has already been discussed, I'd appreciate knowing what search terms you used to find the old discussion.#!/usr/bin/perl use strict; use List::Util qw/sum/; my @votes = ( { item => 'worstest', votes => [ 1000, 0, 0, 0, 0 ] }, { item => 'promising', votes => [ 1, 0, 0, 0, 9 ] }, { item => 'bombing', votes => [ 0, 10, 0, 0, 0 ] }, { item => 'lookinGood', votes => [ 0, 0, 0, 0, 3 ] }, { item => 'bad', votes => [ 50, 50, 0, 0, 0 ] }, { item => 'good', votes => [ 0, 0, 0, 50, 50 ] }, { item => 'iffy', votes => [ 3, 0, 0, 0, 0 ] }, { item => 'bestest', votes => [ 0, 0, 0, 0, 1000 ] }, ); # Ideal sort: # bestest, good, promising, lookinGood, bombing, bad, iffy, worstest # or # bestest, good, promising, lookinGood, bombing, iffy, bad, worstest # These global variables may be useful to your sorting function. our $TOTAL_VOTES_CAST = sum( map { sum( @{$_->{'votes'}} ) } @votes ); our $TOTAL_ITEMS = @votes; print "Best to worst: "; foreach my $vote ( sort sortingMethod @votes ) { print $vote->{'item'}, " : ", &score( @{$vote->{'votes'}} ), "\n"; } print &validateSortingMethod(@votes) ? "Awesome Power!\n" : "Teh Suck! +!\n"; exit; # Gets A & B and uses the scoring system to rank them sub sortingMethod { my $score_a = &score( @{ $a->{'votes'} } ); my $score_b = &score( @{ $b->{'votes'} } ); return $score_b <=> $score_a; # Sort highest-to-lowest } # Expects a list like: # $list[0] == number of '0' votes # $list[1] == number of '1' votes, etc... sub score { my( @list ) = @_; my @votes = &expandList( @list ); # Use 'magic values' scoring method @votes = map { [-4,-1,0,1,4]->[$_] } @votes; # This function doesn't use all these, but you might... my $average = &average( \@votes ); my $median = &median( \@votes ); my $stdDev = &stdDev( \@votes ); my $votesCast = @votes; return $average * $votesCast; } sub average { my $l_ref = shift; return (sum( @$l_ref ) / scalar( @$l_ +ref )); } sub median { my $list_ref = shift; my $voteCount = @$list_ref; if( $voteCount % 2 ) { return $list_ref->[ ($voteCount+1) / 2 ]; # Odd # of votes, pick m +iddle } else { return sum ($list_ref->[($voteCount/2)], $list_ref->[($voteCount/2 +)+1]) / 2; } } sub stdDev { my ($list_ref) = @_; my $squares = 0; foreach my $element ( @$list_ref ) { $squares += $element ** 2; } return sqrt( ($squares/scalar(@$list_ref)) - (&average($list_ref)**2 +) ); } sub expandList { my(@list) = @_; my @expanded = (); for( my $thisValue = 0; $thisValue < @list; $thisValue++ ) { my $voteCount = $list[ $thisValue ]; push( @expanded, split(/,/, "$thisValue," x $voteCount) ); } return @expanded; } sub validateSortingMethod { my @sorted = sort sortingMethod @_; my $nm = join(",", map { $_->{'item'} } @sorted); #print "V [$nm]\n"; if( ($nm eq "bestest,good,promising,lookinGood,bombing,bad,iffy,wors +test")|| ($nm eq "bestest,good,promising,lookinGood,bombing,iffy,bad,wors +test") ) { return 1; } else { return 0; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting Votes, Confidence & Deviations
by GrandFather (Saint) on Apr 12, 2007 at 20:41 UTC | |
|
Re: Sorting Votes, Confidence & Deviations
by billisdog (Sexton) on Apr 12, 2007 at 21:05 UTC | |
by billisdog (Sexton) on Apr 12, 2007 at 21:08 UTC | |
by Anonymous Monk on Apr 13, 2007 at 14:40 UTC | |
by Anonymous Monk on Apr 13, 2007 at 19:35 UTC | |
|
Re: Sorting Votes, Confidence & Deviations
by NiJo (Friar) on Apr 12, 2007 at 20:46 UTC | |
|
Re: Sorting Votes, Confidence & Deviations
by aijin (Monk) on Apr 13, 2007 at 21:06 UTC | |
|
Re: Sorting Votes, Confidence & Deviations
by punch_card_don (Curate) on Apr 13, 2007 at 15:04 UTC | |
|
Re: Sorting Votes, Confidence & Deviations
by snoopy (Curate) on Apr 18, 2007 at 05:25 UTC |