diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-09-17 12:23:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-17 12:23:58 +0100 |
| commit | 4f6cc4f1da13691ebd046ce0959ce9140b990ec8 (patch) | |
| tree | fbe0c90f00a4c6e28b69c4d43e0e36c6873b6df6 | |
| parent | f92885e49151f7d4e930ec0ecee4f9671692e72b (diff) | |
| parent | b54130ab5cd72084e4ef6c7ae67acef4e4345edf (diff) | |
| download | perlweeklychallenge-club-4f6cc4f1da13691ebd046ce0959ce9140b990ec8.tar.gz perlweeklychallenge-club-4f6cc4f1da13691ebd046ce0959ce9140b990ec8.tar.bz2 perlweeklychallenge-club-4f6cc4f1da13691ebd046ce0959ce9140b990ec8.zip | |
Merge pull request #641 from andrezgz/challenge-026
challenge-026 andrezgz solution
| -rw-r--r-- | challenge-026/andrezgz/perl5/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-026/andrezgz/perl5/ch-1.sh | 10 | ||||
| -rw-r--r-- | challenge-026/andrezgz/perl5/ch-2.pl | 35 |
3 files changed, 68 insertions, 0 deletions
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; |
