diff options
| author | Daniel Mantovani <daniel@gmail.com> | 2019-09-21 18:55:27 -0300 |
|---|---|---|
| committer | Daniel Mantovani <daniel@gmail.com> | 2019-09-21 18:55:27 -0300 |
| commit | 8e5c885aa8c685d6552cbce334f1b2080ff05399 (patch) | |
| tree | a49d851170031102b28da5e079b1940b9b36ae27 /challenge-026 | |
| parent | d86f9f6c373991692799baef36420118f7fa279b (diff) | |
| download | perlweeklychallenge-club-8e5c885aa8c685d6552cbce334f1b2080ff05399.tar.gz perlweeklychallenge-club-8e5c885aa8c685d6552cbce334f1b2080ff05399.tar.bz2 perlweeklychallenge-club-8e5c885aa8c685d6552cbce334f1b2080ff05399.zip | |
my proposed solutions for challenge-026, p5 1 & 2
Diffstat (limited to 'challenge-026')
| -rw-r--r-- | challenge-026/daniel-mantovani/perl5/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-026/daniel-mantovani/perl5/ch-2.pl | 35 |
2 files changed, 57 insertions, 0 deletions
diff --git a/challenge-026/daniel-mantovani/perl5/ch-1.pl b/challenge-026/daniel-mantovani/perl5/ch-1.pl new file mode 100644 index 0000000000..ae7c8edd15 --- /dev/null +++ b/challenge-026/daniel-mantovani/perl5/ch-1.pl @@ -0,0 +1,22 @@ +# 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 v5.20; + +# let's read the two strings from command line +my ( $stones, $jewels ) = @ARGV; + +my $n = 0; + +# we just count the times each char from $stones exists in $jewels + +$n += $jewels =~ /$_/ for split '', $stones; + +# and I think that's it, unless I misunderstood the challenge (which is very likely) + +say "$stones letters found in $jewels are $n in total"; diff --git a/challenge-026/daniel-mantovani/perl5/ch-2.pl b/challenge-026/daniel-mantovani/perl5/ch-2.pl new file mode 100644 index 0000000000..b849947081 --- /dev/null +++ b/challenge-026/daniel-mantovani/perl5/ch-2.pl @@ -0,0 +1,35 @@ +# 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 v5.20; + +# first we define constant pi as twice a 90 degrees angle: + +use constant PI => 2 * atan2( 1, 0 ); + +# now we read angles list + +my @angles = @ARGV or die "Usage: perl $0 <list of angles in degrees>"; + +# we are going to just add sines and cosines of each angle. +# The wiki page actually asks to average them, but as we are going to calculate +# an angle at the end, there is no difference with calculating it with the sums + +my ( $sins_sum, $coss_sum ) = ( 0, 0 ); + +$sins_sum += sin( $_ * PI / 180 ) for @angles; +$coss_sum += cos( $_ * PI / 180 ) for @angles; + +# There is a case when no circular mean exists, and it is when +# all angles are uniformly distributed on the circle. +# In that case, both $sins_sum and $coss_sum are 0 +# To avoid precision math errors, we will assume that when sum of +# square of sum of sines and square of sum of cosines is less than +# 1 / 1_000_000 means both are 0 + +say $sins_sum * $sins_sum + $coss_sum * $coss_sum < 1e-6 + ? 'No circular mean exists for given angles' + : sprintf( "Circular mean is: %.2f", 180 / PI * atan2 $sins_sum, $coss_sum ); + |
