Perl
#!/usr/bin/perl use strict; use warnings; use experimental qw{ signatures }; use List::Util qw{ sum }; sub equal_sides ($arr) { my ($left, $current, $right) = (0, 0, sum(@$arr[1 .. $#$arr])); until ($current > $#$arr || $left == $right) { $left += $arr->[$current]; $right -= $arr->[++$current] // 0; } return $current > $#$arr ? -1 : $current } use Test::More tests => 4; my @tests = ([1, 2, 3, 4, 3, 2, 1], 3, [1, 100, 50, -51, 1, 1], 1, [20, 10, 30, 10, 10, 15, 35], 3, [-8505, -5130, 1926, -9026], -1); while (my ($arr, $result) = splice @tests, 0, 2) { is equal_sides($arr), $result; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Perl