aboutsummaryrefslogtreecommitdiff
path: root/challenge-018
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-01-25 20:07:40 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-01-26 00:06:54 +0000
commit3fa58628535d4041c7cc648c005080ca88f18c18 (patch)
tree336fe3cc14f518f05e871ab974cc86a09a2fd8f6 /challenge-018
parent3d3900a2f0f69c54a34683e4e1b5da007b4af9d9 (diff)
downloadperlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.tar.gz
perlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.tar.bz2
perlweeklychallenge-club-3fa58628535d4041c7cc648c005080ca88f18c18.zip
Replace tabs by spaces so that indentation looks correct
Diffstat (limited to 'challenge-018')
-rw-r--r--challenge-018/paulo-custodio/perl/ch-1.pl46
-rw-r--r--challenge-018/paulo-custodio/perl/ch-2.pl100
-rw-r--r--challenge-018/paulo-custodio/test.pl8
3 files changed, 77 insertions, 77 deletions
diff --git a/challenge-018/paulo-custodio/perl/ch-1.pl b/challenge-018/paulo-custodio/perl/ch-1.pl
index 147061054d..a7c7d65c34 100644
--- a/challenge-018/paulo-custodio/perl/ch-1.pl
+++ b/challenge-018/paulo-custodio/perl/ch-1.pl
@@ -18,27 +18,27 @@ say "(", join(", ", map {$_=qq("$_")} longest_substr(@ARGV)), ")";
sub longest_substr {
- my(@strs) = @_;
- my $longest_len = -1;
- my %longest;
-
- for my $str (@strs) { # each string
- for my $s (0 .. length($str)-1) { # each starting point
- for my $len (reverse 1 .. length($str)-$s) { # each substring of str
- next if $longest_len > $len; # prune search
- my $substr = substr($str, $s, $len);
- next if $longest{$str}; # prune search
- if (all {/$substr/} @strs) { # matches all
- if ($longest_len == $len) {
- $longest{$substr}=1;
- }
- else { # $longest_len < $len
- %longest = ($substr=>1);
- $longest_len = $len;
- }
- }
- }
- }
- }
- return sort keys %longest;
+ my(@strs) = @_;
+ my $longest_len = -1;
+ my %longest;
+
+ for my $str (@strs) { # each string
+ for my $s (0 .. length($str)-1) { # each starting point
+ for my $len (reverse 1 .. length($str)-$s) { # each substring of str
+ next if $longest_len > $len; # prune search
+ my $substr = substr($str, $s, $len);
+ next if $longest{$str}; # prune search
+ if (all {/$substr/} @strs) { # matches all
+ if ($longest_len == $len) {
+ $longest{$substr}=1;
+ }
+ else { # $longest_len < $len
+ %longest = ($substr=>1);
+ $longest_len = $len;
+ }
+ }
+ }
+ }
+ }
+ return sort keys %longest;
}
diff --git a/challenge-018/paulo-custodio/perl/ch-2.pl b/challenge-018/paulo-custodio/perl/ch-2.pl
index 4741528a33..6ca55f29e1 100644
--- a/challenge-018/paulo-custodio/perl/ch-2.pl
+++ b/challenge-018/paulo-custodio/perl/ch-2.pl
@@ -8,11 +8,11 @@
# element with high priority is served before an element with low priority.
# Please check this wiki page for more informations. It should serve the
# following operations:
-#
+#
# is_empty: check whether the queue has no elements.
# insert_with_priority: add an element to the queue with an associated priority.
# pull_highest_priority_element: remove the element from the queue that has the
-# highest priority, and return it. If two elements have the same priority,
+# highest priority, and return it. If two elements have the same priority,
# then return element added first.
use strict;
@@ -20,54 +20,54 @@ use warnings;
use 5.030;
# implement a Priority-queue as an ordered list of [priority, list of elements]
-{
- package PQueue;
-
- sub new {
- my($class) = @_;
- return bless [], $class;
- }
-
- sub is_empty {
- my($self) = @_;
- return @$self == 0;
- }
-
- sub insert {
- my($self, $pri, $elem) = @_;
- if ($self->is_empty) { # special case: empty
- push @{$self}, [$pri, [$elem]];
- }
- elsif ($pri < $self->[0][0]) { # special case: lowest
- unshift @{$self}, [$pri, [$elem]];
- }
- elsif ($pri > $self->[-1][0]) { # special case: highest
- push @{$self}, [$pri, [$elem]];
- }
- else {
- for my $i (0 .. $#{$self}) { # traverse list, search for position
- if ($self->[$i][0] == $pri) { # same priority
- push @{$self->[$i][1]}, $elem;
- return;
- }
- elsif ($self->[$i][0] > $pri) { # higher
- splice @$self, $i, 0, [$pri, [$elem]];
- return;
- }
- }
- die "not reached";
- }
- }
-
- sub pull {
- my($self) = @_;
- return if $self->is_empty;
- my $elem = shift @{$self->[-1][1]};
- if (@{$self->[-1][1]}==0) { # bucket now empty
- pop @$self;
- }
- return $elem;
- }
+{
+ package PQueue;
+
+ sub new {
+ my($class) = @_;
+ return bless [], $class;
+ }
+
+ sub is_empty {
+ my($self) = @_;
+ return @$self == 0;
+ }
+
+ sub insert {
+ my($self, $pri, $elem) = @_;
+ if ($self->is_empty) { # special case: empty
+ push @{$self}, [$pri, [$elem]];
+ }
+ elsif ($pri < $self->[0][0]) { # special case: lowest
+ unshift @{$self}, [$pri, [$elem]];
+ }
+ elsif ($pri > $self->[-1][0]) { # special case: highest
+ push @{$self}, [$pri, [$elem]];
+ }
+ else {
+ for my $i (0 .. $#{$self}) { # traverse list, search for position
+ if ($self->[$i][0] == $pri) { # same priority
+ push @{$self->[$i][1]}, $elem;
+ return;
+ }
+ elsif ($self->[$i][0] > $pri) { # higher
+ splice @$self, $i, 0, [$pri, [$elem]];
+ return;
+ }
+ }
+ die "not reached";
+ }
+ }
+
+ sub pull {
+ my($self) = @_;
+ return if $self->is_empty;
+ my $elem = shift @{$self->[-1][1]};
+ if (@{$self->[-1][1]}==0) { # bucket now empty
+ pop @$self;
+ }
+ return $elem;
+ }
}
use Test::More;
diff --git a/challenge-018/paulo-custodio/test.pl b/challenge-018/paulo-custodio/test.pl
index 9eff0e95ef..fee445405b 100644
--- a/challenge-018/paulo-custodio/test.pl
+++ b/challenge-018/paulo-custodio/test.pl
@@ -18,8 +18,8 @@ ok 0==system("perl perl/ch-2.pl");
done_testing;
sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
}