sub Average { my @num = @_; #declare variables my $avg; my $check = @num; if (!@num) { #If there is no inputted value, return 0 return 0; } elsif (AsNumber($_[0]) ){ #foreach list, get the average foreach(@num){ $avg += $_ for @_; return ($avg / scalar @_ ); } } else{ return undef; } } sub Median { my $median; #declare variables my @array = @_; my @array = sort { $a <=> $b } @array; #sort to arrange the numbers in order if (!@_) {#If there is no inputted value, return 0 return 0; } elsif (@array % 2){ #if divisible by 2, do this to get median $median = $array[($#array / 2)]; return $median; } else{ #if not, use this to get median $median = $array[int($#array / 2)] + (($array[int($#array / 2) + 1] - $array[int($#array / 2)]) / 2); return $median; } } sub Low { my @list = @_; #declare variables and assign values my $check = @list; if (!@list) { return 0;#If there is no inputted value, return 0 } else { @list = sort {$a <=> $b} @list; #sort and find lowest value my $low = $list[0]; #take lowest value and return the value return $low; } } sub High { my @array = @_; my $array = @_; if (!@array) { #If there is no inputted value, return 0 return 0; } elsif (looks_like_number(@array)){ @array = sort { $b <=> $a } @array; #reverse sort to find the highest number my $high = $array[0]; #grab the highest value and return return $high; } else{ return undef; } } # Utility Subroutines ... # AsNumber( $s ) returns # the number encoded by $s, or # undef if $s is not defined, or # undef if $s is not a number sub AsNumber { my ($s) = @_; return undef unless defined( $s ); my $number; eval { # make conversion failure a fatal error use warnings FATAL => qw(numeric); # try to use $s as a number $number = 0 + $s; }; if ($@) { # exception happened, thus not a number $number = undef; } return $number; } 1; # module end, must return true } #### use strict; use warnings; use diagnostics; use Scalar::Util qw(looks_like_number); use Test::More 'no_plan'; use ListStats; print "========================================\n"; diag( "Tests of Average" ); #AVERAGE IS DONE YEEEEEEEEAH # fix/add more tests for Average ... is( Average( ), 0, "Average empty list" ); is( Average( (2) ), 2, "Average singleton list" ); is( Average( (-7..5) ), -1, "Average list" ); is( Average( ("a") ), undef, "Average non-number in list" ); is( Average( 1, 2, 3, 4), 2.5, "Average numeric string - test one" ); is( Average( 52, "53", 93, 2678), 719, "Average numeric string - test two" ); print "========================================\n"; diag( "Tests of Median" ); is( Median( ), 0, "Median empty list" ); is( Median( (2) ), 2, "Median singleton list" ); is( Median( (-7..5) ), -1, "Median list" ); is( Median( ("a") ), undef, "Median non-number in list" ); is( Median( 0, "1", 2 ), 1, "Median numeric string - test one" ); is( Median( 0, "65", 34 ), 34, "Median numeric string - test two" ); #find length of string, get middle. print "========================================\n"; diag( "Tests of Low" ); # fix/add more tests for Low ... is( Low( ), 0, "Low empty list" ); is( Low( (2) ), 2, "Low singleton list" ); is( Low( (0,2,-2,-1) ), -2, "Low list" ); is( Low( ("a") ), undef, "Low non-number in list" ); is( Low( 0, "-1", 2 ), -1, "Low numeric string - test one" ); is( Low( 34, "-1", -47 ), -47, "Low numeric string - test two" ); print "========================================\n"; diag( "Tests of High" ); is( High( ), 0, "High empty list" ); is( High( (2) ), 2, "High singleton list" ); is( High( (0,2,-2,-1) ), 2, "High list" ); is( High( ("a") ), undef, "High non-number in list" ); is( High( 0, "-1", 2 ), 2, "High numeric string - test one" ); is( High( 0, "-1", 2,47, 2737 ), 2737, "High numeric string - test two" );