aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-03 00:54:21 +0100
committerGitHub <noreply@github.com>2023-04-03 00:54:21 +0100
commitbd4513b60728b8b0c0ec90d2a1db2fc7d9d3703d (patch)
tree1ee70f8271430f42b2a756c30de98d8c6924a34e
parent67176bcc24144368ad6894df01fe8ecd9f26a7b5 (diff)
parentf0fd3ff6ffed2b05eed28bccdd9a4654b4d15ff6 (diff)
downloadperlweeklychallenge-club-bd4513b60728b8b0c0ec90d2a1db2fc7d9d3703d.tar.gz
perlweeklychallenge-club-bd4513b60728b8b0c0ec90d2a1db2fc7d9d3703d.tar.bz2
perlweeklychallenge-club-bd4513b60728b8b0c0ec90d2a1db2fc7d9d3703d.zip
Merge pull request #7829 from E7-87-83/newt
Week 210 Task 2
-rw-r--r--challenge-210/cheok-yin-fung/perl/ch-2.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-210/cheok-yin-fung/perl/ch-2.pl b/challenge-210/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..34ef750338
--- /dev/null
+++ b/challenge-210/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,40 @@
+# The Weekly Challenge 210
+# Task 2 Number Collision
+use v5.30.0;
+use warnings;
+sub nc {
+ my @a = $_[0]->@*;
+ my $num_of_coll;
+ do {
+ my @remain = ();
+ my $last_ind = -1;
+ $num_of_coll = 0;
+ for my $i (1..$#a) {
+ if ($a[$i-1] >= 0 && $a[$i] <= 0) {
+ if ($a[$i-1] != 0 || $a[$i] != 0) {
+ push @remain, $a[$i-1] if abs($a[$i-1]) > abs($a[$i]);
+ push @remain, $a[$i] if abs($a[$i-1]) < abs($a[$i]);
+ $num_of_coll++;
+ $last_ind = $i;
+ last;
+ }
+ }
+ else {
+ push @remain, $a[$i-1];
+ $last_ind = $i-1;
+ }
+ }
+ push @remain, @a[$last_ind+1..$#a];
+ @a = @remain;
+ } until ($num_of_coll == 0);
+ return [@a];
+}
+
+
+
+use Test::More tests=>4;
+use Test::Deep;
+cmp_deeply nc([2, 3, -1]), [2, 3];
+cmp_deeply nc([3, 2, -4]), [-4];
+cmp_deeply nc([1, -1]), [];
+cmp_deeply nc([-1, 1]), [-1, 1];