WeixiangYan/CodeTransOcean huggingface dataset:
Task: Loops_Wrong ranges



C#: using System;
using System.Collections.Generic;

static class Program
{
    static void Main()
    {
        Example(-2, 2, 1, "Normal");
        Example(-2, 2, 0, "Zero increment");
        Example(-2, 2, -1, "Increments away from stop value");
        Example(-2, 2, 10, "First increment is beyond stop value");
        Example(2, -2, 1, "Start more than stop: positive increment");
        Example(2, 2, 1, "Start equal stop: positive increment");
        Example(2, 2, -1, "Start equal stop: negative increment");
        Example(2, 2, 0, "Start equal stop: zero increment");
        Example(0, 0, 0, "Start equal stop equal zero: zero increment");
    }

    static IEnumerable<int> Range(int start, int stop, int increment)
    {
        
        

        
        

        
        for (int i = start; i <= stop; i += increment)
            yield return i;
    }

    static void Example(int start, int stop, int increment, string comment)
    {
        
        Console.Write((comment + " ").PadRight(50, '-') + " ");

        const int MAX_ITER = 9;

        int iteration = 0;
        foreach (int i in Range(start, stop, increment))
        {
            Console.Write("{0,2} ", i);

            if (++iteration > MAX_ITER) break;
        }

        Console.WriteLine();
    }
}



Java: import java.util.ArrayList;
import java.util.List;

public class LoopsWrongRanges {

    public static void main(String[] args) {
        runTest(new LoopTest(-2, 2, 1, "Normal"));
        runTest(new LoopTest(-2, 2, 0, "Zero increment"));
        runTest(new LoopTest(-2, 2, -1, "Increments away from stop value"));
        runTest(new LoopTest(-2, 2, 10, "First increment is beyond stop value"));
        runTest(new LoopTest(2, -2, 1, "Start more than stop: positive increment"));
        runTest(new LoopTest(2, 2, 1, "Start equal stop: positive increment"));
        runTest(new LoopTest(2, 2, -1, "Start equal stop: negative increment"));
        runTest(new LoopTest(2, 2, 0, "Start equal stop: zero increment"));
        runTest(new LoopTest(0, 0, 0, "Start equal stop equal zero: zero increment"));
    }
    
    private static void runTest(LoopTest loopTest) {
        List<Integer> values = new ArrayList<>();
        for (int i = loopTest.start ; i <= loopTest.stop ; i += loopTest.increment ) {
            values.add(i);
            if ( values.size() >= 10 ) {
                break;
            }
        }
        System.out.printf("%-45s %s%s%n", loopTest.comment, values, values.size()==10 ? " (loops forever)" : "");
    }
    
    private static class LoopTest {
        int start;
        int stop;
        int increment;
        String comment;
        public LoopTest(int start, int stop, int increment, String comment) {
            this.start = start;
            this.stop = stop;
            this.increment = increment;
            this.comment = comment;
        }
    }

}
Task: Loops_Wrong ranges




Go: package main

import "fmt"

type S struct {
    start, stop, incr int
    comment          string
}

var examples = []S{
    {-2, 2, 1, "Normal"},
    {-2, 2, 0, "Zero increment"},
    {-2, 2, -1, "Increments away from stop value"},
    {-2, 2, 10, "First increment is beyond stop value"},
    {2, -2, 1, "Start more than stop: positive increment"},
    {2, 2, 1, "Start equal stop: positive increment"},
    {2, 2, -1, "Start equal stop: negative increment"},
    {2, 2, 0, "Start equal stop: zero increment"},
    {0, 0, 0, "Start equal stop equal zero: zero increment"},
}

func sequence(s S, limit int) []int {
    var seq []int
    for i, c := s.start, 0; i <= s.stop && c < limit; i, c = i+s.incr, c+1 {
        seq = append(seq, i)
    }
    return seq
}

func main() {
    const limit = 10
    for _, ex := range examples {
        fmt.Println(ex.comment)
        fmt.Printf("Range(%d, %d, %d) -> ", ex.start, ex.stop, ex.incr)
        fmt.Println(sequence(ex, limit))
        fmt.Println()
    }
}




Python: import re
from itertools import islice 




data = 

table = [re.split(r'\s\s+', line.strip()) for line in data.strip().split('\n')]

for _start, _stop, _increment, comment in table[1:]:
    start, stop, increment = [int(x) for x in (_start, _stop, _increment)]
    print(f'{comment.upper()}:\n  range({start}, {stop}, {increment})')
    values = None
    try: 
        values = list(islice(range(start, stop, increment), 999))
    except ValueError as e:
        print('  !!ERROR!!', e)
    if values is not None:
        if len(values) < 22:
            print('    =', values)
        else:
            print('    =', str(values[:22])[:-1], '...')
Task: Loops_Wrong ranges







Python: import re
from itertools import islice 




data = 

table = [re.split(r'\s\s+', line.strip()) for line in data.strip().split('\n')]

for _start, _stop, _increment, comment in table[1:]:
    start, stop, increment = [int(x) for x in (_start, _stop, _increment)]
    print(f'{comment.upper()}:\n  range({start}, {stop}, {increment})')
    values = None
    try: 
        values = list(islice(range(start, stop, increment), 999))
    except ValueError as e:
        print('  !!ERROR!!', e)
    if values is not None:
        if len(values) < 22:
            print('    =', values)
        else:
            print('    =', str(values[:22])[:-1], '...')


VB: Public Sub LoopsWrongRanges()
    Call Example(-2, 2, 1, "Normal")
    Call Example(-2, 2, 0, "Zero increment")
    Call Example(-2, 2, -1, "Increments away from stop value")
    Call Example(-2, 2, 10, "First increment is beyond stop value")
    Call Example(2, -2, 1, "Start more than stop: positive increment")
    Call Example(2, 2, 1, "Start equal stop: positive increment")
    Call Example(2, 2, -1, "Start equal stop: negative increment")
    Call Example(2, 2, 0, "Start equal stop: zero increment")
    Call Example(0, 0, 0, "Start equal stop equal zero: zero increment")
End Sub
Private Sub Example(start As Integer, stop_ As Integer, by As Integer, comment As String)
    Dim i As Integer
    Dim c As Integer
    Const limit = 10
    c = 0
    Debug.Print start; " "; stop_; " "; by; " | ";
    For i = start To stop_ Step by
        Debug.Print i & ",";
        c = c + 1
        If c > limit Then Exit For
    Next i
    Debug.Print
    Debug.Print comment & vbCrLf
End Sub
Task: Two sum

C: #include<stdio.h>

int main()
{
	int arr[5] = {0, 2, 11, 19, 90},sum = 21,i,j,check = 0;
	
	for(i=0;i<4;i++){
		for(j=i+1;j<5;j++){
			if(arr[i]+arr[j]==sum){
				printf("[%d,%d]",i,j);
				check = 1;
				break;
			}
		}
	}
	
	if(check==0)
		printf("[]");
	
	return 0;
}



C#: #include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

pair<int, int> twoSum(vector<int> numbers, int sum) {
	auto m = map<int, int>();
	for (size_t i = 0; i < numbers.size(); ++i) {
		
		auto key = sum - numbers[i];

		if (m.find(key) != m.end()) {
			return make_pair(m[key], i);
		}
		m[numbers[i]] = i;
	}

	return make_pair(-1, -1);
}

int main() {
	auto numbers = vector<int>{ 0, 2, 11, 19, 90 };
	const int sum = 21;

	auto ts = twoSum(numbers, sum);
	if (ts.first != -1) {
		cout << "{" << ts.first << ", " << ts.second << "}" << endl;
	} else {
		cout << "no result" << endl;
	}

	return 0;
}
Task: Two sum

C: #include<stdio.h>

int main()
{
	int arr[5] = {0, 2, 11, 19, 90},sum = 21,i,j,check = 0;
	
	for(i=0;i<4;i++){
		for(j=i+1;j<5;j++){
			if(arr[i]+arr[j]==sum){
				printf("[%d,%d]",i,j);
				check = 1;
				break;
			}
		}
	}
	
	if(check==0)
		printf("[]");
	
	return 0;
}



C#: using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        int[] arr = { 0, 2, 11, 19, 90 };
        const int sum = 21;

        var ts = TwoSum(arr, sum);
        Console.WriteLine(ts != null ? $"{ts[0]}, {ts[1]}" : "no result");

        Console.ReadLine();
    }

    public static int[] TwoSum(int[] numbers, int sum)
    {
        var map = new Dictionary<int, int>();
        for (int i = 0; i < numbers.Length; i++)
        {
            
            var key = sum - numbers[i];
            if (map.ContainsKey(key))
            {
                return new[] { map[key], i };
            }
            map.Add(numbers[i], i);
        }
        return null;
    }
}
Task: Two sum

C: #include<stdio.h>

int main()
{
	int arr[5] = {0, 2, 11, 19, 90},sum = 21,i,j,check = 0;
	
	for(i=0;i<4;i++){
		for(j=i+1;j<5;j++){
			if(arr[i]+arr[j]==sum){
				printf("[%d,%d]",i,j);
				check = 1;
				break;
			}
		}
	}
	
	if(check==0)
		printf("[]");
	
	return 0;
}


C++: #include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

pair<int, int> twoSum(vector<int> numbers, int sum) {
	auto m = map<int, int>();
	for (size_t i = 0; i < numbers.size(); ++i) {
		
		auto key = sum - numbers[i];

		if (m.find(key) != m.end()) {
			return make_pair(m[key], i);
		}
		m[numbers[i]] = i;
	}

	return make_pair(-1, -1);
}

int main() {
	auto numbers = vector<int>{ 0, 2, 11, 19, 90 };
	const int sum = 21;

	auto ts = twoSum(numbers, sum);
	if (ts.first != -1) {
		cout << "{" << ts.first << ", " << ts.second << "}" << endl;
	} else {
		cout << "no result" << endl;
	}

	return 0;
}
Task: Two sum

C: #include<stdio.h>

int main()
{
	int arr[5] = {0, 2, 11, 19, 90},sum = 21,i,j,check = 0;
	
	for(i=0;i<4;i++){
		for(j=i+1;j<5;j++){
			if(arr[i]+arr[j]==sum){
				printf("[%d,%d]",i,j);
				check = 1;
				break;
			}
		}
	}
	
	if(check==0)
		printf("[]");
	
	return 0;
}







Python: def two_sum(arr, num):
    i = 0
    j = len(arr) - 1
    while i < j:
        if arr[i] + arr[j] == num:
            return (i, j)
        if arr[i] + arr[j] < num:
            i += 1
        else:
            j -= 1
    return None


numbers = [0, 2, 11, 19, 90]
print(two_sum(numbers, 21))
print(two_sum(numbers, 25))
Task: Two sum

C: #include<stdio.h>

int main()
{
	int arr[5] = {0, 2, 11, 19, 90},sum = 21,i,j,check = 0;
	
	for(i=0;i<4;i++){
		for(j=i+1;j<5;j++){
			if(arr[i]+arr[j]==sum){
				printf("[%d,%d]",i,j);
				check = 1;
				break;
			}
		}
	}
	
	if(check==0)
		printf("[]");
	
	return 0;
}








VB: Option Explicit
Function two_sum(a As Variant, t As Integer) As Variant
    Dim i, j As Integer
    i = 0
    j = UBound(a)
    Do While (i < j)
        If (a(i) + a(j) = t) Then
            two_sum = Array(i, j)
            Exit Function
        ElseIf (a(i) + a(j) < t) Then i = i + 1
        ElseIf (a(i) + a(j) > t) Then j = j - 1
        End If
    Loop
    two_sum = Array()
End Function
Sub prnt(a As Variant)
    If UBound(a) = 1 Then
        Selection.TypeText Text:="(" & a(0) & ", " & a(1) & ")" & vbCrLf
    Else
        Selection.TypeText Text:="()" & vbCrLf
    End If
End Sub
Sub main()
    Call prnt(two_sum(Array(0, 2, 11, 19, 90), 21))
    Call prnt(two_sum(Array(-8, -2, 0, 1, 5, 8, 11), 3))
    Call prnt(two_sum(Array(-3, -2, 0, 1, 5, 8, 11), 17))
    Call prnt(two_sum(Array(-8, -2, -1, 1, 5, 9, 11), 0))
End Sub
Task: Two sum


C++: #include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

pair<int, int> twoSum(vector<int> numbers, int sum) {
	auto m = map<int, int>();
	for (size_t i = 0; i < numbers.size(); ++i) {
		
		auto key = sum - numbers[i];

		if (m.find(key) != m.end()) {
			return make_pair(m[key], i);
		}
		m[numbers[i]] = i;
	}

	return make_pair(-1, -1);
}

int main() {
	auto numbers = vector<int>{ 0, 2, 11, 19, 90 };
	const int sum = 21;

	auto ts = twoSum(numbers, sum);
	if (ts.first != -1) {
		cout << "{" << ts.first << ", " << ts.second << "}" << endl;
	} else {
		cout << "no result" << endl;
	}

	return 0;
}


C#: using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        int[] arr = { 0, 2, 11, 19, 90 };
        const int sum = 21;

        var ts = TwoSum(arr, sum);
        Console.WriteLine(ts != null ? $"{ts[0]}, {ts[1]}" : "no result");

        Console.ReadLine();
    }

    public static int[] TwoSum(int[] numbers, int sum)
    {
        var map = new Dictionary<int, int>();
        for (int i = 0; i < numbers.Length; i++)
        {
            
            var key = sum - numbers[i];
            if (map.ContainsKey(key))
            {
                return new[] { map[key], i };
            }
            map.Add(numbers[i], i);
        }
        return null;
    }
}
Task: Two sum


C++: #include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

pair<int, int> twoSum(vector<int> numbers, int sum) {
	auto m = map<int, int>();
	for (size_t i = 0; i < numbers.size(); ++i) {
		
		auto key = sum - numbers[i];

		if (m.find(key) != m.end()) {
			return make_pair(m[key], i);
		}
		m[numbers[i]] = i;
	}

	return make_pair(-1, -1);
}

int main() {
	auto numbers = vector<int>{ 0, 2, 11, 19, 90 };
	const int sum = 21;

	auto ts = twoSum(numbers, sum);
	if (ts.first != -1) {
		cout << "{" << ts.first << ", " << ts.second << "}" << endl;
	} else {
		cout << "no result" << endl;
	}

	return 0;
}






Python: def two_sum(arr, num):
    i = 0
    j = len(arr) - 1
    while i < j:
        if arr[i] + arr[j] == num:
            return (i, j)
        if arr[i] + arr[j] < num:
            i += 1
        else:
            j -= 1
    return None


numbers = [0, 2, 11, 19, 90]
print(two_sum(numbers, 21))
print(two_sum(numbers, 25))
Task: Two sum


C++: #include <iostream>
#include <map>
#include <tuple>
#include <vector>

using namespace std;

pair<int, int> twoSum(vector<int> numbers, int sum) {
	auto m = map<int, int>();
	for (size_t i = 0; i < numbers.size(); ++i) {
		
		auto key = sum - numbers[i];

		if (m.find(key) != m.end()) {
			return make_pair(m[key], i);
		}
		m[numbers[i]] = i;
	}

	return make_pair(-1, -1);
}

int main() {
	auto numbers = vector<int>{ 0, 2, 11, 19, 90 };
	const int sum = 21;

	auto ts = twoSum(numbers, sum);
	if (ts.first != -1) {
		cout << "{" << ts.first << ", " << ts.second << "}" << endl;
	} else {
		cout << "no result" << endl;
	}

	return 0;
}







VB: Option Explicit
Function two_sum(a As Variant, t As Integer) As Variant
    Dim i, j As Integer
    i = 0
    j = UBound(a)
    Do While (i < j)
        If (a(i) + a(j) = t) Then
            two_sum = Array(i, j)
            Exit Function
        ElseIf (a(i) + a(j) < t) Then i = i + 1
        ElseIf (a(i) + a(j) > t) Then j = j - 1
        End If
    Loop
    two_sum = Array()
End Function
Sub prnt(a As Variant)
    If UBound(a) = 1 Then
        Selection.TypeText Text:="(" & a(0) & ", " & a(1) & ")" & vbCrLf
    Else
        Selection.TypeText Text:="()" & vbCrLf
    End If
End Sub
Sub main()
    Call prnt(two_sum(Array(0, 2, 11, 19, 90), 21))
    Call prnt(two_sum(Array(-8, -2, 0, 1, 5, 8, 11), 3))
    Call prnt(two_sum(Array(-3, -2, 0, 1, 5, 8, 11), 17))
    Call prnt(two_sum(Array(-8, -2, -1, 1, 5, 9, 11), 0))
End Sub
Task: Two sum



C#: using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        int[] arr = { 0, 2, 11, 19, 90 };
        const int sum = 21;

        var ts = TwoSum(arr, sum);
        Console.WriteLine(ts != null ? $"{ts[0]}, {ts[1]}" : "no result");

        Console.ReadLine();
    }

    public static int[] TwoSum(int[] numbers, int sum)
    {
        var map = new Dictionary<int, int>();
        for (int i = 0; i < numbers.Length; i++)
        {
            
            var key = sum - numbers[i];
            if (map.ContainsKey(key))
            {
                return new[] { map[key], i };
            }
            map.Add(numbers[i], i);
        }
        return null;
    }
}






VB: Option Explicit
Function two_sum(a As Variant, t As Integer) As Variant
    Dim i, j As Integer
    i = 0
    j = UBound(a)
    Do While (i < j)
        If (a(i) + a(j) = t) Then
            two_sum = Array(i, j)
            Exit Function
        ElseIf (a(i) + a(j) < t) Then i = i + 1
        ElseIf (a(i) + a(j) > t) Then j = j - 1
        End If
    Loop
    two_sum = Array()
End Function
Sub prnt(a As Variant)
    If UBound(a) = 1 Then
        Selection.TypeText Text:="(" & a(0) & ", " & a(1) & ")" & vbCrLf
    Else
        Selection.TypeText Text:="()" & vbCrLf
    End If
End Sub
Sub main()
    Call prnt(two_sum(Array(0, 2, 11, 19, 90), 21))
    Call prnt(two_sum(Array(-8, -2, 0, 1, 5, 8, 11), 3))
    Call prnt(two_sum(Array(-3, -2, 0, 1, 5, 8, 11), 17))
    Call prnt(two_sum(Array(-8, -2, -1, 1, 5, 9, 11), 0))
End Sub
Task: Two sum







Python: def two_sum(arr, num):
    i = 0
    j = len(arr) - 1
    while i < j:
        if arr[i] + arr[j] == num:
            return (i, j)
        if arr[i] + arr[j] < num:
            i += 1
        else:
            j -= 1
    return None


numbers = [0, 2, 11, 19, 90]
print(two_sum(numbers, 21))
print(two_sum(numbers, 25))


VB: Option Explicit
Function two_sum(a As Variant, t As Integer) As Variant
    Dim i, j As Integer
    i = 0
    j = UBound(a)
    Do While (i < j)
        If (a(i) + a(j) = t) Then
            two_sum = Array(i, j)
            Exit Function
        ElseIf (a(i) + a(j) < t) Then i = i + 1
        ElseIf (a(i) + a(j) > t) Then j = j - 1
        End If
    Loop
    two_sum = Array()
End Function
Sub prnt(a As Variant)
    If UBound(a) = 1 Then
        Selection.TypeText Text:="(" & a(0) & ", " & a(1) & ")" & vbCrLf
    Else
        Selection.TypeText Text:="()" & vbCrLf
    End If
End Sub
Sub main()
    Call prnt(two_sum(Array(0, 2, 11, 19, 90), 21))
    Call prnt(two_sum(Array(-8, -2, 0, 1, 5, 8, 11), 3))
    Call prnt(two_sum(Array(-3, -2, 0, 1, 5, 8, 11), 17))
    Call prnt(two_sum(Array(-8, -2, -1, 1, 5, 9, 11), 0))
End Sub
Task: Two sum



C#: using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        int[] arr = { 0, 2, 11, 19, 90 };
        const int sum = 21;

        var ts = TwoSum(arr, sum);
        Console.WriteLine(ts != null ? $"{ts[0]}, {ts[1]}" : "no result");

        Console.ReadLine();
    }

    public static int[] TwoSum(int[] numbers, int sum)
    {
        var map = new Dictionary<int, int>();
        for (int i = 0; i < numbers.Length; i++)
        {
            
            var key = sum - numbers[i];
            if (map.ContainsKey(key))
            {
                return new[] { map[key], i };
            }
            map.Add(numbers[i], i);
        }
        return null;
    }
}





Python: def two_sum(arr, num):
    i = 0
    j = len(arr) - 1
    while i < j:
        if arr[i] + arr[j] == num:
            return (i, j)
        if arr[i] + arr[j] < num:
            i += 1
        else:
            j -= 1
    return None


numbers = [0, 2, 11, 19, 90]
print(two_sum(numbers, 21))
print(two_sum(numbers, 25))
Task: Equilibrium index

C: #include <stdio.h>
#include <stdlib.h>

int list[] = {-7, 1, 5, 2, -4, 3, 0};

int eq_idx(int *a, int len, int **ret)
{
	int i, sum, s, cnt;
	
        cnt = s = sum = 0;
	*ret = malloc(sizeof(int) * len);

	for (i = 0; i < len; i++)
                sum += a[i];

	for (i = 0; i < len; i++) {
		if (s * 2 + a[i] == sum) {
			(*ret)[cnt] = i;
                        cnt++;
                }
		s += a[i];
	}

        
	*ret = realloc(*ret, cnt * sizeof(int));

	return cnt;
}

int main()
{
	int i, cnt, *idx;
	cnt = eq_idx(list, sizeof(list) / sizeof(int), &idx);

	printf("Found:");
	for (i = 0; i < cnt; i++)
                printf(" %d", idx[i]);
	printf("\n");

	return 0;
}


C++: #include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

template <typename T>
std::vector<size_t> equilibrium(T first, T last)
{
    typedef typename std::iterator_traits<T>::value_type value_t;

    value_t left  = 0;
    value_t right = std::accumulate(first, last, value_t(0));
    std::vector<size_t> result;

    for (size_t index = 0; first != last; ++first, ++index)
    {
        right -= *first;
        if (left == right)
        {
            result.push_back(index);
        }
        left += *first;
    }
    return result;
}

template <typename T>
void print(const T& value)
{
    std::cout << value << "\n";
}

int main() 
{
    const int data[] = { -7, 1, 5, 2, -4, 3, 0 };

    std::vector<size_t> indices(equilibrium(data, data + 7));

    std::for_each(indices.begin(), indices.end(), print<size_t>);
}
Task: Equilibrium index

C: #include <stdio.h>
#include <stdlib.h>

int list[] = {-7, 1, 5, 2, -4, 3, 0};

int eq_idx(int *a, int len, int **ret)
{
	int i, sum, s, cnt;
	
        cnt = s = sum = 0;
	*ret = malloc(sizeof(int) * len);

	for (i = 0; i < len; i++)
                sum += a[i];

	for (i = 0; i < len; i++) {
		if (s * 2 + a[i] == sum) {
			(*ret)[cnt] = i;
                        cnt++;
                }
		s += a[i];
	}

        
	*ret = realloc(*ret, cnt * sizeof(int));

	return cnt;
}

int main()
{
	int i, cnt, *idx;
	cnt = eq_idx(list, sizeof(list) / sizeof(int), &idx);

	printf("Found:");
	for (i = 0; i < cnt; i++)
                printf(" %d", idx[i]);
	printf("\n");

	return 0;
}




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    fmt.Println(ex([]int32{-7, 1, 5, 2, -4, 3, 0}))

    
    
    
    rand.Seed(time.Now().UnixNano())
    verylong := make([]int32, 1e6)
    for i := range verylong {
        verylong[i] = rand.Int31n(1001) - 500
    }
    fmt.Println(ex(verylong))
}

func ex(s []int32) (eq []int) {
    var r, l int64
    for _, el := range s {
        r += int64(el)
    }
    for i, el := range s {
        r -= int64(el)
        if l == r {
            eq = append(eq, i)
        }
        l += int64(el)
    }
    return
}
Task: Equilibrium index

C: #include <stdio.h>
#include <stdlib.h>

int list[] = {-7, 1, 5, 2, -4, 3, 0};

int eq_idx(int *a, int len, int **ret)
{
	int i, sum, s, cnt;
	
        cnt = s = sum = 0;
	*ret = malloc(sizeof(int) * len);

	for (i = 0; i < len; i++)
                sum += a[i];

	for (i = 0; i < len; i++) {
		if (s * 2 + a[i] == sum) {
			(*ret)[cnt] = i;
                        cnt++;
                }
		s += a[i];
	}

        
	*ret = realloc(*ret, cnt * sizeof(int));

	return cnt;
}

int main()
{
	int i, cnt, *idx;
	cnt = eq_idx(list, sizeof(list) / sizeof(int), &idx);

	printf("Found:");
	for (i = 0; i < cnt; i++)
                printf(" %d", idx[i]);
	printf("\n");

	return 0;
}





Java: public class Equlibrium {
	public static void main(String[] args) {
		int[] sequence = {-7, 1, 5, 2, -4, 3, 0};
		equlibrium_indices(sequence);
	}

	public static void equlibrium_indices(int[] sequence){
		
		int totalSum = 0;
		for (int n : sequence) {
			totalSum += n;
		}
		
		int runningSum = 0;
		for (int i = 0; i < sequence.length; i++) {
			int n = sequence[i];
			if (totalSum - runningSum - n == runningSum) {
				System.out.println(i);
			}
			runningSum += n;
		}
	}
}
Task: Equilibrium index

C: #include <stdio.h>
#include <stdlib.h>

int list[] = {-7, 1, 5, 2, -4, 3, 0};

int eq_idx(int *a, int len, int **ret)
{
	int i, sum, s, cnt;
	
        cnt = s = sum = 0;
	*ret = malloc(sizeof(int) * len);

	for (i = 0; i < len; i++)
                sum += a[i];

	for (i = 0; i < len; i++) {
		if (s * 2 + a[i] == sum) {
			(*ret)[cnt] = i;
                        cnt++;
                }
		s += a[i];
	}

        
	*ret = realloc(*ret, cnt * sizeof(int));

	return cnt;
}

int main()
{
	int i, cnt, *idx;
	cnt = eq_idx(list, sizeof(list) / sizeof(int), &idx);

	printf("Found:");
	for (i = 0; i < cnt; i++)
                printf(" %d", idx[i]);
	printf("\n");

	return 0;
}







Python: def eqindex2Pass(data):
    "Two pass"
    suml, sumr, ddelayed = 0, sum(data), 0
    for i, d in enumerate(data):
        suml += ddelayed
        sumr -= d
        ddelayed = d
        if suml == sumr:
            yield i