aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-01-23 23:11:31 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-01-23 23:11:31 +0000
commit5961e0f6eb756d744785bab389d5181ffbf8b9f3 (patch)
tree4f0c00a4118dbf038fc3915167bb10eae8b68c1a
parentefa08f1c70928d4b4f59ee48b0014f88eca8ee89 (diff)
downloadperlweeklychallenge-club-5961e0f6eb756d744785bab389d5181ffbf8b9f3.tar.gz
perlweeklychallenge-club-5961e0f6eb756d744785bab389d5181ffbf8b9f3.tar.bz2
perlweeklychallenge-club-5961e0f6eb756d744785bab389d5181ffbf8b9f3.zip
Add Perl solution to challenge 026
-rw-r--r--challenge-026/paulo-custodio/README1
-rw-r--r--challenge-026/paulo-custodio/perl/ch-1.pl23
-rw-r--r--challenge-026/paulo-custodio/perl/ch-2.pl33
-rw-r--r--challenge-026/paulo-custodio/test.pl20
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;
+}