Equilibrium index: Difference between revisions
Added Easylang
(→{{header|jq}}: shave off one line) |
(Added Easylang) |
||
(10 intermediate revisions by 7 users not shown) | |||
Line 389:
=={{header|AppleScript}}==
===Functional===
{{Trans|JavaScript}}(ES6 version)
<syntaxhighlight lang="applescript">-- equilibriumIndices :: [Int] -> [Int]
Line 570 ⟶ 571:
{{Out}}
<pre>{{3, 6}, {}, {1}, {0, 1, 2, 3, 4, 5, 6}, {0}, {}}</pre>
----
===Straightforward===
<syntaxhighlight lang="applescript">on equilibriumIndices(sequence)
script o
property seq : sequence
property output : {}
end script
set loSum to 0
set hiSum to 0
repeat with value in o's seq
set hiSum to hiSum + value
end repeat
repeat with i from 1 to (count o's seq)
set value to o's seq's item i
set hiSum to hiSum - value
if (hiSum = loSum) then set o's output's end to i
set loSum to loSum + value
end repeat
return o's output
end equilibriumIndices
equilibriumIndices({-7, 1, 5, 2, -4, 3, 0})</syntaxhighlight>
{{output}}
AppleScript uses 1-based indices.
<syntaxhighlight lang="applescript">{4, 7}</syntaxhighlight>
=={{header|Arturo}}==
Line 668 ⟶ 697:
indices: 1 2 3 4 5 6 7
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|Ring}}
<syntaxhighlight lang="vb">arraybase 1
dim list = {-7, 1, 5, 2, -4, 3, 0}
print "equilibrium indices are : "; equilibrium(list)
end
function equilibrium (l)
r = 0: s = 0
e$ = ""
for n = 1 to l[?]
s += l[n]
next
for i = 1 to l[?]
if r = s - r - l[i] then e$ += string(i-1) + " "
r += l[i]
next
e$ = left(e$, length(e$)-1)
return e$
end function</syntaxhighlight>
{{out}}
<pre>The equilibrium indices are : 3 6</pre>
=={{header|Batch File}}==
Line 940 ⟶ 994:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Equilibrium_index#Pascal Pascal].
=={{header|EasyLang}}==
<syntaxhighlight>
func[] equind a[] .
for v in a[]
sumr += v
.
for i to len a[]
sumr -= a[i]
if suml = sumr
r[] &= i
.
suml += a[i]
.
return r[]
.
print equind [ -7 1 5 2 -4 3 0 ]
</syntaxhighlight>
{{out}}
<pre>
[ 4 7 ]
</pre>
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 974 ⟶ 1,050:
while(en.next())
{
var element := *en
right -= element;
bool found := (left == right);
Line 1,001 ⟶ 1,077:
}
get Value() = index;
enumerable() => en;
Line 1,009 ⟶ 1,085:
{
EquilibriumEnumerator.new(new int[]{ -7, 1, 5, 2, -4, 3, 0 })
.forEach
}</syntaxhighlight>
<pre>
Line 1,191 ⟶ 1,267:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Equilibrium_index}}
'''Solution'''
[[File:Fōrmulæ - Equilibrium index 01.png]]
In Fōrmulæ, indices are 1-based so the output of this program will be shifted up by one compared to solutions in languages with 0-based arrays.
'''Test cases'''
[[File:Fōrmulæ - Equilibrium index 02.png]]
[[File:Fōrmulæ - Equilibrium index 03.png]]
[[File:Fōrmulæ - Equilibrium index 04.png]]
[[File:Fōrmulæ - Equilibrium index 05.png]]
[[File:Fōrmulæ - Equilibrium index 06.png]]
[[File:Fōrmulæ - Equilibrium index 07.png]]
[[File:Fōrmulæ - Equilibrium index 08.png]]
[[File:Fōrmulæ - Equilibrium index 09.png]]
=={{header|Go}}==
Line 2,669 ⟶ 2,765:
<pre>
equilibrium indices are : 3,6
</pre>
=={{header|RPL}}==
{| class="wikitable"
! RPL code
! Comment
|-
|
≪
0 SWAP + → seq
≪ { } 0 seq ∑LIST
2 seq SIZE '''FOR''' j
seq j GET - SWAP seq j 1 - GET + SWAP
'''IF''' DUP2 == '''THEN''' ROT j 2 - + ROT ROT '''END'''
'''NEXT''' DROP2
≫ ≫ ‘'''EQIDX'''’ STO
|
'''EQIDX''' ''( { A0..An } -- { equilibrium index } ) ''
add zero at list head to avoid GET error at first loop
left = 0 ; right = A0+A1+...An
loop from j=2 to length(seq) e.g. A0 to An
right -= seq[j] ; left += A[j-1]
if left = right then append j-2 to index list
drop left and right
return list
|}
{ -7 1 5 2 -4 3 0 } EQIDX
{{out}}
<pre>
1: { 3 6 }
</pre>
Line 2,945 ⟶ 3,071:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var equilibrium = Fn.new { |a|
Line 2,972 ⟶ 3,098:
System.print("The equilibrium indices for the following sequences are:\n")
for (test in tests) {
}</syntaxhighlight>
|