Created
September 22, 2019 18:32
-
-
Save adamcrussell/c0bb70cdac8b127e44db55afe225610d to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 026
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment