forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b2883e
commit 555f8b5
Showing
6 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
perl ../../challenge-001/paulo-custodio/test.pl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Solution by Paulo Custodio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- setup: | ||
cleanup: | ||
args: 2 3 4 | ||
input: | ||
output: 6 |