aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-210/zapwai/perl/ch-1.pl20
-rw-r--r--challenge-210/zapwai/perl/ch-2.pl25
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-210/zapwai/perl/ch-1.pl b/challenge-210/zapwai/perl/ch-1.pl
new file mode 100644
index 0000000000..1f03462196
--- /dev/null
+++ b/challenge-210/zapwai/perl/ch-1.pl
@@ -0,0 +1,20 @@
+use v5.30.0;
+my @int = (2, 3, 1);
+#my @int = (1,1,2,2, 3, 2);
+say "Input: \@int = (".join(", ",@int).")";
+my %freq;
+$freq{$_}++ for (@int);
+my $max;
+my $val;
+foreach my $key (keys %freq) {
+ my $sum = 0;
+ for (-1 .. 1) {
+ my $n = $key + $_;
+ $sum += $n*$freq{$n};
+ }
+ if ($max < $sum) {
+ $val = $key;
+ $max = $sum;
+ }
+}
+say "Output: $max (by removing $val)";
diff --git a/challenge-210/zapwai/perl/ch-2.pl b/challenge-210/zapwai/perl/ch-2.pl
new file mode 100644
index 0000000000..f43a9e9234
--- /dev/null
+++ b/challenge-210/zapwai/perl/ch-2.pl
@@ -0,0 +1,25 @@
+use v5.30.0;
+my @list = (2,3,-1);
+say "Input: \@list = (".join(", ",@list).")";
+for (1 .. @list) {
+ my $positive_value_preceding = 0;
+ last if ($#list == 0);
+ foreach my $i (0 .. $#list) {
+ if ($list[$i] < 0) {
+ if ($positive_value_preceding) {
+ my $bad_entry = $i;
+ $bad_entry-- if ( abs($list[$i - 1]) <= abs($list[$i]) );
+ if (abs($list[$i - 1]) == abs($list[$i])) {
+ splice @list, $i, 1;
+ $i--;
+ }
+ splice @list, $bad_entry, 1;
+ $positive_value_preceding = 0;
+ redo;
+ }
+ } else {
+ $positive_value_preceding = 1;
+ }
+ }
+}
+say "Output: (".join(", ",@list).")";