aboutsummaryrefslogtreecommitdiff
path: root/challenge-026
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-09-23 01:23:33 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-09-23 01:23:33 +0100
commiteab8e35282ab29904a9e6d430defdb30849766ce (patch)
tree8557c355e1b8a6fe4e6f31825d023e9a4b6c192f /challenge-026
parent1d20d754685d4f3f415ff69273db4bbc55a02c8e (diff)
downloadperlweeklychallenge-club-eab8e35282ab29904a9e6d430defdb30849766ce.tar.gz
perlweeklychallenge-club-eab8e35282ab29904a9e6d430defdb30849766ce.tar.bz2
perlweeklychallenge-club-eab8e35282ab29904a9e6d430defdb30849766ce.zip
- Added solutions by Mark Senn.
Diffstat (limited to 'challenge-026')
-rw-r--r--challenge-026/mark-senn/perl6/ch-1.p682
-rw-r--r--challenge-026/mark-senn/perl6/ch-2.p641
2 files changed, 123 insertions, 0 deletions
diff --git a/challenge-026/mark-senn/perl6/ch-1.p6 b/challenge-026/mark-senn/perl6/ch-1.p6
new file mode 100644
index 0000000000..eb1dbb57b4
--- /dev/null
+++ b/challenge-026/mark-senn/perl6/ch-1.p6
@@ -0,0 +1,82 @@
+# Perl Weekly Challenge - 026
+# Task #1
+#
+# See
+# engineering.purdue.edu/~mark/pwc -026 -1. pdf
+# for more information.
+#
+# The command line should be, for example ,
+# perl6 t.p6 stones jewels
+# The values for "stones" and "jewels"
+# default to "chancellor" and "chocolate ".
+#
+#
+
+# Run using Perl v6.d;
+use v6.d;
+
+sub MAIN($a = 'chancellor', $b = 'chocolate')
+{
+ # Check that$a and$b consist of only upper and lowerccase letters.
+ ($a ~~ /^^<[A..Za..z]>+$$/) or die qq/"$a" must consist of one or more letters /;
+ ($b ~~ /^^<[A..Za..z]>+$$/) or die qq/"$a" must consist of one or more letters /;
+
+ # Convert$a and$b to arrays.
+ my @a =$a.comb (/./).unique;
+ my @b =$b.comb (/./);
+
+
+ #
+ # Array -based solution.
+ #
+
+ # If keys to a hash can be expressed as small
+ # non -negative integers you may want to use an
+ # array instead of a hash.
+ my @in -a;
+ @a.map({@in -a[$_.ord] = True });
+ $count = 0;
+ @b.map({@in -a[$_.ord] and $count++});
+ $count.say;
+
+
+ #
+ # Cross -product based solution.
+ #
+
+ $count = 0;
+ (@a X @b).map({$_[0] eq $_[1] and $count++});
+ $count.say;
+
+
+ #
+ # Hash -based solution.
+ #
+
+ # Make a %a hash with letter keys and True values.
+ # The letters in %a are the ones we count in$b.
+ my %a = @a.map({$_ => True });
+
+ # Count the characters in$b that occur in$a.
+ my $count = 0;
+ @b.map ({%a{$_} and $count++});
+
+ $count.say;
+
+
+ #
+ # Set -based solution.
+ #
+
+ # When I was looking up "perl6 degrees to radians" to see if there was
+ # a built -in Perl 6 method to do it I came across
+ # http :// blogs.perl.org/users/laurent_r /2019/09/
+ # perl -weekly -challenge -26-common -letters -and -mean -angles.html
+ # showing how to convert a list/array to a set.
+ # I like Laurent Rosenfeld's solution better than any I did above.
+ # The following is based on his work.
+ my $aset = @a.Set;
+ $count = @b.grep({$_ (elem) $aset }).elems;
+ $count.say;
+}
+
diff --git a/challenge-026/mark-senn/perl6/ch-2.p6 b/challenge-026/mark-senn/perl6/ch-2.p6
new file mode 100644
index 0000000000..823df39a4f
--- /dev/null
+++ b/challenge-026/mark-senn/perl6/ch-2.p6
@@ -0,0 +1,41 @@
+#
+# Perl Weekly Challenge - 026
+# Task #2
+#
+# See
+# engineering.purdue.edu/~mark/pwc -026 -2. pdf
+# for more information.
+#
+# The command line should be, for example ,
+# perl6 t.p6 1 2 3
+# to get the mean angle of 1, 2, and 3 degrees.
+#
+
+# Run using Perl v6.d;
+use v6.d;
+
+sub MAIN(*@deg where @deg.elems > 0)
+{
+ # Convert from degrees to radians.
+ my @rad = @deg <<*>> (pi /180);
+
+ # Transform from degrees to polar coordinates.
+ my Complex @z = @rad.map({e**(i *$_)});
+
+ # Compute$p -bar in polar coordinates.
+ my$p -bar = ([+] @z) / @z.elems;
+
+ # Compute arctangent and convert from radians to degrees.
+ # Sample inputs and outputs:
+ # INPUT OUTPUT COMMENT
+ # ---------- ----------------------- -------
+ # 1 1 1
+ # 1 2 1.5
+ # 1 2 3 2
+ # 10 -10 0
+ # 10 20 30 19.999999999999996 wrong
+ # 60 59.99999999999999 wrong
+ # 350 10 -1.614809932057922e-15 wrong
+ my$theta -bar = atan2($p -bar.im ,$p -bar.re) * (180/pi);
+ $theta -bar.say;
+}