Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #57897

Rosetta: Equilibrium index

From "Robert L." <No_spamming@noWhere_7073.org>
Newsgroups comp.lang.forth
Subject Rosetta: Equilibrium index
Date 2017-04-23 10:25 +0000
Organization Aioe.org NNTP Server
Message-ID <odhvev$mb7$1@gioia.aioe.org> (permalink)

Show all headers | View raw


> An equilibrium index of a sequence is an index into the
> sequence such that the sum of elements at lower indices is
> equal to the sum of elements at higher indices.
> 
> Task
> 
> Write a function that, given a sequence, returns its
> equilibrium indices (if any).
> 
> Assume that the sequence may be very long.

def eqi array
  left_sum = x = 0
  right_sum = array.reduce :+
  array.size.times.select{|i|
    (left_sum += x) == (right_sum -= (x = array[i])) }
end

eqi  [-7, 1, 5, 2, -4, 3, 0]
    ==>[3, 6]

eqi [1, -1, 1, -1, 1, -1, 1]
    ==>[0, 1, 2, 3, 4, 5, 6]

eqi Array.new(999_000){rand(23)-11}
    ==>[282389, 282504, 455577, 455823, 456954, 457069, 457198, 458439,
458449, 458483, 458522, 459570, 846169, 849372, 856437, 856497, 857097,
857321, 857541, 857680, 857689, 857704, 884942, 884991, 884993, 922443,
922451, 922452, 948234]

In Forth?

-- 
"Anti-racism," Shamir writes, "is a denial of the autochthon's [native's] right
to decide his fate; a tool to separate Man from his native landscape.  This
concept de-legitimizes objections to swamping a land with a flood of immigrants
and ruining the society's fabric."
www.theoccidentalobserver.net/authors/Connelly-Gaza2.html

Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Rosetta: Equilibrium index "Robert L." <No_spamming@noWhere_7073.org> - 2017-04-23 10:25 +0000
  Re: Rosetta: Equilibrium index Julian Fondren <julian.fondren@gmail.com> - 2017-04-23 06:29 -0700

csiph-web