diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-23 23:46:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-23 23:46:57 +0000 |
| commit | 920a7f4b19080d1f23aaa709af6e1445ad4380fd (patch) | |
| tree | 23dccd0f18efde8ca6caa5427eb45fa6ee4bec8e | |
| parent | 8a9f7b939b6bed0342312920abc04db6991384eb (diff) | |
| parent | 5961e0f6eb756d744785bab389d5181ffbf8b9f3 (diff) | |
| download | perlweeklychallenge-club-920a7f4b19080d1f23aaa709af6e1445ad4380fd.tar.gz perlweeklychallenge-club-920a7f4b19080d1f23aaa709af6e1445ad4380fd.tar.bz2 perlweeklychallenge-club-920a7f4b19080d1f23aaa709af6e1445ad4380fd.zip | |
Merge pull request #3359 from pauloscustodio/026
Add Perl solution to challenge 026
| -rw-r--r-- | challenge-026/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-026/paulo-custodio/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-026/paulo-custodio/perl/ch-2.pl | 33 | ||||
| -rw-r--r-- | challenge-026/paulo-custodio/test.pl | 20 |
4 files changed, 77 insertions, 0 deletions
diff --git a/challenge-026/paulo-custodio/README b/challenge-026/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-026/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-026/paulo-custodio/perl/ch-1.pl b/challenge-026/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..ae55779dda --- /dev/null +++ b/challenge-026/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +# 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; +use 5.030; + +say count(@ARGV); + +sub count { + my($letters, $word) = @_; + my %letters; $letters{$_}=1 for grep {/\w/} split //, $letters; + return scalar grep {$letters{$_}} split //, $word; +} diff --git a/challenge-026/paulo-custodio/perl/ch-2.pl b/challenge-026/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..bbef0fec83 --- /dev/null +++ b/challenge-026/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +# 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. + +use strict; +use warnings; +use 5.030; +use Math::Trig; +use List::Util 'sum'; + +say mean(@ARGV); + +sub mean { + my(@a) = @_; + + # convert to radians + map {$_ = deg2rad($_)} @a; + + # compute sum of sin and cos + my $x = sum( map {cos($_)} @a ); + my $y = sum( map {sin($_)} @a ); + + # compute mean + my $a = atan2($y, $x); + + # convert back to degrees + $a = rad2deg($a); + return $a; +} diff --git a/challenge-026/paulo-custodio/test.pl b/challenge-026/paulo-custodio/test.pl new file mode 100644 index 0000000000..aac0ec953d --- /dev/null +++ b/challenge-026/paulo-custodio/test.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; +use 5.030; + +is capture("perl perl/ch-1.pl chancellor chocolate"), "8\n"; + +is capture("perl perl/ch-2.pl 0 90"), "45\n"; +is capture("perl perl/ch-2.pl 0 45 90"), "45\n"; + +done_testing; + +sub capture { + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \t\v\f\r]*\n/\n/g; + return $out; +} |
