aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/paulo-custodio/Makefile2
-rw-r--r--challenge-059/paulo-custodio/README1
-rw-r--r--challenge-059/paulo-custodio/perl/ch-1.pl56
-rw-r--r--challenge-059/paulo-custodio/perl/ch-2.pl43
-rw-r--r--challenge-059/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-059/paulo-custodio/t/test-2.yaml5
6 files changed, 112 insertions, 0 deletions
diff --git a/challenge-059/paulo-custodio/Makefile b/challenge-059/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-059/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-059/paulo-custodio/README b/challenge-059/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-059/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-059/paulo-custodio/perl/ch-1.pl b/challenge-059/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..99eadcb5fe
--- /dev/null
+++ b/challenge-059/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+# Challenge 059
+#
+# TASK #1 › Linked List
+# Reviewed by Ryan Thompson
+# 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.
+
+use Modern::Perl;
+use HOP::Stream qw( list_to_stream iterator_to_stream head tail );
+
+my($k, @n) = @ARGV;
+my $in = list_to_stream(@n);
+my $out = iterator_to_stream(partition_it($k, $in));
+
+my @out;
+while ($out) {
+ my $head = head($out);
+ $out = tail($out);
+ push @out, $head;
+}
+say join(" -> ", @out);
+
+
+sub partition_it {
+ my($k, $in) = @_;
+ my @pending;
+ return sub {
+ while ($in) {
+ my $head = head($in);
+ $in = tail($in);
+ if ($head < $k) {
+ return $head;
+ }
+ else {
+ push @pending, $head;
+ }
+ }
+ while (@pending) {
+ my $head = shift @pending;
+ return $head;
+ }
+ return;
+ };
+}
diff --git a/challenge-059/paulo-custodio/perl/ch-2.pl b/challenge-059/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..33b6d7b856
--- /dev/null
+++ b/challenge-059/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+# Challenge 059
+#
+# TASK #2 › Bit Sum
+# Reviewed by Ryan Thompson
+# 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
+
+use Modern::Perl;
+use Math::Combinatorics 'combine';
+
+my @n = @ARGV;
+my $sum = 0;
+for my $combin (combine(2, @n)) {
+ $sum += f($combin->[0], $combin->[1]);
+}
+say $sum;
+
+sub f {
+ my($a, $b) = @_;
+ my $r = ($a+0) ^ ($b+0);
+ my $rt = sprintf("%b", $r);
+ return $rt =~ tr/1/1/;
+}
diff --git a/challenge-059/paulo-custodio/t/test-1.yaml b/challenge-059/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..b9c5799a36
--- /dev/null
+++ b/challenge-059/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 3 1 4 3 2 5 2
+ input:
+ output: 1 -> 2 -> 2 -> 4 -> 3 -> 5
diff --git a/challenge-059/paulo-custodio/t/test-2.yaml b/challenge-059/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..dad9937cc6
--- /dev/null
+++ b/challenge-059/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 2 3 4
+ input:
+ output: 6