aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-09 13:18:54 +0100
committerGitHub <noreply@github.com>2020-05-09 13:18:54 +0100
commit1fb3d51769f73123d2d4db70b96fdb70b194ed2f (patch)
tree378438cd694863e18cca4286ca738e71c0954964 /challenge-059
parent8df1cd729dfec2ba13d3f51dbb67dce56f2f2806 (diff)
parentb8649d44c3a5e1bd34a87a14c13426fda45acfd7 (diff)
downloadperlweeklychallenge-club-1fb3d51769f73123d2d4db70b96fdb70b194ed2f.tar.gz
perlweeklychallenge-club-1fb3d51769f73123d2d4db70b96fdb70b194ed2f.tar.bz2
perlweeklychallenge-club-1fb3d51769f73123d2d4db70b96fdb70b194ed2f.zip
Merge pull request #1688 from noudald/challenge-059-noud
Solution to challenge 059 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/noud/raku/ch-1.p618
-rw-r--r--challenge-059/noud/raku/ch-2.p642
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-059/noud/raku/ch-1.p6 b/challenge-059/noud/raku/ch-1.p6
new file mode 100644
index 0000000000..570ab00efe
--- /dev/null
+++ b/challenge-059/noud/raku/ch-1.p6
@@ -0,0 +1,18 @@
+# You are given a linked list and a value k. Write a script to partition the
+# linked list such that all nodes less than k come before nodes greater than or
+# equal to k. Make sure you preserve the original relative order of the nodes
+# in each of the two partitions.
+#
+# For example:
+#
+# Linked List: 1 → 4 → 3 → 2 → 5 → 2
+#
+# k = 3
+#
+# Expected Output: 1 → 2 → 2 → 4 → 3 → 5.
+
+sub partition(@l, $k) {
+ return [|(@l.grep({ $_ < $k })), |(@l.grep({ $_ >= $k }))];
+}
+
+say partition([1, 4, 3, 2, 5, 2], 3).join(' → ');
diff --git a/challenge-059/noud/raku/ch-2.p6 b/challenge-059/noud/raku/ch-2.p6
new file mode 100644
index 0000000000..1c1b1fb693
--- /dev/null
+++ b/challenge-059/noud/raku/ch-2.p6
@@ -0,0 +1,42 @@
+# Helper Function
+#
+# For this task, you will most likely need a function f(a,b) which returns the
+# count of different bits of binary representation of a and b.
+#
+# For example, f(1,3) = 1, since:
+#
+# Binary representation of 1 = 01
+# Binary representation of 3 = 11
+#
+# There is only 1 different bit. Therefore the subroutine should return 1. Note
+# that if one number is longer than the other in binary, the most significant
+# bits of the smaller number are padded (i.e., they are assumed to be zeroes).
+#
+# Script Output
+#
+# You script should accept n positive numbers. Your script should sum the
+# result of f(a,b) for every pair of numbers given:
+# For example, given 2, 3, 4, the output would be 6, since f(2,3) + f(2,4) +
+# f(3,4) = 1 + 2 + 3 = 6
+
+sub f($a, $b) {
+ return [+] ($a +^ $b).base(2).comb;
+}
+
+sub pair-sum(@a) {
+ return [+] @a.combinations(2).map({ f($_[0], $_[1]); });
+}
+
+say pair-sum([2, 3]);
+say pair-sum([2, 3, 4]);
+say pair-sum([2, 3, 4, 5]);
+say pair-sum([2, 3, 4, 5, 6]);
+say pair-sum([2, 3, 4, 5, 6, 7]);
+say pair-sum([2, 3, 4, 5, 6, 7, 8]);
+say pair-sum([2, 3, 4, 5, 6, 7, 8, 9]);
+say pair-sum([2, 3, 4, 5, 6, 7, 8, 9, 10]);
+
+# Note: This sequence is unknown in the online encyclopedia of integer
+# sequences (OEIS). I'm surprised it's not in there. Is this pair-sum function
+# somehow a well know function? If it isn't I think it's a good idea to add it
+# to the OEIS.