WeixiangYan/CodeTransOcean huggingface dataset:
Task: Rot-13







Python: >>> u'foo'.encode('rot13')
'sbb'
>>> 'sbb'.decode('rot13')
u'foo'



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. rot-13.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       78  STR-LENGTH   VALUE 100.
 
       78  normal-lower VALUE "abcdefghijklmnopqrstuvwxyz".
       78  rot13-lower  VALUE "nopqrstuvwxyzabcdefghijklm".

       78  normal-upper VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
       78  rot13-upper  VALUE "NOPQRSTUVWXYZABCDEFGHIJKLM".

       LINKAGE SECTION.
       01  in-str       PIC X(STR-LENGTH).
       01  out-str      PIC X(STR-LENGTH).

       PROCEDURE DIVISION USING VALUE in-str, REFERENCE out-str.
           MOVE in-str TO out-str

           INSPECT out-str CONVERTING normal-lower TO rot13-lower
           INSPECT out-str CONVERTING normal-upper TO rot13-upper

           GOBACK
           .
Task: Rot-13








VB: function ROT13 (InputTxt as string) as string
    dim i as integer, ascVal as byte
    Result = ""
    
    for i = 1 to len(InputTxt)
        ascVal = asc(InputTxt[i])

        select case ascVal 
        case 65 to 77, 97 to 109
            Result = Result + chr$(ascVal + 13)
        case 78 to 90, 110 to 122
            Result = Result + chr$(ascVal - 13)
        case else
            Result = Result + chr$(ascVal)
        end select 
    next
end function

Input "Text to encode: "; a$
Print ROT13(a$)
Input "Press a key to end..."; a$


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. rot-13.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       78  STR-LENGTH   VALUE 100.
 
       78  normal-lower VALUE "abcdefghijklmnopqrstuvwxyz".
       78  rot13-lower  VALUE "nopqrstuvwxyzabcdefghijklm".

       78  normal-upper VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
       78  rot13-upper  VALUE "NOPQRSTUVWXYZABCDEFGHIJKLM".

       LINKAGE SECTION.
       01  in-str       PIC X(STR-LENGTH).
       01  out-str      PIC X(STR-LENGTH).

       PROCEDURE DIVISION USING VALUE in-str, REFERENCE out-str.
           MOVE in-str TO out-str

           INSPECT out-str CONVERTING normal-lower TO rot13-lower
           INSPECT out-str CONVERTING normal-upper TO rot13-upper

           GOBACK
           .
Task: Rot-13




Go: package main

import (
    "fmt"
    "strings"
)

func rot13char(c rune) rune {
    if c >= 'a' && c <= 'm' || c >= 'A' && c <= 'M' {
        return c + 13
    } else if c >= 'n' && c <= 'z' || c >= 'N' && c <= 'Z' {
        return c - 13
    }
    return c
}

func rot13(s string) string {
    return strings.Map(rot13char, s)
}

func main() {
    fmt.Println(rot13("nowhere ABJURER"))
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. rot-13.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       78  STR-LENGTH   VALUE 100.
 
       78  normal-lower VALUE "abcdefghijklmnopqrstuvwxyz".
       78  rot13-lower  VALUE "nopqrstuvwxyzabcdefghijklm".

       78  normal-upper VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
       78  rot13-upper  VALUE "NOPQRSTUVWXYZABCDEFGHIJKLM".

       LINKAGE SECTION.
       01  in-str       PIC X(STR-LENGTH).
       01  out-str      PIC X(STR-LENGTH).

       PROCEDURE DIVISION USING VALUE in-str, REFERENCE out-str.
           MOVE in-str TO out-str

           INSPECT out-str CONVERTING normal-lower TO rot13-lower
           INSPECT out-str CONVERTING normal-upper TO rot13-upper

           GOBACK
           .
Task: Rot-13






PHP: echo str_rot13('foo'), "\n";
Task: Rot-13






PHP: echo str_rot13('foo'), "\n";






Ada: with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
with Ada.Strings.Maps; use Ada.Strings.Maps;
with Ada.Command_Line; use Ada.Command_Line;

procedure Rot_13 is
   
   From_Sequence : Character_Sequence := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   Result_Sequence : Character_Sequence := "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM";
   Rot_13_Mapping : Character_Mapping := To_Mapping(From_Sequence, Result_Sequence);

   In_Char : Character;
   Stdio : Stream_Access := Stream(Ada.Text_IO.Standard_Input);
   Stdout : Stream_Access := Stream(Ada.Text_Io.Standard_Output);
   Input : Ada.Text_Io.File_Type;
 
begin
   if Argument_Count > 0 then
      for I in 1..Argument_Count loop
         begin
            Ada.Text_Io.Open(File => Input, Mode => Ada.Text_Io.In_File, Name => Argument(I));
            Stdio := Stream(Input);
             while not Ada.Text_Io.End_Of_File(Input) loop
               In_Char :=Character'Input(Stdio); 
               Character'Output(Stdout, Value(Rot_13_Mapping, In_Char));
            end loop;
            Ada.Text_IO.Close(Input);
         exception
            when Ada.Text_IO.Name_Error =>
               Ada.Text_Io.Put_Line(File => Ada.Text_Io.Standard_Error, Item => "File " & Argument(I) & " is not a file.");
            when Ada.Text_Io.Status_Error =>
               Ada.Text_Io.Put_Line(File => Ada.Text_Io.Standard_Error, Item => "File " & Argument(I) & " is already opened.");
         end;
      end loop;
   else
      while not Ada.Text_Io.End_Of_File loop
         In_Char :=Character'Input(Stdio); 
         Character'Output(Stdout, Value(Rot_13_Mapping, In_Char));
      end loop;
   end if;
end Rot_13;
Task: Rot-13






PHP: echo str_rot13('foo'), "\n";




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. rot-13.

       DATA DIVISION.
       LOCAL-STORAGE SECTION.
       78  STR-LENGTH   VALUE 100.
 
       78  normal-lower VALUE "abcdefghijklmnopqrstuvwxyz".
       78  rot13-lower  VALUE "nopqrstuvwxyzabcdefghijklm".

       78  normal-upper VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
       78  rot13-upper  VALUE "NOPQRSTUVWXYZABCDEFGHIJKLM".

       LINKAGE SECTION.
       01  in-str       PIC X(STR-LENGTH).
       01  out-str      PIC X(STR-LENGTH).

       PROCEDURE DIVISION USING VALUE in-str, REFERENCE out-str.
           MOVE in-str TO out-str

           INSPECT out-str CONVERTING normal-lower TO rot13-lower
           INSPECT out-str CONVERTING normal-upper TO rot13-upper

           GOBACK
           .
Task: Parametric polymorphism



C#: using System;

class BinaryTree<T>
{
    public T value;
    public BinaryTree<T> left;
    public BinaryTree<T> right;

    public BinaryTree(T value)
    {
        this.value = value;
    }

    public BinaryTree<U> Map<U>(Func<T, U> f)
    {
        BinaryTree<U> tree = new BinaryTree<U>(f(this.value));
        if (this.left != null)
        {
            tree.left = this.left.Map(f);
        }
        if (this.right != null)
        {
            tree.right = this.right.Map(f);
        }
        return tree;
    }
}









Ada: generic
   type Element_Type is private;
package Container is
   type Tree is tagged private;
   procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
private
   type Node;
   type Node_Access is access Node;
   type Tree tagged record
      Value : Element_type;
      Left  : Node_Access := null;
      Right : Node_Access := null;
   end record;
end Container;
Task: Parametric polymorphism

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

#define decl_tree_type(T)                                                       \
        typedef struct node_##T##_t node_##T##_t, *node_##T;                    \
        struct node_##T##_t { node_##T left, right; T value; };                 \
                                                                                \
        node_##T node_##T##_new(T v) {                                          \
                node_##T node = malloc(sizeof(node_##T##_t));                   \
                node->value = v;                                                \
                node->left = node->right = 0;                                   \
                return node;                                                    \
        }                                                                       \
        node_##T node_##T##_insert(node_##T root, T v) {                        \
                node_##T n = node_##T##_new(v);                                 \
                while (root) {                                                  \
                        if (root->value < n->value)                             \
                                if (!root->left) return root->left = n;         \
                                else root = root->left;                         \
                        else                                                    \
                                if (!root->right) return root->right = n;       \
                                else root = root->right;                        \
                }                                                               \
                return 0;                                                       \
        }

#define tree_node(T) node_##T
#define node_insert(T, r, x) node_##T##_insert(r, x)
#define node_new(T, x) node_##T##_new(x)

decl_tree_type(double);
decl_tree_type(int);

int main()
{
        int i;
        tree_node(double) root_d = node_new(double, (double)rand() / RAND_MAX);

        for (i = 0; i < 10000; i++)
                node_insert(double, root_d, (double)rand() / RAND_MAX);

        tree_node(int) root_i = node_new(int, rand());
        for (i = 0; i < 10000; i++)
                node_insert(int, root_i, rand());

        return 0;
}











Ada: generic
   type Element_Type is private;
package Container is
   type Tree is tagged private;
   procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
private
   type Node;
   type Node_Access is access Node;
   type Tree tagged record
      Value : Element_type;
      Left  : Node_Access := null;
      Right : Node_Access := null;
   end record;
end Container;
Task: Parametric polymorphism


C++: template<class T> 
class tree
{
  T value;
  tree *left;
  tree *right;
public:
  void replace_all (T new_value);
};










Ada: generic
   type Element_Type is private;
package Container is
   type Tree is tagged private;
   procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
private
   type Node;
   type Node_Access is access Node;
   type Tree tagged record
      Value : Element_type;
      Left  : Node_Access := null;
      Right : Node_Access := null;
   end record;
end Container;
Task: Parametric polymorphism




Go: package main

import "fmt"

func average(c intCollection) float64 {
    var sum, count int
    c.mapElements(func(n int) {
        sum += n
        count++
    })
    return float64(sum) / float64(count)
}

func main() {
    t1 := new(binaryTree)
    t2 := new(bTree)
    a1 := average(t1)
    a2 := average(t2)
    fmt.Println("binary tree average:", a1)
    fmt.Println("b-tree average:", a2)
}

type intCollection interface {
    mapElements(func(int))
}

type binaryTree struct {
    
    left, right bool
}

func (t *binaryTree) mapElements(visit func(int)) {
    
    if t.left == t.right {
        visit(3)
        visit(1)
        visit(4)
    }
}

type bTree struct {
    
    buckets int
}

func (t *bTree) mapElements(visit func(int)) {
    
    if t.buckets >= 0 {
        visit(1)
        visit(5)
        visit(9)
    }
}








Ada: generic
   type Element_Type is private;
package Container is
   type Tree is tagged private;
   procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
private
   type Node;
   type Node_Access is access Node;
   type Tree tagged record
      Value : Element_type;
      Left  : Node_Access := null;
      Right : Node_Access := null;
   end record;
end Container;
Task: Parametric polymorphism





Java: public class Tree<T>{
	private T value;
	private Tree<T> left;
	private Tree<T> right;

	public void replaceAll(T value){
		this.value = value;
		if (left != null)
			left.replaceAll(value);
		if (right != null)
			right.replaceAll(value);
	}
}







Ada: generic
   type Element_Type is private;
package Container is
   type Tree is tagged private;
   procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
private
   type Node;
   type Node_Access is access Node;
   type Tree tagged record
      Value : Element_type;
      Left  : Node_Access := null;
      Right : Node_Access := null;
   end record;
end Container;
Task: Parametric polymorphism








VB: Class BinaryTree(Of T)
    ReadOnly Property Left As BinaryTree(Of T)
    ReadOnly Property Right As BinaryTree(Of T)
    ReadOnly Property Value As T

    Sub New(value As T, Optional left As BinaryTree(Of T) = Nothing, Optional right As BinaryTree(Of T) = Nothing)
        Me.Value = value
        Me.Left = left
        Me.Right = right
    End Sub

    Function Map(Of U)(f As Func(Of T, U)) As BinaryTree(Of U)
        Return New BinaryTree(Of U)(f(Me.Value), Me.Left?.Map(f), Me.Right?.Map(f))
    End Function

    Overrides Function ToString() As String
        Dim sb As New Text.StringBuilder()
        Me.ToString(sb, 0)
        Return sb.ToString()
    End Function

    Private Overloads Sub ToString(sb As Text.StringBuilder, depth As Integer)
        sb.Append(New String(ChrW(AscW(vbTab)), depth))
        sb.AppendLine(Me.Value?.ToString())
        Me.Left?.ToString(sb, depth + 1)
        Me.Right?.ToString(sb, depth + 1)
    End Sub
End Class

Module Program
    Sub Main()
        Dim b As New BinaryTree(Of Integer)(6, New BinaryTree(Of Integer)(5), New BinaryTree(Of Integer)(7))
        Dim b2 As BinaryTree(Of Double) = b.Map(Function(x) x * 0.5)

        Console.WriteLine(b)
        Console.WriteLine(b2)
    End Sub
End Module




Ada: generic
   type Element_Type is private;
package Container is
   type Tree is tagged private;
   procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
private
   type Node;
   type Node_Access is access Node;
   type Tree tagged record
      Value : Element_type;
      Left  : Node_Access := null;
      Right : Node_Access := null;
   end record;
end Container;
Task: Parametric polymorphism

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

#define decl_tree_type(T)                                                       \
        typedef struct node_##T##_t node_##T##_t, *node_##T;                    \
        struct node_##T##_t { node_##T left, right; T value; };                 \
                                                                                \
        node_##T node_##T##_new(T v) {                                          \
                node_##T node = malloc(sizeof(node_##T##_t));                   \
                node->value = v;                                                \
                node->left = node->right = 0;                                   \
                return node;                                                    \
        }                                                                       \
        node_##T node_##T##_insert(node_##T root, T v) {                        \
                node_##T n = node_##T##_new(v);                                 \
                while (root) {                                                  \
                        if (root->value < n->value)                             \
                                if (!root->left) return root->left = n;         \
                                else root = root->left;                         \
                        else                                                    \
                                if (!root->right) return root->right = n;       \
                                else root = root->right;                        \
                }                                                               \
                return 0;                                                       \
        }

#define tree_node(T) node_##T
#define node_insert(T, r, x) node_##T##_insert(r, x)
#define node_new(T, x) node_##T##_new(x)

decl_tree_type(double);
decl_tree_type(int);

int main()
{
        int i;
        tree_node(double) root_d = node_new(double, (double)rand() / RAND_MAX);

        for (i = 0; i < 10000; i++)
                node_insert(double, root_d, (double)rand() / RAND_MAX);

        tree_node(int) root_i = node_new(int, rand());
        for (i = 0; i < 10000; i++)
                node_insert(int, root_i, rand());

        return 0;
}
Task: Parametric polymorphism



C#: using System;

class BinaryTree<T>
{
    public T value;
    public BinaryTree<T> left;
    public BinaryTree<T> right;

    public BinaryTree(T value)
    {
        this.value = value;
    }

    public BinaryTree<U> Map<U>(Func<T, U> f)
    {
        BinaryTree<U> tree = new BinaryTree<U>(f(this.value));
        if (this.left != null)
        {
            tree.left = this.left.Map(f);
        }
        if (this.right != null)
        {
            tree.right = this.right.Map(f);
        }
        return tree;
    }
}
Task: Parametric polymorphism


C++: template<class T> 
class tree
{
  T value;
  tree *left;
  tree *right;
public:
  void replace_all (T new_value);
};
Task: Parametric polymorphism





Java: public class Tree<T>{
	private T value;
	private Tree<T> left;
	private Tree<T> right;

	public void replaceAll(T value){
		this.value = value;
		if (left != null)
			left.replaceAll(value);
		if (right != null)
			right.replaceAll(value);
	}
}
Task: Parametric polymorphism








VB: Class BinaryTree(Of T)
    ReadOnly Property Left As BinaryTree(Of T)
    ReadOnly Property Right As BinaryTree(Of T)
    ReadOnly Property Value As T

    Sub New(value As T, Optional left As BinaryTree(Of T) = Nothing, Optional right As BinaryTree(Of T) = Nothing)
        Me.Value = value
        Me.Left = left
        Me.Right = right
    End Sub

    Function Map(Of U)(f As Func(Of T, U)) As BinaryTree(Of U)
        Return New BinaryTree(Of U)(f(Me.Value), Me.Left?.Map(f), Me.Right?.Map(f))
    End Function

    Overrides Function ToString() As String
        Dim sb As New Text.StringBuilder()
        Me.ToString(sb, 0)
        Return sb.ToString()
    End Function

    Private Overloads Sub ToString(sb As Text.StringBuilder, depth As Integer)
        sb.Append(New String(ChrW(AscW(vbTab)), depth))
        sb.AppendLine(Me.Value?.ToString())
        Me.Left?.ToString(sb, depth + 1)
        Me.Right?.ToString(sb, depth + 1)
    End Sub
End Class

Module Program
    Sub Main()
        Dim b As New BinaryTree(Of Integer)(6, New BinaryTree(Of Integer)(5), New BinaryTree(Of Integer)(7))
        Dim b2 As BinaryTree(Of Double) = b.Map(Function(x) x * 0.5)

        Console.WriteLine(b)
        Console.WriteLine(b2)
    End Sub
End Module
Task: Parametric polymorphism




Go: package main

import "fmt"

func average(c intCollection) float64 {
    var sum, count int
    c.mapElements(func(n int) {
        sum += n
        count++
    })
    return float64(sum) / float64(count)
}

func main() {
    t1 := new(binaryTree)
    t2 := new(bTree)
    a1 := average(t1)
    a2 := average(t2)
    fmt.Println("binary tree average:", a1)
    fmt.Println("b-tree average:", a2)
}

type intCollection interface {
    mapElements(func(int))
}

type binaryTree struct {
    
    left, right bool
}

func (t *binaryTree) mapElements(visit func(int)) {
    
    if t.left == t.right {
        visit(3)
        visit(1)
        visit(4)
    }
}

type bTree struct {
    
    buckets int
}

func (t *bTree) mapElements(visit func(int)) {
    
    if t.buckets >= 0 {
        visit(1)
        visit(5)
        visit(9)
    }
}
Task: Parametric polymorphism



C#: using System;

class BinaryTree<T>
{
    public T value;
    public BinaryTree<T> left;
    public BinaryTree<T> right;

    public BinaryTree(T value)
    {
        this.value = value;
    }

    public BinaryTree<U> Map<U>(Func<T, U> f)
    {
        BinaryTree<U> tree = new BinaryTree<U>(f(this.value));
        if (this.left != null)
        {
            tree.left = this.left.Map(f);
        }
        if (this.right != null)
        {
            tree.right = this.right.Map(f);
        }
        return tree;
    }
}








Fortran:       MODULE SORTSEARCH		

       INTERFACE FIND			
        MODULE PROCEDURE
     1   FINDI4,				
     2   FINDF4,FINDF8,				
     3          FINDTTI2,FINDTTI4		
       END INTERFACE FIND

      CONTAINS
      INTEGER FUNCTION FINDI4(THIS,NUMB,N)	
       USE ASSISTANCE		
       INTENT(IN) THIS,NUMB,N	
       INTEGER*4 THIS,NUMB(1:*)	
       INTEGER N		
       INTEGER L,R,P		
Chop away.
        L = 0			
        R = N + 1		
    1   P = (R - L)/2		
        IF (P.LE.0) THEN	
          FINDI4 = -L		
          RETURN		
        END IF			
        P = P + L		
        IF (THIS - NUMB(P)) 3,4,2	
    2   L = P			
        GO TO 1			
    3   R = P			
        GO TO 1			
Caught it
    4   FINDI4 = P		
      END FUNCTION FINDI4	

      END MODULE SORTSEARCH
Task: Parametric polymorphism


C++: template<class T> 
class tree
{
  T value;
  tree *left;
  tree *right;
public:
  void replace_all (T new_value);
};









Fortran:       MODULE SORTSEARCH		

       INTERFACE FIND			
        MODULE PROCEDURE
     1   FINDI4,				
     2   FINDF4,FINDF8,				
     3          FINDTTI2,FINDTTI4		
       END INTERFACE FIND

      CONTAINS
      INTEGER FUNCTION FINDI4(THIS,NUMB,N)	
       USE ASSISTANCE		
       INTENT(IN) THIS,NUMB,N	
       INTEGER*4 THIS,NUMB(1:*)	
       INTEGER N		
       INTEGER L,R,P		
Chop away.
        L = 0			
        R = N + 1		
    1   P = (R - L)/2		
        IF (P.LE.0) THEN	
          FINDI4 = -L		
          RETURN		
        END IF			
        P = P + L		
        IF (THIS - NUMB(P)) 3,4,2	
    2   L = P			
        GO TO 1			
    3   R = P			
        GO TO 1			
Caught it
    4   FINDI4 = P		
      END FUNCTION FINDI4	

      END MODULE SORTSEARCH