Equilibrium index: Difference between revisions

Added Easylang
(→‎{{header|AppleScript}}: Added straightforward solution.)
(Added Easylang)
 
(9 intermediate revisions by 6 users not shown)
Line 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 969 ⟶ 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 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,003 ⟶ 1,050:
while(en.next())
{
var element := *en.get();
right -= element;
bool found := (left == right);
Line 1,030 ⟶ 1,077:
}
get Value() = index;
enumerable() => en;
Line 1,038 ⟶ 1,085:
{
EquilibriumEnumerator.new(new int[]{ -7, 1, 5, 2, -4, 3, 0 })
.forEach:(printingLn)
}</syntaxhighlight>
<pre>
Line 1,220 ⟶ 1,267:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Equilibrium_index}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Equilibrium index 01.png]]
In '''[https://formulae.org/?example=Equilibrium_index this]''' page you can see the program(s) related to this task and their results.
 
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,698 ⟶ 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,974 ⟶ 3,071:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var equilibrium = Fn.new { |a|
Line 3,001 ⟶ 3,098:
System.print("The equilibrium indices for the following sequences are:\n")
for (test in tests) {
SystemFmt.print("%(Fmt.s(24$24n -> $n", test)), -> %(equilibrium.call(test))")
}</syntaxhighlight>
 
1,969

edits