aboutsummaryrefslogtreecommitdiff
path: root/challenge-026
diff options
context:
space:
mode:
authorYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2019-09-16 15:03:36 +0800
committerYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2019-09-16 15:03:36 +0800
commit84b454fe21babc53b9fbeacf6f0adb77690ce410 (patch)
tree1e992f4b7d89486a402e12bc8e9b69752125742e /challenge-026
parenta4c02e7ac21b2b0aaf80f3200a660c677dbdde12 (diff)
parente3189b00ff7aa4f98ae69ef18de6f3f43f4945d7 (diff)
downloadperlweeklychallenge-club-84b454fe21babc53b9fbeacf6f0adb77690ce410.tar.gz
perlweeklychallenge-club-84b454fe21babc53b9fbeacf6f0adb77690ce410.tar.bz2
perlweeklychallenge-club-84b454fe21babc53b9fbeacf6f0adb77690ce410.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-026')
-rw-r--r--challenge-026/steven-wilson/perl5/ch-1.pl27
-rw-r--r--challenge-026/steven-wilson/perl5/ch-2.pl66
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-026/steven-wilson/perl5/ch-1.pl b/challenge-026/steven-wilson/perl5/ch-1.pl
new file mode 100644
index 0000000000..d4e9d47f1c
--- /dev/null
+++ b/challenge-026/steven-wilson/perl5/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+# Author: Steven Wilson
+# Date: 2019-09-16
+# Week: 026
+
+# Task #1
+# Create a script that accepts two strings, let us call it, “stones” and
+# “jewels”. It should print the count of “alphabet” from the string
+# “stones” found in the string “jewels”. For example, if your stones is
+# “chancellor” and “jewels” is “chocolate”, then the script should print
+# “8”. To keep it simple, only A-Z,a-z characters are acceptable. Also
+# make the comparison case sensitive.
+# usage: $ perl ch-1.pl chancellor chocolate
+# output: 8
+
+
+use strict;
+use warnings;
+use feature qw/ say /;
+
+my ($stones, $jewels) = @ARGV;
+
+my %alphabet = map { $_ => 0 } split //, $stones;
+
+my $count = grep { exists $alphabet{$_} } split //, $jewels;
+
+say $count;
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 );
+}