aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2022-02-10 07:42:20 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2022-02-10 07:42:20 +0800
commit4aa638fcef37d8c29930d8d07157b6f14beb862a (patch)
treea4fd3969bcb0e19b8ba52a8e8dd21e7e50631808
parent2cdc1a87c35c4ff246c4d35e35c136fa987f9607 (diff)
downloadperlweeklychallenge-club-4aa638fcef37d8c29930d8d07157b6f14beb862a.tar.gz
perlweeklychallenge-club-4aa638fcef37d8c29930d8d07157b6f14beb862a.tar.bz2
perlweeklychallenge-club-4aa638fcef37d8c29930d8d07157b6f14beb862a.zip
Week 151 Task 2
-rw-r--r--challenge-151/cheok-yin-fung/perl/ch-2.pl21
1 files changed, 5 insertions, 16 deletions
diff --git a/challenge-151/cheok-yin-fung/perl/ch-2.pl b/challenge-151/cheok-yin-fung/perl/ch-2.pl
index df522b3397..2ae7dcef22 100644
--- a/challenge-151/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-151/cheok-yin-fung/perl/ch-2.pl
@@ -38,23 +38,13 @@ sub largest_revenue {
my @house = @_;
my @cand;
my $num_of_houses = scalar @house;
- return max @house if $num_of_houses <= 2;
+ return $house[0] if $num_of_houses <= 2;
push @cand, Chain->new(
0,
$house[0],
[$house[0]]
);
- push @cand, Chain->new(
- 2,
- $house[0]+$house[2],
- [$house[0], $house[2]]
- );
- push @cand, Chain->new(
- 1,
- $house[1],
- [$house[1]]
- );
- my $cur_ind = 3;
+ my $cur_ind = 2;
while ($cur_ind < $num_of_houses) {
for (@cand) {
if ($cur_ind - $_->last_ind == 2) {
@@ -85,16 +75,15 @@ sub largest_revenue {
}
$cur_ind++;
}
-
return max (map {$_->sum} @cand);
}
use Test::More tests => 6;
-ok largest_revenue( (1, 8, 9, 4, 2, 7, 6, 5, 3) ) == 24;
+ok largest_revenue( (1, 8, 9, 4, 2, 7, 6, 5, 3) ) == 22;
ok largest_revenue( (4, 2, 3, 6, 5, 3) ) == 13;
-ok largest_revenue( (2, 8, 5) ) == 8;
+ok largest_revenue( (2, 8, 5) ) == 7;
ok largest_revenue( (2, 6, 5) ) == 7;
-ok largest_revenue( (3, 8) ) == 8;
+ok largest_revenue( (3, 8) ) == 3;
ok largest_revenue( (5, 2) ) == 5;