aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2023-03-31 21:29:35 +0800
committerCY Fung <fungcheokyin@gmail.com>2023-03-31 21:29:35 +0800
commitf0fd3ff6ffed2b05eed28bccdd9a4654b4d15ff6 (patch)
treebee91f875dc75682ab454f2a2d4847651d56514f
parent9832763638186750dcf385a9db491334db89a682 (diff)
downloadperlweeklychallenge-club-f0fd3ff6ffed2b05eed28bccdd9a4654b4d15ff6.tar.gz
perlweeklychallenge-club-f0fd3ff6ffed2b05eed28bccdd9a4654b4d15ff6.tar.bz2
perlweeklychallenge-club-f0fd3ff6ffed2b05eed28bccdd9a4654b4d15ff6.zip
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];