Skip to content

Commit

Permalink
Add Perl solution to challenge 059
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloscustodio committed Apr 14, 2022
1 parent 7b2883e commit 555f8b5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions challenge-059/paulo-custodio/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
perl ../../challenge-001/paulo-custodio/test.pl
1 change: 1 addition & 0 deletions challenge-059/paulo-custodio/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Solution by Paulo Custodio
56 changes: 56 additions & 0 deletions challenge-059/paulo-custodio/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -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;
};
}
43 changes: 43 additions & 0 deletions challenge-059/paulo-custodio/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -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/;
}
5 changes: 5 additions & 0 deletions challenge-059/paulo-custodio/t/test-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- setup:
cleanup:
args: 3 1 4 3 2 5 2
input:
output: 1 -> 2 -> 2 -> 4 -> 3 -> 5
5 changes: 5 additions & 0 deletions challenge-059/paulo-custodio/t/test-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- setup:
cleanup:
args: 2 3 4
input:
output: 6

0 comments on commit 555f8b5

Please sign in to comment.