aboutsummaryrefslogtreecommitdiff
path: root/challenge-026
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-09-20 21:59:40 +0200
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-09-20 21:59:40 +0200
commitb0c7e074a39fa483f4a52cceb4170a2f749c630d (patch)
tree948601384c1f5a58978ddf6c127bb8b84b59a5dd /challenge-026
parent46742d04f0c11a82dd4db224590380788c05b15e (diff)
downloadperlweeklychallenge-club-b0c7e074a39fa483f4a52cceb4170a2f749c630d.tar.gz
perlweeklychallenge-club-b0c7e074a39fa483f4a52cceb4170a2f749c630d.tar.bz2
perlweeklychallenge-club-b0c7e074a39fa483f4a52cceb4170a2f749c630d.zip
Solutions to challenge 026 problem 1 and 2 in Perl 6 by Noud
Diffstat (limited to 'challenge-026')
-rw-r--r--challenge-026/noud/perl6/ch-1.p613
-rw-r--r--challenge-026/noud/perl6/ch-2.p645
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-026/noud/perl6/ch-1.p6 b/challenge-026/noud/perl6/ch-1.p6
new file mode 100644
index 0000000000..f31eed9c8a
--- /dev/null
+++ b/challenge-026/noud/perl6/ch-1.p6
@@ -0,0 +1,13 @@
+# 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.
+
+# I hope I understood the exercise.
+sub count_abc(Str $stones, Str $jewels) {
+ $jewels.comb.grep({$_ (elem) $stones.comb.Set}).elems;
+}
+
+count_abc('chancellor', 'chocolate').say;
diff --git a/challenge-026/noud/perl6/ch-2.p6 b/challenge-026/noud/perl6/ch-2.p6
new file mode 100644
index 0000000000..e1a03f3091
--- /dev/null
+++ b/challenge-026/noud/perl6/ch-2.p6
@@ -0,0 +1,45 @@
+# 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.
+
+sub mean(@array) {
+ @array.sum / @array.elems;
+}
+
+# Mean of circular quantities using atan2.
+sub mean_angle(@angles) {
+ atan2(mean(@angles.map(&sin)), mean(@angles.map(&cos)));
+}
+
+# Mean of circular quantities using complex numbers.
+sub mean_angle2(@angles) {
+ my $z = mean(@angles.map({ exp(i * $_) }));
+ atan($z.im / $z.re);
+}
+
+# Mean of circular quantities using modulo over the circle
+sub mean_angle3(@angles) {
+ mean(@angles.map({ ($_ + pi) % (2 * pi) - pi }));
+}
+
+# Compute means in radians
+mean_angle((0.1, 2 * pi + 0.2, -2 * pi + 0.3)).say;
+mean_angle2((0.1, 2 * pi + 0.2, -2 * pi + 0.3)).say;
+mean_angle3((0.1, 2 * pi + 0.2, -2 * pi + 0.3)).say;
+
+# Wrap methods to convert degrees to radians
+sub deg2rad($degree) {
+ $degree * pi / 180;
+}
+
+sub rad2deg($rad) {
+ $rad * 180 / pi;
+}
+
+&mean_angle.wrap: -> @arr { rad2deg(callwith(@arr.map(&deg2rad))) };
+&mean_angle2.wrap: -> @arr { rad2deg(callwith(@arr.map(&deg2rad))) };
+&mean_angle3.wrap: -> @arr { rad2deg(callwith(@arr.map(&deg2rad))) };
+
+mean_angle((10, 20 + 360, 30 - 360)).say;
+mean_angle2((10, 20 + 360, 30 - 360)).say;
+mean_angle3((10, 20 + 360, 30 - 360)).say;