diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-11 17:42:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-11 17:42:30 +0100 |
| commit | 0397988699aa9648910a4c328c9313435fcf8435 (patch) | |
| tree | c8ad09cbeb632e5a516def113c439b9e2e209bc4 | |
| parent | 635f81890b1d36fd472afbe94df2c7cb0a289d6e (diff) | |
| parent | 237c14495623ce369ec0239e34ae4e42b87688d4 (diff) | |
| download | perlweeklychallenge-club-0397988699aa9648910a4c328c9313435fcf8435.tar.gz perlweeklychallenge-club-0397988699aa9648910a4c328c9313435fcf8435.tar.bz2 perlweeklychallenge-club-0397988699aa9648910a4c328c9313435fcf8435.zip | |
Merge pull request #5913 from Firedrake/rogerbw-challenge-160
Solutions for challenge #160
18 files changed, 946 insertions, 0 deletions
diff --git a/challenge-160/roger-bell-west/javascript/ch-1.js b/challenge-160/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..18097af7c5 --- /dev/null +++ b/challenge-160/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,51 @@ +#! /usr/bin/node + +function fim (n0) { + let words=["zero","one","two","three","four", + "five","six","seven","eight","nine"] + let n=n0 + let p=[] + while (true) { + let s=words[n] + " is " + if (n == 4) { + s += "magic." + p.push(s) + break + } else { + n=words[n].length + s += words[n] + p.push(s) + } + } + return p.join(", ").replace(/^\w/,function (c) { + return c.toUpperCase() + }) +} + +if (fim(5) == "Five is four, four is magic.") { + process.stdout.write("Pass") +} else { + process.stdout.write("FAIL") +} +process.stdout.write(" "); + +if (fim(7) == "Seven is five, five is four, four is magic.") { + process.stdout.write("Pass") +} else { + process.stdout.write("FAIL") +} +process.stdout.write(" "); + +if (fim(6) == "Six is three, three is five, five is four, four is magic.") { + process.stdout.write("Pass") +} else { + process.stdout.write("FAIL") +} +process.stdout.write(" "); + +if (fim(4) == "Four is magic.") { + process.stdout.write("Pass") +} else { + process.stdout.write("FAIL") +} +process.stdout.write("\n"); diff --git a/challenge-160/roger-bell-west/javascript/ch-2.js b/challenge-160/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..40ad5412b6 --- /dev/null +++ b/challenge-160/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,35 @@ +#! /usr/bin/node + +function equilibriumindex (s) { + let sm=s.reduce((x,y) => x+y, 0) + let sa=0 + for (let [i,v] of s.entries()) { + sa += v + if (sa == sm) { + return i + } + sa += v + } + return -1 +} + +if (equilibriumindex([1,3,5,7,9]) == 3) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (equilibriumindex([1,2,3,4,5]) == -1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (equilibriumindex([2,4,2]) == 1) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-160/roger-bell-west/kotlin/ch-1.kt b/challenge-160/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..c7964f46d0 --- /dev/null +++ b/challenge-160/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,48 @@ +import kotlin.math.* + +fun fim(n0: Int): String { + val words=listOf("zero","one","two","three","four", + "five","six","seven","eight","nine") + var n=n0 + var p=ArrayList<String>() + while (true) { + var s = words[n] + " is " + if (n == 4) { + s += "magic." + p.add(s) + break + } else { + n = words[n].length + s += words[n] + p.add(s) + } + } + return p.joinToString(", ").replaceFirstChar(Char::titlecase) +} + +fun main() { + if (fim(5) == "Five is four, four is magic.") { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (fim(7) == "Seven is five, five is four, four is magic.") { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (fim(6) == "Six is three, three is five, five is four, four is magic.") { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (fim(4) == "Four is magic.") { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-160/roger-bell-west/kotlin/ch-2.kt b/challenge-160/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..34ed8e2667 --- /dev/null +++ b/challenge-160/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,35 @@ +import kotlin.math.* + +fun equilibriumindex(s: List<Int>): Int { + val sm = s.sum() + var sa = 0 + for ((i,v) in s.withIndex()) { + sa += v + if (sa == sm) { + return i + } + sa += v + } + return -1 +} + +fun main() { + if (equilibriumindex(listOf(1,3,5,7,9)) == 3) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (equilibriumindex(listOf(1,2,3,4,5)) == -1) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (equilibriumindex(listOf(2,4,2)) == 1) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-160/roger-bell-west/lua/ch-1.lua b/challenge-160/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..4c73ccc235 --- /dev/null +++ b/challenge-160/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,49 @@ +#! /usr/bin/lua + +function fim(n0) + words={"zero","one","two","three","four", + "five","six","seven","eight","nine"} + local n=n0 + local p={} + while true do + local s=words[n+1] .. " is " + if n==4 then + s = s .. "magic." + table.insert(p,s) + break + else + n = #(words[n+1]) + s = s .. words[n+1] + table.insert(p,s) + end + end + return string.gsub(table.concat(p,", "),"^(%a)",string.upper) +end + +if fim(5) == "Five is four, four is magic." then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if fim(7) == "Seven is five, five is four, four is magic." then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if fim(6) == "Six is three, three is five, five is four, four is magic." then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if fim(4) == "Four is magic." then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-160/roger-bell-west/lua/ch-2.lua b/challenge-160/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..00611ad38d --- /dev/null +++ b/challenge-160/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,38 @@ +#! /usr/bin/lua + +function equilibriumindex(s) + local sm=0 + for k,v in ipairs(s) do + sm = sm + v + end + local sa=0 + for k,v in ipairs(s) do + sa = sa + v + if sa == sm then + return k + end + sa = sa + v + end + return 0 +end + +if equilibriumindex({1,3,5,7,9}) == 4 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if equilibriumindex({1,2,3,4,5}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if equilibriumindex({2,4,2}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-160/roger-bell-west/perl/ch-1.pl b/challenge-160/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..f945c0965e --- /dev/null +++ b/challenge-160/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,30 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 4; + +is(fim(5),"Five is four, four is magic.",'example 1'); +is(fim(7),"Seven is five, five is four, four is magic.",'example 2'); +is(fim(6),"Six is three, three is five, five is four, four is magic.",'example 3'); +is(fim(4),"Four is magic.",'example 4'); + +sub fim { + my $n=shift; + my @words=qw(zero one two three four five six seven eight nine); + my @p; + while (1) { + my $s=$words[$n] . ' is '; + if ($n == 4) { + $s .= 'magic.'; + push @p,$s; + last; + } else { + $n=length($words[$n]); + $s .= $words[$n]; + push @p,$s; + } + } + return ucfirst(join(', ',@p)); +} diff --git a/challenge-160/roger-bell-west/perl/ch-2.pl b/challenge-160/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..b5315ad1f7 --- /dev/null +++ b/challenge-160/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; +use List::Util qw(sum); + +is(equilibriumindex([1,3,5,7,9]),3,'example 1'); +is(equilibriumindex([1,2,3,4,5]),-1,'example 2'); +is(equilibriumindex([2,4,2]),1,'example 3'); + +sub equilibriumindex { + my $s=shift; + my $sm=sum(@{$s}); + my $sa=0; + foreach my $i (0..$#{$s}) { + $sa += $s->[$i]; + if ($sa == $sm) { + return $i; + } + $sa += $s->[$i]; + } + return -1; +} diff --git a/challenge-160/roger-bell-west/postscript/ch-1.ps b/challenge-160/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..78445003b6 --- /dev/null +++ b/challenge-160/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,169 @@ +%!PS + +% begin library code + +/strconcat % (a) (b) -> (ab) +{ exch dup length + 2 index length add string + dup dup 4 2 roll copy length + 4 -1 roll putinterval +} bind def + +/strjoin % [(a) (b) (c)] (j) -> (ajbjc) +{ + 3 dict begin + /j exch def + dup 0 get /out exch def + /first true def + { + first { + /first false def + } { + out j strconcat + exch strconcat + /out exch def + } ifelse + } forall + out + end +} def + +/apush { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +% end library code + +/fid { + 3 dict begin + /words [ (zero) (one) (two) (three) (four) + (five) (six) (seven) (eight) (nine) ] def + /p 0 array def + { + dup + words exch get ( is ) strconcat /s exch def + dup 4 eq { + pop + /s s (magic.) strconcat def + /p p s apush def + exit + } { + words exch get length + dup words exch get s exch strconcat /s exch def + /p p s apush def + } ifelse + } loop + p (, ) strjoin + dup 0 get 32 sub exch dup 3 -1 roll 0 exch put + end +} bind def + +(fid) test.start + +5 fid +(Five is four, four is magic.) +deepeq test + +7 fid +(Seven is five, five is four, four is magic.) +deepeq test + +6 fid +(Six is three, three is five, five is four, four is magic.) +deepeq test + +4 fid +(Four is magic.) +deepeq test + +test.end diff --git a/challenge-160/roger-bell-west/postscript/ch-2.ps b/challenge-160/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..14ba76e597 --- /dev/null +++ b/challenge-160/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,84 @@ +%!PS + +% begin library code + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/reduce { % array proc -> value + 2 dict begin + /p exch def + /a exch def + a 0 get + 1 1 a length 1 sub { + a exch get + p + } for + end +} bind def + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + +% end library code + +/equilibriumindex { + 3 dict begin + /x -1 def + dup { add } reduce /sm exch def + /sa 0 def + enumerate.array { + dup 1 get sa add /sa exch def + sa sm eq { + 0 get /x exch def + exit + } if + 1 get sa add /sa exch def + } forall + x + end +} bind def + +(equilibriumindex) test.start + +[ 1 3 5 7 9 ] equilibriumindex 3 eq test +[ 1 2 3 4 5 ] equilibriumindex -1 eq test +[ 2 4 2 ] equilibriumindex 1 eq test + +test.end diff --git a/challenge-160/roger-bell-west/python/ch-1.py b/challenge-160/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..3881cbf52f --- /dev/null +++ b/challenge-160/roger-bell-west/python/ch-1.py @@ -0,0 +1,44 @@ +#! /usr/bin/python3 + +import unittest + +def fim(n0): + words=["zero","one","two","three","four", + "five","six","seven","eight","nine"] + n=n0 + p=[] + while True: + s = words[n] + " is " + if n==4: + s += "magic." + p.append(s) + break + else: + n = len(words[n]) + s += words[n] + p.append(s) + return (", ".join(p)).capitalize() + +class TestFim(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(fim(5), + "Five is four, four is magic.", + 'example 1') + + def test_ex2(self): + self.assertEqual(fim(7), + "Seven is five, five is four, four is magic.", + 'example 2') + + def test_ex3(self): + self.assertEqual(fim(6), + "Six is three, three is five, five is four, four is magic.", + 'example 3') + + def test_ex4(self): + self.assertEqual(fim(4), + "Four is magic.", + 'example 4') + +unittest.main() diff --git a/challenge-160/roger-bell-west/python/ch-2.py b/challenge-160/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..aeca8ffbb1 --- /dev/null +++ b/challenge-160/roger-bell-west/python/ch-2.py @@ -0,0 +1,34 @@ +#! /usr/bin/python3 + +import unittest + +from functools import reduce + +def equilibriumindex(s): + sm=sum(s) + sa=0 + for i,v in enumerate(s): + sa += v + if sa == sm: + return i + sa += v + return -1 + +class TestEquilibriumindex(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(equilibriumindex([1,3,5,7,9]), + 3, + 'example 1') + + def test_ex2(self): + self.assertEqual(equilibriumindex([1,2,3,4,5]), + -1, + 'example 2') + + def test_ex3(self): + self.assertEqual(equilibriumindex([2,4,2]), + 1, + 'example 3') + +unittest.main() diff --git a/challenge-160/roger-bell-west/raku/ch-1.p6 b/challenge-160/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..d4c1a17dbe --- /dev/null +++ b/challenge-160/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,30 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is(fim(5),"Five is four, four is magic.",'example 1'); +is(fim(7),"Seven is five, five is four, four is magic.",'example 2'); +is(fim(6),"Six is three, three is five, five is four, four is magic.",'example 3'); +is(fim(4),"Four is magic.",'example 4'); + +sub fim($n0) { + my $n=$n0; + my @words=("zero","one","two","three","four", + "five","six","seven","eight","nine"); + my @p; + while (True) { + my $s=@words[$n] ~ ' is '; + if ($n==4) { + $s ~= 'magic.'; + @p.push($s); + last; + } else { + $n=chars(@words[$n]); + $s ~= @words[$n]; + @p.push($s); + } + } + return tc(join(', ',@p)); +} diff --git a/challenge-160/roger-bell-west/raku/ch-2.p6 b/challenge-160/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..dc797f081e --- /dev/null +++ b/challenge-160/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,89 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(equilibriumindex([1,3,5,7,9]),3,'example 1'); + +is(equilibriumindex([1,2,3,4,5]),-1,'example 2'); + +is(equilibriumindex([2,4,2]),1,'example 3'); + +sub equilibriumindex(@s) { + my $sm=sum(@s); + my $sa=0; + for @s.kv -> $i,$v { + $sa += $v; + if ($sa == $sm) { + return $i; + } + $sa += $v; + } + return -1; +} + +sub genprimes($mx) { + my @primes; + { + my $primesh=(2,3).SetHash; + loop (my $i=6;$i <= $mx+1; $i += 6) { + for ($i-1,$i+1) -> $j { + if ($j <= $mx) { + $primesh{$j}=True; + } + } + } + my $p=2; + my @q=[2,3,5,7]; + my $mr=floor(sqrt($mx)); + while ($p <= $mr) { + if ($primesh{$p}:exists) { + my $i=$p*$p; + while ($i <= $mx) { + $primesh{$i}:delete; + $i += $p; + } + } + if (@q.elems < 2) { + @q.push(@q[*-1]+4); + @q.push(@q[*-1]+2); + } + $p=@q.shift; + } + @primes=$primesh.keys.sort; + } + return @primes; +} + +sub primefactor($n) { + my %f; + my $m=$n; + for genprimes(floor(sqrt($n))) -> $p { + while ($m % $p == 0) { + %f{$p}++; + $m=floor($m/$p); + } + if ($m == 1) { + last; + } + } + if ($m > 1) { + %f{$m}++; + } + return %f; +} + +sub moebius($n) { + my $z=0; + for primefactor($n).values -> $v { + if ($v > 1) { + return 0; + } + $z++; + } + if ($z % 2 == 0) { + return 1; + } + return -1; +} diff --git a/challenge-160/roger-bell-west/ruby/ch-1.rb b/challenge-160/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..a490156938 --- /dev/null +++ b/challenge-160/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,47 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def fim(n0) + words=["zero","one","two","three","four", + "five","six","seven","eight","nine"] + n=n0 + p=[] + while true do + s = words[n] + " is " + if n==4 then + s += "magic." + p.push(s) + break + else + n = words[n].length + s += words[n] + p.push(s) + end + end + return p.join(", ").capitalize +end + +class TestFim < Test::Unit::TestCase + + def test_ex1 + assert_equal("Five is four, four is magic.", + fim(5)) + end + + def test_ex2 + assert_equal("Seven is five, five is four, four is magic.", + fim(7)) + end + + def test_ex3 + assert_equal("Six is three, three is five, five is four, four is magic.", + fim(6)) + end + + def test_ex4 + assert_equal("Four is magic.", + fim(4)) + end + +end diff --git a/challenge-160/roger-bell-west/ruby/ch-2.rb b/challenge-160/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..eb1ce62389 --- /dev/null +++ b/challenge-160/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,35 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def equilibriumindex(s) + sm=s.sum + sa=0 + s.each_with_index do |v,i| + sa += v + if sa == sm then + return i + end + sa += v + end + return -1 +end + +class TestEquilibriumindex < Test::Unit::TestCase + + def test_ex1 + assert_equal(3, + equilibriumindex([1,3,5,7,9])) + end + + def test_ex2 + assert_equal(-1, + equilibriumindex([1,2,3,4,5])) + end + + def test_ex3 + assert_equal(1, + equilibriumindex([2,4,2])) + end + +end diff --git a/challenge-160/roger-bell-west/rust/ch-1.rs b/challenge-160/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..aed6fa49c8 --- /dev/null +++ b/challenge-160/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,64 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!( + fim(5), + "Five is four, four is magic." + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + fim(7), + "Seven is five, five is four, four is magic." + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + fim(6), + "Six is three, three is five, five is four, four is magic." + ); +} + +#[test] +fn test_ex4() { + assert_eq!( + fim(4), + "Four is magic." + ); +} + +fn fim(n0: usize) -> String { + let words=vec!["zero","one","two","three","four", + "five","six","seven","eight","nine"]; + let mut n = n0; + let mut p = Vec::new(); + loop { + let mut s = format!("{} is ",words[n]); + if n==4 { + s.push_str("magic."); + p.push(s); + break; + } else { + n = words[n].len(); + s.push_str(words[n]); + p.push(s); + } + } + upfirst(p.join(", ")) +} + +// borrowed from Shepmaster +// https://stackoverflow.com/questions/38406793/why-is-capitalizing-the-first-letter-of-a-string-so-convoluted-in-rust +fn upfirst(s: String) -> String { + let mut c = s.chars(); + match c.next() { + None => String::new(), + Some(f) => f.to_uppercase().collect::<String>() + c.as_str(), + } +} diff --git a/challenge-160/roger-bell-west/rust/ch-2.rs b/challenge-160/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..c5bbc6d4dd --- /dev/null +++ b/challenge-160/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,39 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!( + equilibriumindex(vec![1,3,5,7,9]), + 3 + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + equilibriumindex(vec![1,2,3,4,5]), + -1 + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + equilibriumindex(vec![2,4,2]), + 1 + ); +} + +fn equilibriumindex(s: Vec<isize>) -> isize { + let sm=&s.iter().sum::<isize>(); + let mut sa=0; + for (i,v) in s.iter().enumerate() { + sa += v; + if sa == *sm { + return i as isize; + } + sa += v; + } + -1 +} |
