aboutsummaryrefslogtreecommitdiff
path: root/challenge-026
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2019-09-22 14:59:52 -0400
committerAdam Russell <ac.russell@live.com>2019-09-22 14:59:52 -0400
commit4bda7287cb25a42f0444e5e84a95f294c4e5f56a (patch)
treea864a7d981bfdc68ab5ebbe7a4fbead3e3603c2f /challenge-026
parentb46a93f225908da05ff54d40728c59ad8adf7c18 (diff)
downloadperlweeklychallenge-club-4bda7287cb25a42f0444e5e84a95f294c4e5f56a.tar.gz
perlweeklychallenge-club-4bda7287cb25a42f0444e5e84a95f294c4e5f56a.tar.bz2
perlweeklychallenge-club-4bda7287cb25a42f0444e5e84a95f294c4e5f56a.zip
solutions for challenge 026
Diffstat (limited to 'challenge-026')
-rw-r--r--challenge-026/adam-russell/blog.txt1
-rw-r--r--challenge-026/adam-russell/perl5/ch-1.pl44
-rw-r--r--challenge-026/adam-russell/perl5/ch-2.pl35
3 files changed, 80 insertions, 0 deletions
diff --git a/challenge-026/adam-russell/blog.txt b/challenge-026/adam-russell/blog.txt
new file mode 100644
index 0000000000..b0d5a96b5c
--- /dev/null
+++ b/challenge-026/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/9318.html
diff --git a/challenge-026/adam-russell/perl5/ch-1.pl b/challenge-026/adam-russell/perl5/ch-1.pl
new file mode 100644
index 0000000000..27b6048ea5
--- /dev/null
+++ b/challenge-026/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,44 @@
+use strict;
+use warnings;
+##
+# Create a script that accepts two strings, let us call it them "stones" and "jewels".
+# It should print the count of the "alphabet" from "stones" found in "jewels".
+##
+
+sub contains_remove{
+ my($c) = @_;
+ return sub{
+ my($word) = @_;
+ $word =~ s/$c//;
+ return $word;
+ }
+}
+
+sub make_checks{
+ my($word) = @_;
+ my @letters = split(//, $word);
+ my @checks;
+ for my $c (@letters){
+ push @checks, contains_remove($c);
+ }
+ return @checks;
+}
+
+sub check_jewels{
+ my($jewels, $checks) = @_;
+ my $count = 0;
+ while(@{$checks}){
+ my $check = pop @{$checks};
+ my $c = $check->($jewels);
+ $count += (length($jewels) - length($c));
+ }
+ return $count;
+}
+
+MAIN:{
+ my($stones, $jewels) = ($ARGV[0], $ARGV[1]);
+ chomp($stones);
+ chomp($jewels);
+ my @checks = make_checks($stones);
+ print check_jewels($jewels, \@checks) . "\n";
+}
diff --git a/challenge-026/adam-russell/perl5/ch-2.pl b/challenge-026/adam-russell/perl5/ch-2.pl
new file mode 100644
index 0000000000..75d0e699c5
--- /dev/null
+++ b/challenge-026/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,35 @@
+use strict;
+use warnings;
+##
+# Create a script that prints mean angles of the given list of angles in degrees.
+##
+use constant PI => atan2(1,1) * 4;
+
+sub deg2rad {
+ my($degrees) = @_;
+ return ($degrees / 180) * PI;
+}
+
+sub rad2deg {
+ my($radians) = @_;
+ return ($radians / PI) * 180;
+}
+
+sub compute_mean_angle{
+ my(@angles) = @_;
+ my $sum = 0;
+ map { $sum += $_ } map { sin($_) } @angles;
+ my $sin_mean = $sum / @angles;
+ $sum = 0;
+ map { $sum += $_ } map { cos($_) } @angles;
+ my $cos_mean = $sum / @angles;
+ return atan2($sin_mean, $cos_mean) if $sin_mean > 0 && $cos_mean > 0;
+ return atan2($sin_mean, $cos_mean) + deg2rad(180) if $cos_mean < 0;
+ return atan2($sin_mean, $cos_mean) + deg2rad(360) if $sin_mean < 0 && $cos_mean > 0;
+}
+
+
+MAIN:{
+ my @angles = map { deg2rad($_) } @ARGV[0 .. (@ARGV - 1)];
+ print rad2deg(compute_mean_angle(@angles)) . "\n";
+}