added average goodie

master
hunterlang 2011-12-31 22:03:50 -05:00
parent d618c4033f
commit b1a0914c0a
2 changed files with 34 additions and 0 deletions

32
average/goodie.pl Executable file
View File

@ -0,0 +1,32 @@
# Outputs the mean and median of the queried numbers.
if (!$type && $q_check =~ m/^(?:avg|average) ([0-9\. ]*)$/i) {
# get an array of the entered numbers
my @nums = split(" ", $1);
# initialize the sum
my $sum = 0;
# calculate the sum
$sum += $_ for @nums;
# get the length of the array
my $len = @nums;
# calculate the mean
my $mean = $sum/$len;
# sort the list numerically, least to greatest
@nums = sort { $a <=> $b } @nums;
my $med = 0;
if ($len % 2 eq 0) {
# get the two middle numbers, since the
# length is even, and calculate their mean
$med = ($nums[$len/2] + $nums[$len/2-1])/2;
}
else {
# get the middle number
$med = $nums[int($len/2)]
}
# set the results
$answer_results = qq(Mean: $mean\nMedian: $med);
if ($answer_results) {
$answer_type = 'average';
$type = 'E';
}
}

2
average/queries.txt Normal file
View File

@ -0,0 +1,2 @@
average 12 45 78 1234.12
avg 78 4221 95 .01 657 .0484