From b54130ab5cd72084e4ef6c7ae67acef4e4345edf Mon Sep 17 00:00:00 2001 From: andrezgz Date: Tue, 17 Sep 2019 08:19:28 -0300 Subject: challenge-026 andrezgz solution --- challenge-026/andrezgz/perl5/ch-1.pl | 23 +++++++++++++++++++++++ challenge-026/andrezgz/perl5/ch-1.sh | 10 ++++++++++ challenge-026/andrezgz/perl5/ch-2.pl | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 challenge-026/andrezgz/perl5/ch-1.pl create mode 100644 challenge-026/andrezgz/perl5/ch-1.sh create mode 100644 challenge-026/andrezgz/perl5/ch-2.pl diff --git a/challenge-026/andrezgz/perl5/ch-1.pl b/challenge-026/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..e2bdf04779 --- /dev/null +++ b/challenge-026/andrezgz/perl5/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-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. + +use strict; +use warnings; + +die "Usage: $0 word1 word2" unless @ARGV == 2; +my ($w1, $w2) = @ARGV; + +print scalar # print the number + grep { + $_ =~ /[A-Za-z]/ # of alphabethic case insensitive characters + && index($w2, $_) != -1 # that exist on the second word + } split //, $w1; # from each one of the first word diff --git a/challenge-026/andrezgz/perl5/ch-1.sh b/challenge-026/andrezgz/perl5/ch-1.sh new file mode 100644 index 0000000000..faf638c62f --- /dev/null +++ b/challenge-026/andrezgz/perl5/ch-1.sh @@ -0,0 +1,10 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-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. +perl -e 'print scalar grep {$_ =~ /[A-Za-z]/ && index($ARGV[1], $_) != -1} split //, $ARGV[0];' $1 $2 diff --git a/challenge-026/andrezgz/perl5/ch-2.pl b/challenge-026/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..389d631e2a --- /dev/null +++ b/challenge-026/andrezgz/perl5/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-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 + +use strict; +use warnings; +use Math::Trig qw/rad2deg deg2rad/; + +die "Usage: $0 angle-deg [angle-deg..]" if @ARGV < 1; +die "Angles should be numbers (in degrees)" if grep { $_ !~ /\d+/ } @ARGV; +my @angles = map {deg2rad $_} @ARGV; + +my ($sin_sum,$cos_sum) = (0,0); +foreach my $angle (@angles) { + $sin_sum += sin $angle; + $cos_sum += cos $angle; +} + +if (abs $cos_sum < 1e-10 ) { + print 'Mean angle: ? (Cosine sum is zero)'; + exit 0; +} + +#Scaling does not matter for atan2, no need to calculate sin and cos means +my $angles_mean = rad2deg atan2 $sin_sum, $cos_sum; + +if ($cos_sum < 0 ) {$angles_mean +=180} +elsif ($sin_sum < 0 ) {$angles_mean +=360} + +$angles_mean -= 360 if $angles_mean > 180; #analogous angle +print 'Mean angle: '.$angles_mean; -- cgit