aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Lied <boblied+github@gmail.com>2023-09-20 07:50:08 -0500
committerBob Lied <boblied+github@gmail.com>2023-09-20 07:50:08 -0500
commit623c745cd9a47e34f77683e505601e87b6232cb8 (patch)
tree818857bda5dd585cdc953133ecf99a7c7c1feef0
parentd5c84ed05f7d37a00dbb6a9b48dd598511e9dc50 (diff)
downloadperlweeklychallenge-club-623c745cd9a47e34f77683e505601e87b6232cb8.tar.gz
perlweeklychallenge-club-623c745cd9a47e34f77683e505601e87b6232cb8.tar.bz2
perlweeklychallenge-club-623c745cd9a47e34f77683e505601e87b6232cb8.zip
Refinements and blog reference
-rw-r--r--challenge-235/bob-lied/blog.txt1
-rw-r--r--challenge-235/bob-lied/perl/ch-1.pl89
-rw-r--r--challenge-235/bob-lied/perl/ch-2.pl20
3 files changed, 86 insertions, 24 deletions
diff --git a/challenge-235/bob-lied/blog.txt b/challenge-235/bob-lied/blog.txt
new file mode 100644
index 0000000000..db7e36a16c
--- /dev/null
+++ b/challenge-235/bob-lied/blog.txt
@@ -0,0 +1 @@
+https://dev.to/boblied/pwc-235-steppin-in-a-slide-zone-la6
diff --git a/challenge-235/bob-lied/perl/ch-1.pl b/challenge-235/bob-lied/perl/ch-1.pl
index 06ce8fe5cf..76a280fae6 100644
--- a/challenge-235/bob-lied/perl/ch-1.pl
+++ b/challenge-235/bob-lied/perl/ch-1.pl
@@ -27,9 +27,73 @@ my $DoTest = 0;
GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
exit(!runTest()) if $DoTest;
+sub removeOne(@ints)
+{
+ use List::Util qw/min/;
+
+ return true if @ints < 3;
+
+ my $rmvCount = 0;
+
+ # Walk forward until we hit a descending step.
+ for ( my $i = 0 ; $i < $#ints && $rmvCount < 2 ; $i++ )
+ {
+ next if $ints[$i+1] >= $ints[$i];
+
+ # How far backward would we have to go to get back in order?
+ my $back = 0;
+ for ( my $j = $i ; $j >= 0 && $ints[$j] > $ints[$i+1] && $back < 3; $j-- )
+ {
+ $back++;
+ }
+
+ # How far ahead would we have to go to get back in order?
+ my $ahead = 0;
+ for ( my $j = $i+1; $j <= $#ints && $ints[$j] <= $ints[$i] && $ahead < 3; $j++ )
+ {
+ $ahead++;
+ }
+ $rmvCount += min($back, $ahead);
+
+ }
+ return $rmvCount < 2;
+}
+
+sub runTest
+{
+ use Test2::V0;
+ no warnings "experimental::builtin";
+
+ is( removeOne(0,2,9,4,6), true, "Example 1");
+ is( removeOne(5,1,3,2 ), false, "Example 2");
+ is( removeOne(2,2,3 ), true, "Example 3");
+ is( removeOne(10,1,2,3 ), true, "First element goes");
+ is( removeOne(10,11,1 ), true, "Last element goes");
+ is( removeOne(10,20,30,24,25,40), true, "One high peak");
+ is( removeOne(10,20,30,18,25,40), false, "One high, one low");
+ is( removeOne(1,2,5,3,4,6,3,4), false, "Multiple disorders");
+ is( removeOne(99, 1000, 1..999, 2000), false, "Long failure");
+
+ # is( ro_A(0,2.9,4,6), true, "Example 1");
+ # is( ro_A(5,1,3,2 ), false, "Example 2");
+ # is( ro_A(2,2,3 ), true, "Example 3");
+
+ # is( ro_B(0,2.9,4,6), true, "Example 1");
+ # is( ro_B(5,1,3,2 ), false, "Example 2");
+ # is( ro_B(2,2,3 ), true, "Example 3");
+
+ # is( ro_C(0,2.9,4,6), true, "Example 1");
+ # is( ro_C(5,1,3,2 ), false, "Example 2");
+ # is( ro_C(2,2,3 ), true, "Example 3");
+
+ done_testing;
+}
+
+# Things that don't work.
+
# Move down the list in pairs and count the number of times that
# we find a decreasing pair. Stop when we hit the second one.
-sub removeOne(@ints)
+sub ro_D(@ints)
{
my $rmvCount = 0;
for ( my $i = 0; $i < $#ints && $rmvCount < 2 ; $i++ )
@@ -76,26 +140,3 @@ sub ro_C(@ints)
return $rmvOne;
}
-sub runTest
-{
- use Test2::V0;
- no warnings "experimental::builtin";
-
- is( removeOne(0,2.9,4,6), true, "Example 1");
- is( removeOne(5,1,3,2 ), false, "Example 2");
- is( removeOne(2,2,3 ), true, "Example 3");
-
- is( ro_A(0,2.9,4,6), true, "Example 1");
- is( ro_A(5,1,3,2 ), false, "Example 2");
- is( ro_A(2,2,3 ), true, "Example 3");
-
- is( ro_B(0,2.9,4,6), true, "Example 1");
- is( ro_B(5,1,3,2 ), false, "Example 2");
- is( ro_B(2,2,3 ), true, "Example 3");
-
- is( ro_C(0,2.9,4,6), true, "Example 1");
- is( ro_C(5,1,3,2 ), false, "Example 2");
- is( ro_C(2,2,3 ), true, "Example 3");
-
- done_testing;
-}
diff --git a/challenge-235/bob-lied/perl/ch-2.pl b/challenge-235/bob-lied/perl/ch-2.pl
index 1c2b6ac1b1..0a7b0febf7 100644
--- a/challenge-235/bob-lied/perl/ch-2.pl
+++ b/challenge-235/bob-lied/perl/ch-2.pl
@@ -46,6 +46,20 @@ sub dz_A(@ints)
return \@output;
}
+sub dz_B(@ints)
+{
+ for (my $i = 0 ; $i <= $#ints; $i++ )
+ {
+ if ( $ints[$i] == 0 )
+ {
+ # Insert a zero and advance i past it
+ splice(@ints, $i++, 0, 0);
+ pop @ints; # Maintain the length;
+ }
+ }
+ return \@ints;
+}
+
sub runTest
{
use Test2::V0;
@@ -62,5 +76,11 @@ sub runTest
is( dz_A(0), [0], "One Zero");
is( dz_A(2, 1, 0), [2, 1, 0], "Ends on a zero");
+ is( dz_B(1,0,2,3,0,4,5,0), [1,0,0,2,3,0,0,4], "Example 1");
+ is( dz_B(1,2,3 ), [1,2,3 ], "Example 2");
+ is( dz_B(0,3,0,4,5 ), [0,0,3,0,0 ], "Example 3");
+ is( dz_B(0), [0], "One Zero");
+ is( dz_B(2, 1, 0), [2, 1, 0], "Ends on a zero");
+
done_testing;
}