How to resolve the algorithm Equilibrium index step by step in the Logo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Equilibrium index step by step in the Logo programming language
Table of Contents
Problem Statement
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.
For example, in a sequence
A
{\displaystyle A}
: 3 is an equilibrium index, because: 6 is also an equilibrium index, because: (sum of zero elements is zero) 7 is not an equilibrium index, because it is not a valid index of sequence
A
{\displaystyle A}
.
Write a function that, given a sequence, returns its equilibrium indices (if any). Assume that the sequence may be very long.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Equilibrium index step by step in the Logo programming language
Source code in the logo programming language
to equilibrium.iter :i :before :after :tail :ret
if equal? :before :after [make "ret lput :i :ret]
if empty? butfirst :tail [output :ret]
output equilibrium.iter :i+1 (:before+first :tail) (:after-first butfirst :tail) (butfirst :tail) :ret
end
to equilibrium.index :list
output equilibrium.iter 1 0 (apply "sum butfirst :list) :list []
end
show equilibrium_index [-7 1 5 2 -4 3 0] ; [4 7]
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Perl programming language
You may also check:How to resolve the algorithm Determine sentence type step by step in the Julia programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Window creation step by step in the TorqueScript programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the MMIX programming language