aboutsummaryrefslogtreecommitdiff
path: root/challenge-291
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-10-20 23:38:13 +0100
committerGitHub <noreply@github.com>2024-10-20 23:38:13 +0100
commita9c04811837fe3bf89ba2d28fa9cfa880bb7e254 (patch)
treec075204499e06b905f23b226b10c6e32c189e116 /challenge-291
parent18da86d1a589c11a6dc9414b62942dbfb3f2cada (diff)
parent0e804b69a367ecaa1c5b0426100a56862347ae15 (diff)
downloadperlweeklychallenge-club-a9c04811837fe3bf89ba2d28fa9cfa880bb7e254.tar.gz
perlweeklychallenge-club-a9c04811837fe3bf89ba2d28fa9cfa880bb7e254.tar.bz2
perlweeklychallenge-club-a9c04811837fe3bf89ba2d28fa9cfa880bb7e254.zip
Merge pull request #11049 from E7-87-83/newh
Week 291 Task 1
Diffstat (limited to 'challenge-291')
-rw-r--r--challenge-291/cheok-yin-fung/perl/ch-1.pl27
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-291/cheok-yin-fung/perl/ch-1.pl b/challenge-291/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..6607811d29
--- /dev/null
+++ b/challenge-291/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,27 @@
+# The Weekly Challenge 291
+# Task 1 Middle Index
+use v5.30;
+use warnings;
+use List::Util qw/sum/;
+
+sub mi {
+ my @arr = $_[0]->@*;
+ my $rsum = (sum @arr) - $arr[0];
+ my $lsum = 0;
+ my $i = 0;
+ while ($lsum != $rsum) {
+ $i++;
+ last if $i > $#arr;
+ $lsum += $arr[$i-1];
+ $rsum = $rsum - $arr[$i];
+ # say ($i," ",$lsum," ",$rsum," ", $lsum+$rsum+$arr[$i]);
+ }
+ return $i if $lsum == $rsum;
+ return -1;
+}
+
+use Test::More tests=>4;
+ok mi([2,3,-1,8,4]) == 3;
+ok mi([1,-1,4]) == 2;
+ok mi([2,5])==-1;
+ok mi([0,2,-1,-1])==0;