aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-18 09:09:15 +0100
committerGitHub <noreply@github.com>2022-10-18 09:09:15 +0100
commitf379ea9eabcb675797f124f29bacd787bb04b610 (patch)
tree0a670223cc309142621d3f4bb14232a1e7173a9e
parent044a9eff0b45b809903f26348a97d915aa5753f8 (diff)
parenteab280cf97a99b53750615c6d53659b12abdabdb (diff)
downloadperlweeklychallenge-club-f379ea9eabcb675797f124f29bacd787bb04b610.tar.gz
perlweeklychallenge-club-f379ea9eabcb675797f124f29bacd787bb04b610.tar.bz2
perlweeklychallenge-club-f379ea9eabcb675797f124f29bacd787bb04b610.zip
Merge pull request #6928 from drbaggy/master
Neater more compact solution..
-rw-r--r--challenge-187/james-smith/README.md11
-rw-r--r--challenge-187/james-smith/perl/ch-2.pl19
2 files changed, 22 insertions, 8 deletions
diff --git a/challenge-187/james-smith/README.md b/challenge-187/james-smith/README.md
index b1fbbf2150..6779522e79 100644
--- a/challenge-187/james-smith/README.md
+++ b/challenge-187/james-smith/README.md
@@ -92,21 +92,24 @@ This is a permutation challenge, so we loop through the values with 3 nested loo
By sorting we can make the assumption that `$a,$b,$c` are automatically sorted highest to lowest..
+This has two advantages:
+ 1. The results will always be sorted `$a>=$b>=$c`
+ 2. The 3 inequalities can reduce to a single inequality - all three are satisfied if `$b+$c > $a`, as this is the smallest sum of two numbers and the largest single number;
+ 3. The first solution we find will have the largest sum of `$a+$b+$c` so we don't need to keep track of the maximum solution - but return the first we can find...
+
Each time through we use shift to get the first element of each loop until there are no values left. Note in this case we use `while` for the outer two loops...
```perl
sub magical {
@_ = sort { $b <=> $a } @_;
- my @max = (0);
while(@_>2) {
my($a,$b,@c)=@_;
while(@c) {
- ($a+$b>$_) && ($b+$_>$a) && ($a+$_>$b) && ($a+$b+$_>$max[0]) && (@max=($a+$b+$_,$a,$b,$_)) for @c;
+ ( $b+$_ > $a ) && ( return $a,$b,$_ ) for @c;
$b = shift @c;
}
shift;
}
- shift @max;
- @max;
+ ();
}
```
diff --git a/challenge-187/james-smith/perl/ch-2.pl b/challenge-187/james-smith/perl/ch-2.pl
index e08218ad26..e7c46dd075 100644
--- a/challenge-187/james-smith/perl/ch-2.pl
+++ b/challenge-187/james-smith/perl/ch-2.pl
@@ -13,24 +13,35 @@ my @TESTS = (
[ [1,3,2], '' ],
[ [1,1,2,3], '' ],
[ [2,4,3], '4 3 2' ],
+ [ [1..10], '10 9 8' ],
+ [ [7,4,1], '' ],
+ [ [201,101,1], '' ],
+ [ [1,1,2,3,4,5,2,1,2,3,2,1,1,1,1,1], '5 4 3' ],
);
+for( 1..100 ) {
+ my @T = map { 1 + int rand 20 } 1 .. 3 + int rand 17;
+ my $r = join ' ', (sort {$b<=>$a} @T)[0,1,2];
+ push @TESTS, [ \@T, $r ]
+}
+
+
is( "@{[magical(@{$_->[0]})]}", $_->[1] ) foreach @TESTS;
done_testing();
sub magical {
@_ = sort { $b <=> $a } @_;
- my @max = (0);
while(@_>2) {
my($a,$b,@c)=@_;
while(@c) {
- ($a+$b>$_) && ($b+$_>$a) && ($a+$_>$b) && ($a+$b+$_>$max[0]) && (@max=($a+$b+$_,$a,$b,$_)) for @c;
+ ($b+$_>$a) && (return $a,$b,$_ ) for @c;
$b = shift @c;
}
shift;
}
- shift @max;
- @max;
+ ();
+ #shift @max;
+ #@max;
}