diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2019-09-16 03:31:57 +0100 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2019-09-16 03:31:57 +0100 |
| commit | 54a9d74b5a9f064c6cd4849d3d4193fec6d64e84 (patch) | |
| tree | 69d0e5f5fba465d83ce76a6a10f2aa30062e91e6 /challenge-026 | |
| parent | a2b6730c6bdbcaa800a30029f517de44f3f859f4 (diff) | |
| download | perlweeklychallenge-club-54a9d74b5a9f064c6cd4849d3d4193fec6d64e84.tar.gz perlweeklychallenge-club-54a9d74b5a9f064c6cd4849d3d4193fec6d64e84.tar.bz2 perlweeklychallenge-club-54a9d74b5a9f064c6cd4849d3d4193fec6d64e84.zip | |
add solution week 26 task 2
Diffstat (limited to 'challenge-026')
| -rw-r--r-- | challenge-026/steven-wilson/perl5/ch-2.pl | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-026/steven-wilson/perl5/ch-2.pl b/challenge-026/steven-wilson/perl5/ch-2.pl new file mode 100644 index 0000000000..ad66f64f94 --- /dev/null +++ b/challenge-026/steven-wilson/perl5/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-09-16 +# Week: 026 + +# Task #2 +# Create a script that prints mean angles of the given list of angles in +# degrees. Please read wiki page that explains the formula in details +# with an example. +# https://en.wikipedia.org/wiki/Mean_of_circular_quantities +# usage: perl ch-2.pl 355 5 15 +# output: Mean of the angles 355 5 15 is 5 + +use strict; +use warnings; +use feature qw/ say /; +use Test::More; +use List::Util qw/ sum /; +use Math::Trig; + +my @t_angles = qw/ 355 5 15 /; + +ok( sprintf( "%.0f", calculate_circular_mean( \@t_angles ) ) == 5, "test circular mean" ); +ok( sprintf( "%.3f", mean_sines( \@t_angles ) ) == 0.086, "test mean sines" ); +ok( sprintf( "%.3f", mean_cosines( \@t_angles ) ) == 0.986, "test mean cosines" ); +done_testing(); + +my @angles = @ARGV; + +say "Mean of the angles ", + join( " ", @angles, "is", calculate_circular_mean( \@angles ) ); + +sub calculate_circular_mean { + my $ref_angles = shift; + my @angles = @{$ref_angles}; + my $s = mean_sines($ref_angles); + my $c = mean_cosines($ref_angles); + my $mean; + if ( $s > 0 && $c > 0 ) { + $mean = rad2deg( atan( $s / $c ) ); + } + elsif ( $c < 0 ) { + $mean = rad2deg( atan( $s / $c ) ) + 180; + } + else { + $mean = rad2deg( atan( $s / $c ) ) + 360; + } + return $mean; +} + +sub mean_cosines { + my $ref_angles = shift; + my @angles = @{$ref_angles}; + my @cosines = map { cos( deg2rad($_) ) } @angles; + my $sum_cosines = sum @cosines; + return ( $sum_cosines / @angles ); + +} + +sub mean_sines { + my $ref_angles = shift; + my @angles = @{$ref_angles}; + my @sines = map { sin( deg2rad($_) ) } @angles; + my $sum_sines = sum @sines; + return ( $sum_sines / @angles ); +} |
