Equilibrium index: Difference between revisions

m
→‎version 1: added/changed whitespace, use a template for the output sections.
(Added Wren)
m (→‎version 1: added/changed whitespace, use a template for the output sections.)
Line 2,261:
<lang rexx>/*REXX program calculates and displays the equilibrium index for a numeric array (list).*/
parse arg x /*obtain the optional arguments from CL*/
if x='' then x= copies(" 7 -7", 50) 7 /*Not specified? Then use the default.*/
say ' array list: ' space(x) /*echo the array list to the terminal. */
#= words(x) /*the number of elements in the X list.*/
do j=0 for #; @.j= word(x, j+1) /*zero─start is for zero─based array. */
end /*j*/ /* [↑] assign @.0 @.1 @.3 ··· */
say /* ··· and also display a blank line. */
answer= equilibriumIDX(); w= words(answer) /*calculate the equilibrium index. */
say 'equilibrium' word("(none) index: indices:", 1 + (w>0) + (w>1)) answer
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
equilibriumIDX: $=; do i=0 for #; sum= 0
do k=0 for #; sum= sum + @.k * sign(k-i); end /*k*/
if sum==0 then $= $ i
end /*i*/ /* [↑] Zero? Found an equilibrium index*/
return $ /*return equilibrium list (may be null)*/</lang>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> -7 &nbsp; 1 &nbsp; 5 &nbsp; 2 &nbsp; -4 &nbsp; 3 &nbsp; 0 </tt>}}
<pre>
array list: -7 1 5 2 -4 3 0
Line 2,282:
equilibrium indices: 3 6
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 2 &nbsp; 9 &nbsp; 2 </tt>}}
<pre>
array list: 2 9 2
Line 2,288:
equilibrium index: 1
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 5 &nbsp; 4 &nbsp; 4 &nbsp; 5 </tt>}}
<pre>
array list: 5 4 4 5
Line 2,294:
equilibrium (none)
</pre>
'''{{out|output''' |text=&nbsp; when using the default input:}}
<pre>
array list: 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7 -7 7