RosettaCodeData/Task/Equilibrium-index/FutureBasic/equilibrium-index.basic

28 lines
892 B
Plaintext

CFArrayRef local fn EquilibriumIndexes( arr as CFArrayRef )
NSInteger count = len(arr)
if ( count == 0 ) then return @[]
CFNumberRef sumLeft = @0, sumRight, sumAll = fn ObjectValueForKeyPath( arr, @"@sum.self" )
CFMutableArrayRef result = fn MutableArrayNew
for NSUInteger i = 0 to count - 1
CFNumberRef currVal = arr[i]
sumRight = @(dblval(sumAll) - dblval(sumLeft) - dblval(currVal))
if ( fn NumberIsEqual( sumLeft, sumRight ) )
MutableArrayAddObject( result, @(i) )
end if
sumLeft = @(dblval(sumLeft) + dblval(currVal))
next
end fn = result
void local fn DoIt
CFArrayRef arr = @[@(-7), @1, @5, @2, @(-4), @3, @0]
CFArrayRef eqIndexes = fn EquilibriumIndexes( arr )
printf @"Equilibrium indexes of [%@]: [%@]",fn ArrayComponentsJoinedByString( arr, @", " ) ,fn ArrayComponentsJoinedByString( eqIndexes, @", " )
end fn
fn DoIt
HandleEvents