if ($answer_counter == 1): ?>
endif; ?>
#!/usr/bin/perl
use List::MoreUtils qw(pairwise);
use List::Util qw(sum);
use strict;
sub read_file {
my ($filename) = @_;
open F, '<', $filename or die "Could not open $filename: $!";
my %data;
while (<F>) {
my ($id, @data) = split;
$data{$id} = \@data;
}
close F;
return %data;
}
sub output_file {
my ($filename, %data) = @_;
open F, '>', $filename or die "Could not open $filename: $!";
for (sort keys %data) {
print F "$_\t$data{$_}\n";
}
close F;
}
my %votes = read_file 'votes.tsv';
my %weights = read_file 'weights.tsv';
my %unweighted;
while (my ($id, $data) = each(%votes)) {
my $sum = List::Util::sum(@$data);
$unweighted{$id} = $sum < 0 ? -1 :
$sum > 0 ? +1 : 0;
}
output_file('unweighted.tsv', %unweighted);
my %weighted;
while (my ($id, $data) = each(%weights)) {
my $dot_prod = sum(pairwise { $a * $b } @{$votes{$id}}, @$data);
$weighted{$id} = $dot_prod < 0 ? -1 :
$dot_prod > 0 ? +1 : 0;
}
output_file('weighted.tsv', %weighted);