Equilibrium index: Difference between revisions

Added Easylang
(→‎{{header|jq}}: use foreach; now also works with jaq)
(Added Easylang)
 
(12 intermediate revisions by 8 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 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 974 ⟶ 1,050:
while(en.next())
{
var element := *en.get();
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:(printingLn)
}</syntaxhighlight>
<pre>
Line 1,191 ⟶ 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 1,528 ⟶ 1,624:
. as $in
| add as $add
| foreach range(0;length) as $i (
| if 0 == $add - .[0] then 0 else empty end,
[0, 0, $add]; # [before, pivot, after]
foreach range(1;length) as $i (
[0, .[0], $add - .[0]]; # [before, pivot, after]
$in[$i] as $x | [.[0]+.[1], $x, .[2] - $x];
if .[0] == .[2] then $i else empty end) ;
Line 1,546 ⟶ 1,641:
def array(n;init): reduce range(0;n) as $i ([]; . + [0]);
 
count( array(1e410000;0) | equilibrium_indices )</syntaxhighlight>
{{out}}
$ jq -M -n -f equilibrium_indices.jq
Line 2,670 ⟶ 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,946 ⟶ 3,071:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var equilibrium = Fn.new { |a|
Line 2,973 ⟶ 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