Skip to content

Instantly share code, notes, and snippets.

@peczenyj
Created January 10, 2013 13:27
Show Gist options
  • Select an option

  • Save peczenyj/4502025 to your computer and use it in GitHub Desktop.

Select an option

Save peczenyj/4502025 to your computer and use it in GitHub Desktop.
Codility task Equi, solution in perl with score 100 and O(n), see http://codility.com/demo/take-sample-test/
use strict;
use warnings;
use v5.10;
use List::Util qw(sum first);
sub equi {
my (@A) = @_;
my $right = sum(@A);
my $size = scalar @A;
my $p = first {
state $pivot = 0;
state $left += $pivot;
$pivot = $A[$_];
$right -= $pivot;
$left == $right
} 0 .. $size - 1;
$p // -1
}
use Test::More tests => 4;
is(equi(-7,1,5,2,-4,3,0),3,"example");
is(equi(1..5),-1,"simple");
is(equi(),-1,"trivial");
is(equi(555),0,"single");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment