aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-04 16:12:42 +0100
committerGitHub <noreply@github.com>2020-05-04 16:12:42 +0100
commit434beb1c5c3c10753a1ed7fb25366a973e661490 (patch)
treef61d0fc44fc5e85b700b5c08bcf8ebba551451d3
parentda768eab9c95cffb1b36835aee1c692d0f546f24 (diff)
parentb6e77a26d682ebad2ca6760c00043b664fb56988 (diff)
downloadperlweeklychallenge-club-434beb1c5c3c10753a1ed7fb25366a973e661490.tar.gz
perlweeklychallenge-club-434beb1c5c3c10753a1ed7fb25366a973e661490.tar.bz2
perlweeklychallenge-club-434beb1c5c3c10753a1ed7fb25366a973e661490.zip
Merge pull request #1671 from fluca1978/pwc59
Pwc59
-rw-r--r--challenge-059/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-059/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-059/luca-ferrari/raku/ch-1.p628
-rw-r--r--challenge-059/luca-ferrari/raku/ch-2.p656
4 files changed, 86 insertions, 0 deletions
diff --git a/challenge-059/luca-ferrari/blog-1.txt b/challenge-059/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..67820d6dca
--- /dev/null
+++ b/challenge-059/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/05/04/PerlWeeklyChallenge59.html#task1
diff --git a/challenge-059/luca-ferrari/blog-2.txt b/challenge-059/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..5bc81c54ac
--- /dev/null
+++ b/challenge-059/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2020/05/04/PerlWeeklyChallenge59.html#task2
diff --git a/challenge-059/luca-ferrari/raku/ch-1.p6 b/challenge-059/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..ad73ef8831
--- /dev/null
+++ b/challenge-059/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,28 @@
+#!env raku
+
+
+# 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.
+
+
+# example of invocation
+# % raku ch-1.p6 --k=4
+# Index 4 makes 1 4 3 2 5 2 to become 1 3 2 2 4 5
+
+sub MAIN( Int:D :$k = 3 ) {
+
+ my @L = 1, 4, 3, 2, 5, 2 ;
+ my @l = | @L.grep( * < $k ), | @L.grep( * >= $k );
+ say "Index $k makes { @L } to become { @l }";
+}
diff --git a/challenge-059/luca-ferrari/raku/ch-2.p6 b/challenge-059/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..6e9d5b7ca1
--- /dev/null
+++ b/challenge-059/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,56 @@
+#!env raku
+
+# 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( Int:D $a, Int:D $b ) {
+ my $different-bits = 0;
+
+ my @a-bits = $a.base( 2 ).Str.comb.reverse;
+ my @b-bits = $b.base( 2 ).Str.comb.reverse;
+
+ # find the longest number
+ my $max-length = max( @a-bits.elems, @b-bits.elems );
+ # do the padding with zeros (to the end, the arra)
+ @a-bits.push: 0 for 0 .. ( $max-length - @a-bits.elems );
+ @b-bits.push: 0 for 0 .. ( $max-length - @b-bits.elems );
+
+
+ # compute the difference
+ for 0 ..^ @a-bits.elems {
+ $different-bits += 1 if ( @a-bits[ $_ ] != @b-bits[ $_ ] );
+ }
+
+ $different-bits;
+}
+
+################
+my $sum = 0;
+for 0 ..^ @*ARGS.elems -> $first {
+ for $first + 1 ..^ @*ARGS.elems -> $second {
+ $sum += f( @*ARGS[ $first ].Int, @*ARGS[ $second ].Int );
+ }
+}
+say "Sum is $sum";