Created
January 10, 2013 13:27
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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