diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-10-20 11:52:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-20 11:52:17 +0100 |
| commit | 56784c5b048cd172c73f1d59b527e6306252dadd (patch) | |
| tree | e6a4f5562cba649533eb08a1576fdca97fe343d9 | |
| parent | 62a91c21ad7effc54629c295bb77d5427e0813a2 (diff) | |
| parent | c0a20a36303cb37be60cd91df6c0308204a54894 (diff) | |
| download | perlweeklychallenge-club-56784c5b048cd172c73f1d59b527e6306252dadd.tar.gz perlweeklychallenge-club-56784c5b048cd172c73f1d59b527e6306252dadd.tar.bz2 perlweeklychallenge-club-56784c5b048cd172c73f1d59b527e6306252dadd.zip | |
Merge pull request #11047 from jeanluc2020/jeanluc2020-291
Add solution 291 (only part 1)
| -rw-r--r-- | challenge-291/jeanluc2020/blog-1.txt | 1 | ||||
| -rwxr-xr-x | challenge-291/jeanluc2020/perl/ch-1.pl | 73 |
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-291/jeanluc2020/blog-1.txt b/challenge-291/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..c8673b183f --- /dev/null +++ b/challenge-291/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-291-1.html diff --git a/challenge-291/jeanluc2020/perl/ch-1.pl b/challenge-291/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..de4339ea46 --- /dev/null +++ b/challenge-291/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-291/#TASK1 +# +# Task 1: Middle Index +# ==================== +# +# You are given an array of integers, @ints. +# +# Write a script to find the leftmost middle index (MI) i.e. the smallest +# amongst all the possible ones. +# +## A middle index is an index where ints[0] + ints[1] + … + ints[MI-1] == +## ints[MI+1] + ints[MI+2] + … + ints[ints.length-1]. +# +# If MI == 0, the left side sum is considered to be 0. Similarly, +# if MI == ints.length - 1, the right side sum is considered to be 0. +# +# Return the leftmost MI that satisfies the condition, or -1 if there is no +# such index. +# +## Example 1 +## +## Input: @ints = (2, 3, -1, 8, 4) +## Output: 3 +## +## The sum of the numbers before index 3 is: 2 + 3 + -1 = 4 +## The sum of the numbers after index 3 is: 4 = 4 +# +## Example 2 +## +## Input: @ints = (1, -1, 4) +## Output: 2 +## +## The sum of the numbers before index 2 is: 1 + -1 = 0 +## The sum of the numbers after index 2 is: 0 +# +## Example 3 +## +## Input: @ints = (2, 5) +## Output: -1 +## +## There is no valid MI. +# +############################################################ +## +## discussion +## +############################################################ +# +# Starting at the beginning, we check the left and right side sum +# at each step. If they match, we return the index. If in the +# end, we didn't find the index, we return -1. + +use strict; +use warnings; +use List::Util qw(sum); + +middle_index(2, 3, -1, 8, 4); +middle_index(1, -1, 4); +middle_index(2, 5); + +sub middle_index { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + foreach my $i (0..$#ints) { + my $l = sum(@ints[0..$i-1]) // 0; + my $r = sum(@ints[$i+1..$#ints]) // 0; + if($r == $l) { + return print "Output: $i\n"; + } + } + print "Output: -1\n"; +} |
