2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,24 +1,31 @@
|
|||
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 <math>A</math>:
|
||||
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.
|
||||
|
||||
: <math>A_0 = -7</math>
|
||||
: <math>A_1 = 1</math>
|
||||
: <math>A_2 = 5</math>
|
||||
: <math>A_3 = 2</math>
|
||||
: <math>A_4 = -4</math>
|
||||
: <math>A_5 = 3</math>
|
||||
: <math>A_6 = 0</math>
|
||||
|
||||
3 is an equilibrium index, because:
|
||||
For example, in a sequence <big><math>A</math></big>:
|
||||
|
||||
: <math>A_0 + A_1 + A_2 = A_4 + A_5 + A_6</math>
|
||||
::::: <big><math>A_0 = -7</math></big>
|
||||
::::: <big><math>A_1 = 1</math></big>
|
||||
::::: <big><math>A_2 = 5</math></big>
|
||||
::::: <big><math>A_3 = 2</math></big>
|
||||
::::: <big><math>A_4 = -4</math></big>
|
||||
::::: <big><math>A_5 = 3</math></big>
|
||||
::::: <big><math>A_6 = 0</math></big>
|
||||
|
||||
6 is also an equilibrium index, because:
|
||||
3 is an equilibrium index, because:
|
||||
|
||||
: <math>A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0</math>
|
||||
::::: <big><math>A_0 + A_1 + A_2 = A_4 + A_5 + A_6</math></big>
|
||||
|
||||
6 is also an equilibrium index, because:
|
||||
|
||||
::::: <big><math>A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0</math></big>
|
||||
|
||||
(sum of zero elements is zero)
|
||||
|
||||
7 is not an equilibrium index, because it is not a valid index of sequence <math>A</math>.
|
||||
7 is not an equilibrium index, because it is not a valid index of sequence <big><math>A</math></big>.
|
||||
|
||||
|
||||
;Task;
|
||||
Write a function that, given a sequence, returns its equilibrium indices (if any).
|
||||
|
||||
Assume that the sequence may be very long.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,21 +1,19 @@
|
|||
function equilibrium (array) {
|
||||
var equilibriums = [];
|
||||
|
||||
array.forEach(function(_, idx, arr) {
|
||||
var left = 0, right = 0;
|
||||
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
if (i < idx) {
|
||||
left += array[i];
|
||||
} else if (i > idx) {
|
||||
right += array[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (left === right) equilibriums.push(idx);
|
||||
});
|
||||
|
||||
return equilibriums;
|
||||
function equilibrium(a) {
|
||||
var N = a.length, i, l = [], r = [], e = []
|
||||
for (l[0] = a[0], r[N - 1] = a[N - 1], i = 1; i<N; i++)
|
||||
l[i] = l[i - 1] + a[i], r[N - i - 1] = r[N - i] + a[N - i - 1]
|
||||
for (i = 0; i < N; i++)
|
||||
if (l[i] === r[i]) e.push(i)
|
||||
return e
|
||||
}
|
||||
|
||||
console.log(equilibrium([-7,1,5,2,-4,3,0]));
|
||||
// test & output
|
||||
[ [-7, 1, 5, 2, -4, 3, 0], // 3, 6
|
||||
[2, 4, 6], // empty
|
||||
[2, 9, 2], // 1
|
||||
[1, -1, 1, -1, 1, -1, 1], // 0,1,2,3,4,5,6
|
||||
[1], // 0
|
||||
[] // empty
|
||||
].forEach(function(x) {
|
||||
console.log(equilibrium(x))
|
||||
});
|
||||
|
|
|
|||
86
Task/Equilibrium-index/Pascal/equilibrium-index-2.pascal
Normal file
86
Task/Equilibrium-index/Pascal/equilibrium-index-2.pascal
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
Program EquilibriumIndexDemo(output);
|
||||
{$IFDEF FPC}{$Mode delphi}{$ENDIF}
|
||||
type
|
||||
tEquiData = shortInt;//Int64;extended ,double
|
||||
tnumList = array of tEquiData;
|
||||
tresList = array of LongInt;
|
||||
const
|
||||
cNumbers: array [11..17] of tEquiData = (-7, 1, 5, 2, -4, 3, 0);
|
||||
|
||||
function ArraySum(const list: tnumList):tEquiData;
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
result := 0;
|
||||
for i := Low(list) to High(list) do
|
||||
result := result+list[i];
|
||||
end;
|
||||
|
||||
procedure EquilibriumIndex(const list:tnumList;
|
||||
var indices:tresList);
|
||||
var
|
||||
pC : ^tEquiData;
|
||||
LeftSum,
|
||||
RightSum : tEquiData;
|
||||
i,idx,HiList: integer;
|
||||
|
||||
begin
|
||||
HiList := High(List);
|
||||
RightSum :=ArraySum(list);
|
||||
setlength(indices,10);
|
||||
idx := 0;
|
||||
|
||||
i := -Hilist;
|
||||
pC := @List[0];
|
||||
LeftSum:= 0;
|
||||
repeat
|
||||
Rightsum:= RightSum-pC^;
|
||||
IF LeftSum = RightSum then
|
||||
Begin
|
||||
indices[idx] := Hilist+i;
|
||||
inc(idx);
|
||||
IF idx > high(indices) then
|
||||
setlength(indices, idx+10);
|
||||
end;
|
||||
inc(i);
|
||||
leftSum := leftsum+pC^;
|
||||
inc(pC);
|
||||
until i>=0;
|
||||
leftSum := leftsum+pC^;
|
||||
IF LeftSum = RightSum then
|
||||
Begin
|
||||
indices[idx] := Hilist+i;
|
||||
inc(idx);
|
||||
end;
|
||||
setlength(indices,idx);
|
||||
end;
|
||||
|
||||
procedure TestRun(const numbers:tnumList);
|
||||
var
|
||||
indices : tresList;
|
||||
i: integer;
|
||||
Begin
|
||||
write('List of numbers: ');
|
||||
for i := low(numbers) to high(numbers) do
|
||||
write(numbers[i]:3);
|
||||
writeln;
|
||||
EquilibriumIndex(numbers,indices);
|
||||
write('Equilibirum indices: ');
|
||||
EquilibriumIndex(numbers,indices);
|
||||
for i := low(indices) to high(indices) do
|
||||
write(indices[i]:3);
|
||||
writeln;
|
||||
writeln;
|
||||
end;
|
||||
|
||||
var
|
||||
numbers: tnumList;
|
||||
I: integer;
|
||||
begin
|
||||
setlength(numbers,High(cNumbers)-Low(cNumbers)+1);
|
||||
move(cNumbers[Low(cNumbers)],numbers[0],sizeof(cnumbers));
|
||||
TestRun(numbers);
|
||||
for i := low(numbers) to high(numbers) do
|
||||
numbers[i]:= 0;
|
||||
TestRun(numbers);
|
||||
end.
|
||||
|
|
@ -9,4 +9,4 @@ sub equilibrium_index(@list) {
|
|||
}
|
||||
|
||||
my @list = -7, 1, 5, 2, -4, 3, 0;
|
||||
.say for equilibrium_index(@list);
|
||||
.say for equilibrium_index(@list).grep(/\d/);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
sub equilibrium_index(@list) {
|
||||
my @a := [\+] @list;
|
||||
my @a = [\+] @list;
|
||||
my @b := reverse [\+] reverse @list;
|
||||
^@list Zxx (@a »==« @b);
|
||||
}
|
||||
|
|
|
|||
22
Task/Equilibrium-index/PowerShell/equilibrium-index-1.psh
Normal file
22
Task/Equilibrium-index/PowerShell/equilibrium-index-1.psh
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function Get-EquilibriumIndex ( $Sequence )
|
||||
{
|
||||
$Indexes = 0..($Sequence.Count - 1)
|
||||
$EqulibriumIndex = @()
|
||||
|
||||
ForEach ( $TestIndex in $Indexes )
|
||||
{
|
||||
$Left = 0
|
||||
$Right = 0
|
||||
ForEach ( $Index in $Indexes )
|
||||
{
|
||||
If ( $Index -lt $TestIndex ) { $Left += $Sequence[$Index] }
|
||||
ElseIf ( $Index -gt $TestIndex ) { $Right += $Sequence[$Index] }
|
||||
}
|
||||
|
||||
If ( $Left -eq $Right )
|
||||
{
|
||||
$EqulibriumIndex += $TestIndex
|
||||
}
|
||||
}
|
||||
return $EqulibriumIndex
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
Get-EquilibriumIndex -7, 1, 5, 2, -4, 3, 0
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
function equil($arr){
|
||||
$res=@()
|
||||
for($i=0;$i -lt $arr.length;$i++){
|
||||
$left=0;$right=0
|
||||
|
||||
for($j=0;$j -lt $arr.length;$j++){
|
||||
if ($j -lt $i){$left+=$arr[$j]}
|
||||
if ($j -gt $i){$right+=$arr[$j]}
|
||||
}
|
||||
if($left -eq $right){$res+=$i}
|
||||
}
|
||||
[String]$res
|
||||
}
|
||||
|
||||
equil -7,1,5,2,-4,3,0
|
||||
|
|
@ -1,22 +1,21 @@
|
|||
/*REXX program finds the equilibrium index for a numeric array (list). */
|
||||
parse arg x /*get array's numbers from the CL.*/
|
||||
if x='' then x=copies(' 7 -7',50) 7 /*Nothing given? Generate a list.*/
|
||||
say ' array list: ' space(x) /*echo the array list to screen. */
|
||||
n=words(x) /*the number of words in the list.*/
|
||||
do j=0 for n /*0─start is for zero─based array.*/
|
||||
A.j=word(x, j+1) /*define the array element. */
|
||||
end /*j*/ /* [↑] assign A.0 A.1 A.3 ··· */
|
||||
say /*··· and also show a blank line. */
|
||||
ans=equilibrium_index(n) /*calculate the equilibrium index.*/
|
||||
say 'equilibrium' word('indices index', 1 + (words(ans==1)))": " ans
|
||||
exit /*stick a fork in it, we're done. */
|
||||
/*──────────────────────────────────EQUILIBRIUM_INDEX subroutine─────────*/
|
||||
equilibrium_index: procedure expose A. /*have the array A. be exposed.*/
|
||||
parse arg # /*stemmed array A. starts at 0*/
|
||||
$= /*equilibrium indices (so far). */
|
||||
do i=0 for #; sum=0
|
||||
do k=0 for #; sum=sum + A.k*sign(k-i); end /*k*/
|
||||
if sum=0 then $=$ i
|
||||
end /*i*/
|
||||
if $=='' then $="(none)" /*adjust if no indices are found. */
|
||||
return strip($) /*return the equilibrium list. */
|
||||
/*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. */
|
||||
n=words(x) /*the number of numbers in the X list.*/
|
||||
do j=0 for n /*zero─start is for zero─based array. */
|
||||
A.j=word(x, j+1) /*define the array element ───► A.j */
|
||||
end /*j*/ /* [↑] assign A.0 A.1 A.3 ··· */
|
||||
say /* ··· and also display a blank line. */
|
||||
ans=equilibriumIDX(n) /*calculate the equilibrium index. */
|
||||
say 'equilibrium' word("indices index", 1 + (words(ans==1)))': ' ans
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
equilibriumIDX: procedure expose A.; parse arg # /*expose the array A. (make global).*/
|
||||
$= /*equilibrium indices (so far). */
|
||||
do i=0 for #; sum=0
|
||||
do k=0 for #; sum=sum + A.k*sign(k-i); end /*k*/
|
||||
if sum=0 then $=$ i
|
||||
end /*i*/
|
||||
if $=='' then $="(none)" /*adjust if no indices were found. */
|
||||
return strip($) /*return the equilibrium list. */
|
||||
|
|
|
|||
8
Task/Equilibrium-index/Scala/equilibrium-index.scala
Normal file
8
Task/Equilibrium-index/Scala/equilibrium-index.scala
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def getEquilibriumIndex(A: Array[Int]): Int = {
|
||||
val bigA: Array[BigInt] = A.map(BigInt(_))
|
||||
val partialSums: Array[BigInt] = bigA.scanLeft(BigInt(0))(_+_).tail
|
||||
def lSum(i: Int): BigInt = if (i == 0) 0 else partialSums(i - 1)
|
||||
def rSum(i: Int): BigInt = partialSums.last - partialSums(i)
|
||||
def isRandLSumEqual(i: Int): Boolean = lSum(i) == rSum(i)
|
||||
(0 until partialSums.length).find(isRandLSumEqual).getOrElse(-1)
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
10 DATA 7,-7,1,5,2,-4,3,0
|
||||
20 READ n
|
||||
30 DIM a(n): LET sum=0: LET leftsum=0: LET s$=""
|
||||
40 FOR i=1 TO n: READ a(i): LET sum=sum+a(i): NEXT i
|
||||
50 FOR i=1 TO n
|
||||
60 LET sum=sum-a(i)
|
||||
70 IF leftsum=sum THEN LET s$=s$+STR$ i+" "
|
||||
80 LET leftsum=leftsum+a(i)
|
||||
90 NEXT i
|
||||
100 PRINT "Numbers: ";
|
||||
110 FOR i=1 TO n: PRINT a(i);" ";: NEXT i
|
||||
120 PRINT '"Indices: ";s$
|
||||
Loading…
Add table
Add a link
Reference in a new issue