aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-026/duane-powell/perl5/ch-1.pl32
-rwxr-xr-xchallenge-026/duane-powell/perl5/ch-2.pl34
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-026/duane-powell/perl5/ch-1.pl b/challenge-026/duane-powell/perl5/ch-1.pl
new file mode 100755
index 0000000000..4a088bcb18
--- /dev/null
+++ b/challenge-026/duane-powell/perl5/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+use strict;
+
+# 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
+
+my $stones = shift || "chancellor";
+my $jewels = shift || "chocolate";
+
+$stones =~ s/[^a-zA-Z]//g;
+$jewels =~ s/[^a-zA-Z]//g;
+
+my $exp = join('|',split(//,$stones));
+my $count = () = $jewels =~ /$exp/g;
+print "$count\n";
+
+__END__
+
+./ch-1.pl
+8
+
+./ch-1.pl abcdefgh ijklmnop
+0
+
+./ch-1.pl abcdefgh abcd
+4
+
+./ch-1.pl abcdefgh aaaaahhhhh
+10
+
diff --git a/challenge-026/duane-powell/perl5/ch-2.pl b/challenge-026/duane-powell/perl5/ch-2.pl
new file mode 100755
index 0000000000..3121a97d2f
--- /dev/null
+++ b/challenge-026/duane-powell/perl5/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+use strict;
+use Math::Trig;
+
+# Create a script that prints mean angles of the given list of angles in degrees. Please read see
+# https://en.wikipedia.org/wiki/Mean_of_circular_quantities
+# that explains the formula in details with an example
+
+my @angles = @ARGV;
+my $count = scalar(@angles);
+my $x;
+my $y;
+foreach (@angles) {
+ $x += cos($_ * pi/180);
+ $y += sin($_ * pi/180);
+}
+
+my $angle_mean = atan2( $y/$count, $x/$count) * 180/pi;
+print "The mean of angles ", join(',',@angles), " is $angle_mean\n";
+
+__END__
+
+/ch-2.pl 10 20 30
+The mean of angles 10,20,30 is 20
+
+./ch-2.pl 355 5 15
+The mean of angles 355,5,15 is 5
+
+./ch-2.pl 5 10 15 20 25 30 35
+The mean of angles 5,10,15,20,25,30,35 is 20
+
+./ch-2.pl 180 270 360
+The mean of angles 180,270,360 is -90
+