diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-19 18:26:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-19 18:26:53 +0100 |
| commit | e02c3d1595e3a5dad8945db3bde18e058db93ada (patch) | |
| tree | f6e5d5e0cb5d2a75a49b6b007155b8f60ac5bccb | |
| parent | a81e9533ebaf78ccd6fbbac810b51e15afe115e7 (diff) | |
| parent | ba7375711d24663568eaae8e95112b466a99f196 (diff) | |
| download | perlweeklychallenge-club-e02c3d1595e3a5dad8945db3bde18e058db93ada.tar.gz perlweeklychallenge-club-e02c3d1595e3a5dad8945db3bde18e058db93ada.tar.bz2 perlweeklychallenge-club-e02c3d1595e3a5dad8945db3bde18e058db93ada.zip | |
Merge pull request #12700 from andemark/challenge-339
Bug fix and more tests
| -rw-r--r-- | challenge-339/mark-anderson/raku/ch-1.raku | 102 | ||||
| -rw-r--r-- | challenge-339/mark-anderson/raku/ch-2.raku | 2 |
2 files changed, 70 insertions, 34 deletions
diff --git a/challenge-339/mark-anderson/raku/ch-1.raku b/challenge-339/mark-anderson/raku/ch-1.raku index 89d5b34d16..136f32289d 100644 --- a/challenge-339/mark-anderson/raku/ch-1.raku +++ b/challenge-339/mark-anderson/raku/ch-1.raku @@ -1,65 +1,101 @@ #!/usr/bin/env raku use Test; -is max-diff(< 5 9 3 4 6 >), 42; -is max-diff(< 1 -2 3 -4 >), 10; -is max-diff(< -3 -1 -2 -4 >), 10; -is max-diff(< 10 2 0 5 1 >), 50; -is max-diff(< 7 8 9 10 10 >), 44; +is max-diff(< 5 9 3 4 6 >), 42, "Example 1"; +is max-diff(< 1 -2 3 -4 >), 10, "Example 2"; +is max-diff(< -3 -1 -2 -4 >), 10, "Example 3"; +is max-diff(< 10 2 0 5 1 >), 50, "Example 4"; +is max-diff(< 7 8 9 10 10 >), 44, "Example 5"; + +# +# I think the tests below cover each possibility but I'm not positive. +# + +is max-diff(< 0 0 0 0 0 0 3 >), 0, "[ 0 0 0 0 0 0 3 ] -> (0 * *) - (0 * *)"; +is max-diff(< 3 4 5 6 7 8 9 >), 60, "[ 3 4 5 6 7 8 9 ] -> (8 * 9) - (3 * 4)"; +is max-diff(< -9 -8 -7 -6 -5 -4 -3 >), 60, "[ -9 -8 -7 -6 -5 -4 -3 ] -> (-9 * -8) - (-4 * -3)"; +is max-diff(< -13 -9 -6 0 7 8 10 >), 186, "[ -13 -9 -6 0 7 8 10 ] -> (7 * 8) - (-13 * 10)"; +is max-diff(< -2 -1 3 4 8 14 15 >), 226, "[ -2 -1 3 4 8 14 15 ] -> (14 * 15) - (-2 * 8)"; +is max-diff(< -12 -11 -8 0 0 3 9 >), 204, "[ -12 -11 -8 0 0 3 9 ] -> (-12 * -11) - (-8 * 9)"; +is max-diff(< -12 -8 -5 -1 0 2 14 >), 208, "[ -12 -8 -5 -1 0 2 14 ] -> (-8 * -5) - (-12 * 14)"; +is max-diff(< -14 -2 0 0 0 3 10 >), 140, "[ -14 -2 0 0 0 3 10 ] -> (0 * *) - (-14 * 10)"; +is max-diff(< 0 0 0 2 6 8 14 >), 112, "[ 0 0 0 2 6 8 14 ] -> (8 * 14) - (0 * *)"; +is max-diff(< -14 -8 -6 -2 0 0 0 >), 112, "[ -14 -8 -6 -2 0 0 0 ] -> (-14 * -8) - (0 * *)"; +is max-diff(< -15 -12 2 4 5 6 7 >), 172, "[ -15 -12 2 4 5 6 7 ] -> (-15 * -12) - (2 * 4)"; +is max-diff(< -6 -6 -5 -3 -1 12 15 >), 177, "[ -6 -6 -5 -3 -1 12 15 ] -> (12 * 15) - (-3 * -1)"; +is max-diff(< -9 -1 1 8 >), 71, "[ -9 -1 1 8 ] -> (-1 * 1) - (-9 * 8)"; sub max-diff(@a) { my %c := { -1 => [], 0 => [], 1 => [] } @a.classify(*.sign, :into(%c)); + + if %c<0> >= @a.end + { + return 0 + } + %c<-1> = %c<-1>.sort.Array; %c<1> = %c<1> .sort.Array; - if %c<0> >= @a.end { return 0 } - if none(%c<-1 0>) { return ([*] %c<1> .tail(2)) - ([*] %c<1> .head(2)) } - if none(%c<1 0>) { return ([*] %c<-1>.head(2)) - ([*] %c<-1>.tail(2)) } + if none %c<-1 0> + { + return ([*] %c<1> .tail(2)) - ([*] %c<1> .head(2)) + } + if none %c<1 0> + { + return ([*] %c<-1>.head(2)) - ([*] %c<-1>.tail(2)) + } + return .max given gather { - if all(%c<1> >= 3, %c<-1> >= 1) + if all %c<1> >= 3, %c<-1> { - take ([*] %c<1>[*-2,*-1]) - ([*] %c<-1>[0], %c<1>[*-3]); - take ([*] %c<1>[*-3,*-2]) - ([*] %c<-1>[0], %c<1>[*-1]) + take ([*] %c<1>[*-3,*-2]) - ([*] %c<-1>[0], %c<1>[*-1]); + take ([*] %c<1>[*-2,*-1]) - ([*] %c<-1>[0], %c<1>[*-3]) } - if all(%c<-1> >= 3, %c<1> >= 1) + if all %c<-1> >= 3, %c<1> { take ([*] %c<-1>[0,1]) - ([*] %c<-1>[2], %c<1>[*-1]); take ([*] %c<-1>[1,2]) - ([*] %c<-1>[0], %c<1>[*-1]) } - - if all(all(%c<-1 1>) >= 2, none(%c<0>)) - { - take (%c<-1>.tail * %c<1>.head) - (%c<-1>.head * %c<1>.tail) - } - if all(%c<-1 0 1>) >= 1 + if %c<0> { - take -([*] %c<-1>.head, %c<1>.tail) - } + if all %c<-1 1> + { + take -([*] %c<-1>.head, %c<1>.tail) + } - if all(%c<1> >= 2, %c<0> >= 1) - { - take [*] %c<1>.tail(2) - } + if %c<1> >= 2 + { + take [*] %c<1>.tail(2); + } - if all(%c<-1> >= 2, %c<0> >= 1) - { - take [*] %c<-1>.head(2) + if %c<-1> >= 2 + { + take [*] %c<-1>.head(2) + } } - if %c<-1> == 2 + else { - take ([*] %c<-1>.head(2)) - ([*] %c<1>.head(2)) - } + if %c<-1> == 2 + { + take ([*] %c<-1>.head(2)) - ([*] %c<1>.head(2)); + } - if %c<1> == 2 - { - take ([*] %c<1>.tail(2)) - ([*] %c<-1>.tail(2)) + if %c<1> == 2 + { + take ([*] %c<1>.tail(2)) - ([*] %c<-1>.tail(2)) + } + + if all(%c<-1 1>) == 2 + { + take ([*] %c<-1>.tail, %c<1>.head) - ([*] %c<-1>.head, %c<1>.tail) + } } } } diff --git a/challenge-339/mark-anderson/raku/ch-2.raku b/challenge-339/mark-anderson/raku/ch-2.raku index 4cfc4d54e6..f64daf48b4 100644 --- a/challenge-339/mark-anderson/raku/ch-2.raku +++ b/challenge-339/mark-anderson/raku/ch-2.raku @@ -7,7 +7,7 @@ is peak-point(< 3 -4 2 5 -6 1 >), 6; is peak-point(< -1 -2 -3 -4 >), 0; is peak-point(< -10 15 5 >), 10; -sub peak-point(+@gain) +sub peak-point(@gain) { max [\+] 0, |@gain } |
