WeixiangYan/CodeTransOcean huggingface dataset:
Task: Collections




Go: package main

import "fmt"

func main() {
    var a []interface{}
    a = append(a, 3)
    a = append(a, "apples", "oranges")
    fmt.Println(a)
}
Task: Collections



C#: 
int[] intArray = new int[5] { 1, 2, 3, 4, 5 };

int[] intArray = new int[]{ 1, 2, 3, 4, 5 };

int[] intArray = { 1, 2, 3, 4, 5 };


string[] stringArr = new string[5];
stringArr[0] = "string";








Fortran:  REAL A(36)   
  A(1) = 1           
  A(2) = 3*A(1) + 5
Task: Collections


C++: int a[5]; 
a[0] = 1; 

int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; 

#include <string>
std::string strings[4]; 
                        









Fortran:  REAL A(36)   
  A(1) = 1           
  A(2) = 3*A(1) + 5
Task: Collections

C: #define cSize( a )  ( sizeof(a)/sizeof(a[0]) ) 
int ar[10];               
ar[0] = 1;                
ar[1] = 2;

int* p;                   
for (p=ar;                
       p<(ar+cSize(ar));  
       p++) {             
  printf("%d\n",*p);      
}                         










Fortran:  REAL A(36)   
  A(1) = 1           
  A(2) = 3*A(1) + 5
Task: Collections





Java: List arrayList = new ArrayList();
arrayList.add(new Integer(0));

arrayList.add(0); 



List<Integer> myarrlist = new ArrayList<Integer>();


int sum;
for(int i = 0; i < 10; i++) {
    myarrlist.add(i);
}






Fortran:  REAL A(36)   
  A(1) = 1           
  A(2) = 3*A(1) + 5
Task: Collections







Python: collection = [0, '1']                 
x = collection[0]                     
collection.append(2)                  
collection.insert(0, '-1')            
y = collection[0]                     
collection.extend([2,'3'])            
collection += [2,'3']                 
collection[2:6]                       
len(collection)                       
collection = (0, 1)                   
collection[:]                         
collection[-4:-1]                     
collection[::2]                       
collection="some string"              
x = collection[::-1]                  
collection[::2] == "some string"[::2] 
collection.__getitem__(slice(0,len(collection),2))  
collection = {0: "zero", 1: "one"}    
collection['zero'] = 2                
collection = set([0, '1'])            




Fortran:  REAL A(36)   
  A(1) = 1           
  A(2) = 3*A(1) + 5
Task: Collections








VB: Dim coll As New Collection
coll.Add "apple"
coll.Add "banana"



Fortran:  REAL A(36)   
  A(1) = 1           
  A(2) = 3*A(1) + 5
Task: Collections






PHP: <?php
$a = array();
# add elements "at the end"
array_push($a, 55, 10, 20);
print_r($a);
# using an explicit key
$a['one'] = 1;
$a['two'] = 2;
print_r($a);
?>





Fortran:  REAL A(36)   
  A(1) = 1           
  A(2) = 3*A(1) + 5
Task: Collections

C: #define cSize( a )  ( sizeof(a)/sizeof(a[0]) ) 
int ar[10];               
ar[0] = 1;                
ar[1] = 2;

int* p;                   
for (p=ar;                
       p<(ar+cSize(ar));  
       p++) {             
  printf("%d\n",*p);      
}                         









COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Collections



C#: 
int[] intArray = new int[5] { 1, 2, 3, 4, 5 };

int[] intArray = new int[]{ 1, 2, 3, 4, 5 };

int[] intArray = { 1, 2, 3, 4, 5 };


string[] stringArr = new string[5];
stringArr[0] = "string";







COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Collections


C++: int a[5]; 
a[0] = 1; 

int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 }; 

#include <string>
std::string strings[4]; 
                        








COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Collections





Java: List arrayList = new ArrayList();
arrayList.add(new Integer(0));

arrayList.add(0); 



List<Integer> myarrlist = new ArrayList<Integer>();


int sum;
for(int i = 0; i < 10; i++) {
    myarrlist.add(i);
}





COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Collections







Python: collection = [0, '1']                 
x = collection[0]                     
collection.append(2)                  
collection.insert(0, '-1')            
y = collection[0]                     
collection.extend([2,'3'])            
collection += [2,'3']                 
collection[2:6]                       
len(collection)                       
collection = (0, 1)                   
collection[:]                         
collection[-4:-1]                     
collection[::2]                       
collection="some string"              
x = collection[::-1]                  
collection[::2] == "some string"[::2] 
collection.__getitem__(slice(0,len(collection),2))  
collection = {0: "zero", 1: "one"}    
collection['zero'] = 2                
collection = set([0, '1'])            



COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Collections








VB: Dim coll As New Collection
coll.Add "apple"
coll.Add "banana"


COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Collections




Go: package main

import "fmt"

func main() {
    var a []interface{}
    a = append(a, 3)
    a = append(a, "apples", "oranges")
    fmt.Println(a)
}






COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Collections






PHP: <?php
$a = array();
# add elements "at the end"
array_push($a, 55, 10, 20);
print_r($a);
# using an explicit key
$a['one'] = 1;
$a['two'] = 2;
print_r($a);
?>
Task: Collections






PHP: <?php
$a = array();
# add elements "at the end"
array_push($a, 55, 10, 20);
print_r($a);
# using an explicit key
$a['one'] = 1;
$a['two'] = 2;
print_r($a);
?>






Ada: procedure Array_Collection is

   A : array (-3 .. -1) of Integer := (1, 2, 3);
   
begin
   
   A (-3) := 3;
   A (-2) := 2;
   A (-1) := 1;
   
end Array_Collection;
Task: Collections






PHP: <?php
$a = array();
# add elements "at the end"
array_push($a, 55, 10, 20);
print_r($a);
# using an explicit key
$a['one'] = 1;
$a['two'] = 2;
print_r($a);
?>




COBOL:        identification division.
       program-id. collections.

       data division.
       working-storage section.
       01 sample-table.
          05 sample-record occurs 1 to 3 times depending on the-index.
             10 sample-alpha   pic x(4).
             10 filler         pic x value ":".
             10 sample-number  pic 9(4).
             10 filler         pic x value space.
       77 the-index            usage index.

       procedure division.
       collections-main.

       set the-index to 3
       move 1234 to sample-number(1)
       move "abcd" to sample-alpha(1)

       move "test" to sample-alpha(2)

       move 6789 to sample-number(3)
       move "wxyz" to sample-alpha(3)

       display "sample-table    : " sample-table
       display "sample-number(1): " sample-number(1)
       display "sample-record(2): " sample-record(2)
       display "sample-number(3): " sample-number(3)

      
       set the-index down by 1
       display "sample-table    : " sample-table
       display "sample-number(3): " sample-number(3)

       goback.
       end program collections.
Task: Matrix-exponentiation operator



C#: using System;
using System.Collections;
using System.Collections.Generic;
using static System.Linq.Enumerable;

public static class MatrixExponentation
{
    public static double[,] Identity(int size) {
        double[,] matrix = new double[size, size];
        for (int i = 0; i < size; i++) matrix[i, i] = 1;
        return matrix;
    }

    public static double[,] Multiply(this double[,] left, double[,] right) {
        if (left.ColumnCount() != right.RowCount()) throw new ArgumentException();
        double[,] m = new double[left.RowCount(), right.ColumnCount()];
        foreach (var (row, column) in from r in Range(0, m.RowCount()) from c in Range(0, m.ColumnCount()) select (r, c)) {
            m[row, column] = Range(0, m.RowCount()).Sum(i => left[row, i] * right[i, column]);
        }
        return m;
    }

    public static double[,] Pow(this double[,] matrix, int exp) {
        if (matrix.RowCount() != matrix.ColumnCount()) throw new ArgumentException("Matrix must be square.");
        double[,] accumulator = Identity(matrix.RowCount());
        for (int i = 0; i < exp; i++) {
            accumulator = accumulator.Multiply(matrix);
        }
        return accumulator;
    }

    private static int RowCount(this double[,] matrix) => matrix.GetLength(0);
    private static int ColumnCount(this double[,] matrix) => matrix.GetLength(1);

    private static void Print(this double[,] m) {
        foreach (var row in Rows()) {
            Console.WriteLine("[ " + string.Join("   ", row) + " ]");
        }
        Console.WriteLine();

        IEnumerable<IEnumerable<double>> Rows() =>
            Range(0, m.RowCount()).Select(row => Range(0, m.ColumnCount()).Select(column => m[row, column]));
    }

    public static void Main() {
        var matrix = new double[,] {
            { 3, 2 },
            { 2, 1 }
        };
        
        matrix.Pow(0).Print();
        matrix.Pow(1).Print();
        matrix.Pow(2).Print();
        matrix.Pow(3).Print();
        matrix.Pow(4).Print();
        matrix.Pow(50).Print();
    }

}
Task: Matrix-exponentiation operator


C++: #include <complex>
#include <cmath>
#include <iostream>
using namespace std;

template<int MSize = 3, class T = complex<double> >
class SqMx {
  typedef T Ax[MSize][MSize];
  typedef SqMx<MSize, T> Mx;

private:
  Ax a;
  SqMx() { }

public:
  SqMx(const Ax &_a) { 
    for (int r = 0; r < MSize; r++)
      for (int c = 0; c < MSize; c++)
        a[r][c] = _a[r][c];
  }

  static Mx identity() {
    Mx m;
    for (int r = 0; r < MSize; r++)
      for (int c = 0; c < MSize; c++)
        m.a[r][c] = (r == c ? 1 : 0);
    return m;
  }

  friend ostream &operator<<(ostream& os, const Mx &p)
  { 
    for (int i = 0; i < MSize; i++) {
      for (int j = 0; j < MSize; j++)
        os << p.a[i][j] << ',';
      os << endl;
    }
    return os;
  }

  Mx operator*(const Mx &b) {
    Mx d;
    for (int r = 0; r < MSize; r++)
      for (int c = 0; c < MSize; c++) {
        d.a[r][c] = 0;
        for (int k = 0; k < MSize; k++)
          d.a[r][c] += a[r][k] * b.a[k][c];
      }
    return d;
  }
Task: Matrix-exponentiation operator







Python: >>> from operator import mul
>>> def matrixMul(m1, m2):
  return map(
    lambda row:
      map(
        lambda *column:
          sum(map(mul, row, column)),
        *m2),
    m1)

>>> def identity(size):
	size = range(size)
	return [[(i==j)*1 for i in size] for j in size]

>>> def matrixExp(m, pow):
	assert pow>=0 and int(pow)==pow, "Only non-negative, integer powers allowed"
	accumulator = identity(len(m))
	for i in range(pow):
		accumulator = matrixMul(accumulator, m)
	return accumulator

>>> def printtable(data):
	for row in data:
		print ' '.join('%-5s' % ('%s' % cell) for cell in row)

		
>>> m = [[3,2], [2,1]]
>>> for i in range(5):
	print '\n%i:' % i
	printtable( matrixExp(m, i) )

	

0:
1     0    
0     1    

1:
3     2    
2     1    

2:
13    8    
8     5    

3:
55    34   
34    21   

4:
233   144  
144   89   
>>> printtable( matrixExp(m, 10) )
1346269 832040
832040 514229
>>>
Task: Matrix-exponentiation operator








VB: Option Base 1
Private Function Identity(n As Integer) As Variant
    Dim I() As Variant
    ReDim I(n, n)
    For j = 1 To n
        For k = 1 To n
            I(j, k) = 0
        Next k
    Next j
    For j = 1 To n
        I(j, j) = 1
    Next j
    Identity = I
End Function
 Function MatrixExponentiation(ByVal x As Variant, ByVal n As Integer) As Variant
    If n < 0 Then
        x = WorksheetFunction.MInverse(x)
        n = -n
    End If
    If n = 0 Then
        MatrixExponentiation = Identity(UBound(x))
        Exit Function
    End If
    Dim y() As Variant
    y = Identity(UBound(x))
    Do While n > 1
        If n Mod 2 = 0 Then
            x = WorksheetFunction.MMult(x, x)
            n = n / 2
        Else
            y = WorksheetFunction.MMult(x, y)
            x = WorksheetFunction.MMult(x, x)
            n = (n - 1) / 2
        End If
    Loop
    MatrixExponentiation = WorksheetFunction.MMult(x, y)
End Function
Public Sub pp(x As Variant)
    For i_ = 1 To UBound(x)
        For j_ = 1 To UBound(x)
            Debug.Print x(i_, j_),
        Next j_
        Debug.Print
    Next i_
End Sub
Public Sub main()
    M2 = [{3,2;2,1}]
    M3 = [{1,2,0;0,3,1;1,0,0}]
    pp MatrixExponentiation(M2, -1)
    Debug.Print
    pp MatrixExponentiation(M2, 0)
    Debug.Print
    pp MatrixExponentiation(M2, 10)
    Debug.Print
    pp MatrixExponentiation(M3, 10)
End Sub
Task: Matrix-exponentiation operator




Go: package main

import "fmt"

type vector = []float64
type matrix []vector

func (m1 matrix) mul(m2 matrix) matrix {
    rows1, cols1 := len(m1), len(m1[0])
    rows2, cols2 := len(m2), len(m2[0])
    if cols1 != rows2 {
        panic("Matrices cannot be multiplied.")
    }
    result := make(matrix, rows1)
    for i := 0; i < rows1; i++ {
        result[i] = make(vector, cols2)
        for j := 0; j < cols2; j++ {
            for k := 0; k < rows2; k++ {
                result[i][j] += m1[i][k] * m2[k][j]
            }
        }
    }
    return result
}

func identityMatrix(n int) matrix {
    if n < 1 {
        panic("Size of identity matrix can't be less than 1")
    }
    ident := make(matrix, n)
    for i := 0; i < n; i++ {
        ident[i] = make(vector, n)
        ident[i][i] = 1
    }
    return ident
}

func (m matrix) pow(n int) matrix {
    le := len(m)
    if le != len(m[0]) {
        panic("Not a square matrix")
    }
    switch {
    case n < 0:
        panic("Negative exponents not supported")
    case n == 0:
        return identityMatrix(le)
    case n == 1:
        return m
    }
    pow := identityMatrix(le)
    base := m
    e := n
    for e > 0 {
        if (e & 1) == 1 {
            pow = pow.mul(base)
        }
        e >>= 1
        base = base.mul(base)
    }
    return pow
}

func main() {
    m := matrix{{3, 2}, {2, 1}}
    for i := 0; i <= 10; i++ {
        fmt.Println("** Power of", i, "**")
        fmt.Println(m.pow(i))
        fmt.Println()
    }
}