aboutsummaryrefslogtreecommitdiff
path: root/challenge-026
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-09-23 01:45:08 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-09-23 01:45:08 +0100
commitde7d0b7df0a02c3c79f30f84ef76078168df4a58 (patch)
tree382a0037346d5f50785c118bb63317ff4cb4ac75 /challenge-026
parenteab8e35282ab29904a9e6d430defdb30849766ce (diff)
downloadperlweeklychallenge-club-de7d0b7df0a02c3c79f30f84ef76078168df4a58.tar.gz
perlweeklychallenge-club-de7d0b7df0a02c3c79f30f84ef76078168df4a58.tar.bz2
perlweeklychallenge-club-de7d0b7df0a02c3c79f30f84ef76078168df4a58.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-026')
-rw-r--r--challenge-026/colin-crain/perl5/ch-1.pl35
-rw-r--r--challenge-026/colin-crain/perl5/ch-2.pl57
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-026/colin-crain/perl5/ch-1.pl b/challenge-026/colin-crain/perl5/ch-1.pl
new file mode 100644
index 0000000000..8850b2c724
--- /dev/null
+++ b/challenge-026/colin-crain/perl5/ch-1.pl
@@ -0,0 +1,35 @@
+#! /opt/local/bin/perl
+#
+# stones_and_jewels.pl
+#
+# task: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.
+#
+# 2019 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN
+
+my ($stone, $jewel) = @ARGV;
+my $jewels = { map { $_, 1 } (split //, $jewel) };
+my @gravel = split //, $stone;
+my $result = scalar (grep { $jewels->{$_} } @gravel);
+say $result;
+
+## this could alternately be done in just two lines:
+
+# my $jewels = { map { $_, 1 } (split //, $ARGV[1]) };
+# say scalar (grep { $jewels->{$_} } (split //, $ARGV[0]));
+
+
diff --git a/challenge-026/colin-crain/perl5/ch-2.pl b/challenge-026/colin-crain/perl5/ch-2.pl
new file mode 100644
index 0000000000..763bc30e70
--- /dev/null
+++ b/challenge-026/colin-crain/perl5/ch-2.pl
@@ -0,0 +1,57 @@
+#! /opt/local/bin/perl
+#
+# mean_angles.pl
+#
+# task: Create a script that prints mean angles of the given list of angles in degrees.
+#
+# usage: mean_angles.pl angle1 angle2 angle3
+#
+# 2019 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+use List::Util qw( reduce );
+
+my $pi = 3.14159265358979;
+
+## ## ## ## ## MAIN
+
+## the angles are fed to the program in degrees, but Perl's trig functions want radians
+my @angles = map { deg2rad($_) } @ARGV;
+
+## first we convert to polar, r->(cos r, sin r)
+## summing and dividing the coordinates to provide the average
+## then use atan2(y,x) to get the result in radians
+
+## this works well:
+# my ($sumx, $sumy);
+# for my $angle ( @angles ) {
+# $sumx += cos $angle;
+# $sumy += sin $angle;
+# }
+# my $v_avg = atan2( $sumy/(scalar @angles), $sumx/(scalar @angles) );
+
+## but I like this more:
+my $x = (reduce {$a + cos $b} 0, @angles) / scalar @angles;
+my $y = (reduce {$a + sin $b} 0, @angles) / scalar @angles;
+my $v_avg = atan2( $y, $x );
+
+## convert back to degrees
+say rad2deg( $v_avg );
+
+
+
+## ## ## ## ## SUBS
+
+sub deg2rad {
+ return ($_[0]/180) * $pi;
+}
+
+sub rad2deg {
+ return ($_[0]/$pi) * 180;
+}
+
+