diff options
| -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 ); + |
