WeixiangYan/CodeTransOcean huggingface dataset:
Task: Pseudo-random numbers_Xorshift star





Java: public class XorShiftStar {
    private static final long MAGIC = Long.parseUnsignedLong("2545F4914F6CDD1D", 16);
    private long state;

    public void seed(long num) {
        state = num;
    }

    public int nextInt() {
        long x;
        int answer;

        x = state;
        x = x ^ (x >>> 12);
        x = x ^ (x << 25);
        x = x ^ (x >>> 27);
        state = x;
        answer = (int) ((x * MAGIC) >> 32);

        return answer;
    }

    public float nextFloat() {
        return (float) Integer.toUnsignedLong(nextInt()) / (1L << 32);
    }

    public static void main(String[] args) {
        var rng = new XorShiftStar();
        rng.seed(1234567);
        System.out.println(Integer.toUnsignedString(rng.nextInt()));
        System.out.println(Integer.toUnsignedString(rng.nextInt()));
        System.out.println(Integer.toUnsignedString(rng.nextInt()));
        System.out.println(Integer.toUnsignedString(rng.nextInt()));
        System.out.println(Integer.toUnsignedString(rng.nextInt()));
        System.out.println();

        int[] counts = {0, 0, 0, 0, 0};
        rng.seed(987654321);
        for (int i = 0; i < 100_000; i++) {
            int j = (int) Math.floor(rng.nextFloat() * 5.0);
            counts[j]++;
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.printf("%d: %d\n", i, counts[i]);
        }
    }
}







Ada: with Interfaces;  use Interfaces;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   const : constant Unsigned_64 := 16#2545_F491_4F6C_DD1D#;
   state : Unsigned_64          := 0;
   Unseeded_Error : exception;

   procedure seed (num : Unsigned_64) is
   begin
      state := num;
   end seed;

   function Next_Int return Unsigned_32 is
      x : Unsigned_64 := state;
   begin
      if state = 0 then
         raise Unseeded_Error;
      end if;

      x     := x xor (x / 2**12);
      x     := x xor (x * 2**25);
      x     := x xor (x / 2**27);
      state := x;
      return Unsigned_32 ((x * const) / 2**32);
   end Next_Int;

   function Next_Float return Long_Float is
   begin
      return Long_Float (Next_Int) / 2.0**32;
   end Next_Float;

   counts : array (0 .. 4) of Natural := (others => 0);
   J      : Natural;
begin
   seed (1_234_567);
   for I in 1 .. 5 loop
      Put_Line (Unsigned_32'Image (Next_Int));
   end loop;

   seed (987_654_321);
   for I in 1 .. 100_000 loop
      J          := Natural (Long_Float'Floor (Next_Float * 5.0));
      counts (J) := counts (J) + 1;
   end loop;

   New_Line;
   for I in counts'Range loop
      Put_Line (I'Image & " :" & counts (I)'Image);
   end loop;

end Main;
Task: Numbers with equal rises and falls





Java: public class EqualRisesFalls {
    public static void main(String[] args) {
        final int limit1 = 200;
        final int limit2 = 10000000;
        System.out.printf("The first %d numbers in the sequence are:\n", limit1);
        int n = 0;
        for (int count = 0; count < limit2; ) {
            if (equalRisesAndFalls(++n)) {
                ++count;
                if (count <= limit1)
                    System.out.printf("%3d%c", n, count % 20 == 0 ? '\n' : ' ');
            }
        }
        System.out.printf("\nThe %dth number in the sequence is %d.\n", limit2, n);
    }

    private static boolean equalRisesAndFalls(int n) {
        int total = 0;
        for (int previousDigit = -1; n > 0; n /= 10) {
            int digit = n % 10;
            if (previousDigit > digit)
                ++total;
            else if (previousDigit >= 0 && previousDigit < digit)
                --total;
            previousDigit = digit;
        }
        return total == 0;
    }
}







Ada: with Ada.Text_Io;
with Ada.Integer_Text_Io;

procedure Equal_Rise_Fall is
   use Ada.Text_Io;

   function Has_Equal_Rise_Fall (Value : Natural) return Boolean is
      Rises : Natural := 0;
      Falls : Natural := 0;
      Image : constant String := Natural'Image (Value);
      Last  : Character := Image (Image'First + 1);
   begin
      for Pos in Image'First + 2 .. Image'Last loop
         if Image (Pos) > Last then
            Rises := Rises + 1;
         elsif Image (Pos) < Last then
            Falls := Falls + 1;
         end if;
         Last := Image (Pos);
      end loop;
      return Rises = Falls;
   end Has_Equal_Rise_Fall;

   Value : Natural := 1;
   Count : Natural := 0;
begin
   loop
      if Has_Equal_Rise_Fall (Value) then
         Count := Count + 1;
         if Count <= 200 then
            Ada.Integer_Text_Io.Put (Value, Width => 5);
            if Count mod 20 = 0 then
               New_Line;
            end if;
         end if;
         if Count = 10_000_000 then
            New_Line;
            Put_Line ("The 10_000_000th: " & Natural'Image (Value));
            exit;
         end if;
      end if;
      Value := Value + 1;
   end loop;
end Equal_Rise_Fall;
Task: Pseudo-random numbers_Combined recursive generator MRG32k3a





Java: public class App {
    private static long mod(long x, long y) {
        long m = x % y;
        if (m < 0) {
            if (y < 0) {
                return m - y;
            } else {
                return m + y;
            }
        }
        return m;
    }

    public static class RNG {
        
        private final long[] a1 = {0, 1403580, -810728};
        private static final long m1 = (1L << 32) - 209;
        private long[] x1;
        
        private final long[] a2 = {527612, 0, -1370589};
        private static final long m2 = (1L << 32) - 22853;
        private long[] x2;
        
        private static final long d = m1 + 1;

        public void seed(long state) {
            x1 = new long[]{state, 0, 0};
            x2 = new long[]{state, 0, 0};
        }

        public long nextInt() {
            long x1i = mod(a1[0] * x1[0] + a1[1] * x1[1] + a1[2] * x1[2], m1);
            long x2i = mod(a2[0] * x2[0] + a2[1] * x2[1] + a2[2] * x2[2], m2);
            long z = mod(x1i - x2i, m1);

            
            x1 = new long[]{x1i, x1[0], x1[1]};
            
            x2 = new long[]{x2i, x2[0], x2[1]};

            return z + 1;
        }

        public double nextFloat() {
            return 1.0 * nextInt() / d;
        }
    }

    public static void main(String[] args) {
        RNG rng = new RNG();

        rng.seed(1234567);
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println(rng.nextInt());
        System.out.println();

        int[] counts = {0, 0, 0, 0, 0};
        rng.seed(987654321);
        for (int i = 0; i < 100_000; i++) {
            int value = (int) Math.floor(rng.nextFloat() * 5.0);
            counts[value]++;
        }
        for (int i = 0; i < counts.length; i++) {
            System.out.printf("%d: %d%n", i, counts[i]);
        }
    }
}







Ada: package MRG32KA is
   type I64 is range -2**63..2**63 - 1;
   m1 : constant I64 := 2**32 - 209;
   m2 : constant I64 := 2**32 - 22853;

   subtype state_value is I64 range 1..m1;
   
   procedure Seed (seed_state : state_value);
   function Next_Int return I64;
   function Next_Float return Long_Float;
end MRG32KA;
Task: Self-describing numbers





Java: public class SelfDescribingNumbers{
    public static boolean isSelfDescribing(int a){
        String s = Integer.toString(a);
        for(int i = 0; i < s.length(); i++){
            String s0 = s.charAt(i) + "";
            int b = Integer.parseInt(s0); 
            int count = 0;
            for(int j = 0; j < s.length(); j++){
                int temp = Integer.parseInt(s.charAt(j) + "");
                if(temp == i){
                    count++;
                }
                if (count > b) return false;
            }
            if(count != b) return false;
        }
        return true;
    }

    public static void main(String[] args){
        for(int i = 0; i < 100000000; i++){
            if(isSelfDescribing(i)){
                System.out.println(i);
             }
        }
    }
}







Ada: with Ada.Text_IO; use Ada.Text_IO;
procedure SelfDesc is
   subtype Desc_Int is Long_Integer range 0 .. 10**10-1;

   function isDesc (innum : Desc_Int) return Boolean is
      subtype S_Int is Natural range 0 .. 10;
      type S_Int_Arr is array (0 .. 9) of S_Int;
      ref, cnt : S_Int_Arr := (others => 0);
      n, digit : S_Int := 0;  num : Desc_Int := innum;
   begin
      loop
         digit := S_Int (num mod 10);
         ref (9 - n) := digit;  cnt (digit) := cnt (digit) + 1;
         num := num / 10; exit when num = 0; n := n + 1;
      end loop;
      return ref (9 - n .. 9) = cnt (0 .. n);
   end isDesc;

begin
   for i in Desc_Int range 1 .. 100_000_000 loop
      if isDesc (i) then
         Put_Line (Desc_Int'Image (i));
      end if;
   end loop;
end SelfDesc;
Task: Changeable words





Java: import java.io.*;
import java.util.*;

public class ChangeableWords {
    public static void main(String[] args) {
        try {
            final String fileName = "unixdict.txt";
            List<String> dictionary = new ArrayList<>();
            try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.length() > 11)
                        dictionary.add(line);
                }
            }
            System.out.printf("Changeable words in %s:\n", fileName);
            int n = 1;
            for (String word1 : dictionary) {
                for (String word2 : dictionary) {
                    if (word1 != word2 && hammingDistance(word1, word2) == 1)
                        System.out.printf("%2d: %-14s -> %s\n", n++, word1, word2);
                }
            }
        } catch (Exception e)  {
            e.printStackTtexture();
        }
    }

    private static int hammingDistance(String str1, String str2) {
        int len1 = str1.length();
        int len2 = str2.length();
        if (len1 != len2)
            return 0;
        int count = 0;
        for (int i = 0; i < len1; ++i) {
            if (str1.charAt(i) != str2.charAt(i))
                ++count;
            
            if (count == 2)
                break;
        }
        return count;
    }
}







Ada: with Ada.Text_Io;
with Ada.Strings.Fixed;
with Ada.Containers.Indefinite_Vectors;

procedure Changeable is
   use Ada.Text_Io, Ada.Strings.Fixed;

   package String_Vectors is
     new Ada.Containers.Indefinite_Vectors (Index_Type   => Positive,
                                            Element_Type => String);

   Filename : constant String := "unixdict.txt";
   File     : File_Type;
   Words    : String_Vectors.Vector;
begin
   Open (File, In_File, Filename);
   while not End_Of_File (File) loop
      declare
         Word : constant String := Get_Line (File);
      begin
         if Word'Length > 11 then
            Words.Append (Word);
         end if;
      end;
   end loop;
   Close (File);

   for Word_Index_1 in Words.First_Index .. Words.Last_Index loop
      for Word_Index_2 in Word_Index_1 + 1 .. Words.Last_Index loop
         declare
            Image  : String (1 .. 14);
            Word_1 : String renames Words (Word_Index_1);
            Word_2 : String renames Words (Word_Index_2);
            Match  : Natural := 0;
         begin
            if Word_1'Length = Word_2'Length then
               for Index in Word_1'Range loop
                  if Word_1 (Index) = Word_2 (Index) then
                     Match := Match + 1;
                  end if;
               end loop;
               if Match = Word_1'Length - 1 then
                  Move (Word_1, Image);
                  Put (Image); Put (" <-> ");
                  Move (Word_2, Image);
                  Put_Line (Image);
               end if;
            end if;
         end;
      end loop;
   end loop;
end Changeable;
Task: Mosaic matrix





Java: 

size(1000,1000);

textSize(50);

for(int i=0;i<10;i++){
  for(int j=0;j<10;j++){
    noFill();
    square(i*100,j*100,100);
    fill(#000000);
    if((i+j)%2==0){
      text("1",i*100+50,j*100+50);
    }
    else{
      text("0",i*100+50,j*100+50);
    } 
  }
}







Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Command_Line;

procedure Mosaic_Matrix is

   type Matrix_Type is array (Positive range <>, Positive range <>) of Character;

   function Mosaic (Length : Natural) return Matrix_Type is
   begin
      return M : Matrix_Type (1 .. Length, 1 .. Length) do
         for Row in M'Range (1) loop
            for Col in M'Range (2) loop
               M (Row, Col) := (if (Row + Col) mod 2 = 0
                                then '1' else '.');
            end loop;
         end loop;
      end return;
   end Mosaic;

   procedure Put (M : Matrix_Type) is
   begin
      for Row in M'Range (1) loop
         for Col in M'Range (2) loop
            Put (' ');
            Put (M (Row, Col));
         end loop;
         New_Line;
      end loop;
   end Put;

begin
   Put (Mosaic (Length => Natural'Value (Ada.Command_Line.Argument (1))));

exception
   when others =>
      Put_Line ("Usage: ./mosaic_matrix <side-length>");

end Mosaic_Matrix;
Task: Input_Output for lines of text





Java: import java.util.Scanner;

public class Main {
	public static void doStuff(String word){
	   System.out.println(word);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());  
		for(int i=0; i<n; i++){		
			String word = in.nextLine();
			doStuff(word);
		}
	}
}







Ada: 







with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   Num_Lines : Integer;
begin
   Get(Num_Lines);
   Skip_Line;
   for I in 1..Num_Lines loop
      Put_Line(Get_Line);
   end loop;
end Main;
Task: Length of an arc between two angles





Java: public static double arcLength(double r, double a1, double a2){
    return (360.0 - Math.abs(a2-a1))*Math.PI/180.0 * r;
}







Ada: with Ada.Text_Io;
with Ada.Numerics;

procedure Calculate_Arc_Length is
   use Ada.Text_Io;

   type Angle_Type is new Float range 0.0 .. 360.0;       
   type Distance   is new Float range 0.0 .. Float'Last;  

   function Major_Arc_Length (Angle_1, Angle_2 : Angle_Type;
                              Radius           : Distance)
                             return Distance
   is
      Pi            : constant := Ada.Numerics.Pi;
      Circumference : constant Distance   := 2.0 * Pi * Radius;
      Major_Angle   : constant Angle_Type := 360.0 - abs (Angle_2 - Angle_1);
      Arc_Length    : constant Distance   :=
        Distance (Major_Angle) / 360.0 * Circumference;
   begin
      return Arc_Length;
   end Major_Arc_Length;

   package Distance_Io is new Ada.Text_Io.Float_Io (Distance);

   Arc_Length : constant Distance := Major_Arc_Length (Angle_1 =>  10.0,
                                                       Angle_2 => 120.0,
                                                       Radius  =>  10.0);
begin
   Put ("Arc length : ");
   Distance_Io.Put (Arc_Length, Exp => 0, Aft => 4);
   New_Line;
end Calculate_Arc_Length;
Task: Shift list elements to left by 3





Java: import java.util.List;
import java.util.Arrays;
import java.util.Collections;

public class RotateLeft {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        System.out.println("original: " + list);
        Collections.rotate(list, -3);
        System.out.println("rotated: " + list);
    }
}







Ada: with Ada.Text_Io;

procedure Shift_Left is

   Shift_Count : constant := 3;

   type List_Type is array (Positive range <>) of Integer;

   procedure Put_List (List : List_Type) is
      use Ada.Text_Io;
   begin
      for V of List loop
         Put (V'Image); Put ("  ");
      end loop;
      New_Line;
   end Put_List;

   Shift : List_Type := (1, 2, 3, 4, 5, 6, 7, 8, 9);
begin
   Put_List (Shift);

   Shift :=
     Shift (Shift'First + Shift_Count .. Shift'Last) &
       Shift (Shift'First .. Shift_Count);

   Put_List (Shift);
end Shift_Left;
Task: Matrix with two diagonals





Java: package example.diagdiag;

public class Program {

    public static void main(String[] args) {
        DiagonalDiagonalMatrix A = new DiagonalDiagonalMatrix(7);
        System.out.println(A);
    }

}

class DiagonalDiagonalMatrix {

    final int n;
    private double[][] a = null;

    public Matrix(int n) {
        this.n = n;
    }

    public double get(int i, int j) {
        if (a == null) {
            return (i == j || i == n - j + 1) ? 1.0 : 0.0;
        } else {
            return a[i - 1][j - 1];
        }
    }















    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                sb.append('\t');
                sb.append(get(i, j));
            }
            sb.append('\n');
        }
        return sb.toString();
    }

}







Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Command_Line;

procedure Matrix_With_Diagonals is

   type Matrix_Type is array (Positive range <>, Positive range <>) of Character;

   function Matrix_X (Length : Natural) return Matrix_Type is
   begin
      return M : Matrix_Type (1 .. Length, 1 .. Length) do
         for Row in M'Range (1) loop
            for Col in M'Range (2) loop
               M (Row, Col) := (if
                                  Row = Col or else
                                  Row = M'Last (2) - (Col - M'First (1))
                                then '1' else '0');
            end loop;
         end loop;
      end return;
   end Matrix_X;

   procedure Put (M : Matrix_Type) is
   begin
      for Row in M'Range (1) loop
         for Col in M'Range (2) loop
            Put (' ');
            Put (M (Row, Col));
         end loop;
         New_Line;
      end loop;
   end Put;

begin
   Put (Matrix_X (Length => Natural'Value (Ada.Command_Line.Argument (1))));

exception
   when others =>
      Put_Line ("Usage: ./matrix_with_diagonals <side-length>");

end Matrix_With_Diagonals;
Task: Draw pixel 2





Java: 

size(640,480);

stroke(#ffff00);

ellipse(random(640),random(480),1,1);







Ada: with Ada.Numerics.Discrete_Random;

with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Events.Events;

procedure Draw_A_Pixel_2 is

   Width   : constant := 640;
   Height  : constant := 480;

   use SDL.C;
   subtype Width_Range  is SDL.C.int range 0 .. Width - 1;
   subtype Height_Range is SDL.C.int range 0 .. Height - 1;
   package Random_Width  is new Ada.Numerics.Discrete_Random (Width_Range);
   package Random_Height is new Ada.Numerics.Discrete_Random (Height_Range);

   Width_Gen  : Random_Width.Generator;
   Height_Gen : Random_Height.Generator;
   Window     : SDL.Video.Windows.Window;
   Renderer   : SDL.Video.Renderers.Renderer;

   procedure Wait is
      use type SDL.Events.Event_Types;
      Event : SDL.Events.Events.Events;
   begin
      loop
         while SDL.Events.Events.Poll (Event) loop
            if Event.Common.Event_Type = SDL.Events.Quit then
               return;
            end if;
         end loop;
         delay 0.100;
      end loop;
   end Wait;

begin
   if not SDL.Initialise (Flags => SDL.Enable_Screen) then
      return;
   end if;
   Random_Width.Reset (Width_Gen);
   Random_Height.Reset (Height_Gen);

   SDL.Video.Windows.Makers.Create (Win      => Window,
                                    Title    => "Draw a pixel 2",
                                    Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
                                    Size     => SDL.Positive_Sizes'(Width, Height),
                                    Flags    => 0);
   SDL.Video.Renderers.Makers.Create (Renderer, Window.Get_Surface);
   Renderer.Set_Draw_Colour ((0, 0, 0, 255));
   Renderer.Fill (Rectangle => (0, 0, Width, Height));
   Renderer.Set_Draw_Colour ((255, 255, 0, 255));
   Renderer.Draw (Point => (X => Random_Width.Random (Width_Gen),
                            Y => Random_Height.Random (Height_Gen)));
   Window.Update_Surface;

   Wait;
   Window.Finalize;
   SDL.Finalise;
end Draw_A_Pixel_2;
Task: Four sides of square





Java: 

size(1000,1000);

textSize(50);

for(int i=0;i<10;i++){
  for(int j=0;j<10;j++){
    noFill();
    square(i*100,j*100,100);
    fill(#000000);
    if(i==0||i==9||j==0||j==9){
      text("1",i*100+50,j*100+50);
    }
    else{
      text("0",i*100+50,j*100+50);
    } 
  }
}







Ada: with Ada.Text_Io;
with Ada.Command_Line;

procedure Four_Sides is

   type Matrix_Type is array (Natural range <>, Natural range <>) of Character;

   function Hollow (Length : Natural) return Matrix_Type is
   begin
      return M : Matrix_Type (1 .. Length, 1 .. Length) do
         for Row in M'Range(1) loop
            for Col in M'Range (2) loop
               M (Row, Col) := (if Row in M'First (1) | M'Last (1) or
                                  Col in M'First (2) | M'Last (2)
                                then '1' else '0');
            end loop;
         end loop;
      end return;
   end Hollow;

   procedure Put (M : Matrix_Type) is
   begin
      for Row in M'Range (1) loop
         for Col in M'Range (2) loop
            Ada.Text_Io.Put (" ");
            Ada.Text_Io.Put (M(Row,Col));
         end loop;
         Ada.Text_Io.New_Line;
      end loop;
   end Put;

begin
   Put (Hollow (Length => Natural'Value (Ada.Command_Line.Argument (1))));
exception
   when others =>
      Ada.Text_Io.Put_Line ("Usage: ./four_sides <length>");
end Four_Sides;
Task: Numeric separator syntax





Java: public class NumericSeparatorSyntax {

    public static void main(String[] args) {
        runTask("Underscore allowed as seperator", 1_000);
        runTask("Multiple consecutive underscores allowed:", 1__0_0_0);
        runTask("Many multiple consecutive underscores allowed:", 1________________________00);
        runTask("Underscores allowed in multiple positions", 1__4__4);
        runTask("Underscores allowed in negative number", -1__4__4);
        runTask("Underscores allowed in floating point number", 1__4__4e-5);
        runTask("Underscores allowed in floating point exponent", 1__4__440000e-1_2);
        
        
        
        
    }
    
    private static void runTask(String description, long n) {
        runTask(description, n, "%d");
    }

    private static void runTask(String description, double n) {
        runTask(description, n, "%3.7f");
    }

    private static void runTask(String description, Number n, String format) {
        System.out.printf("%s:  " + format + "%n", description, n);
    }

}







Ada: with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Main is
   type u64 is mod 2**64;
   pi       : constant Float := 
     3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;
   Trillion : u64            := 1_000_000_000_000;
begin
   Put ("pi : ");
   Put (Item => pi, Exp => 0, Aft => 7);
   New_Line;
   Put_Line ("Trillion : " & Trillion'Image);
end Main;
Task: Hello world_Newbie





Java: public class HelloWorld {
    public static void main(String[] args) {
        
        System.out.println("Hello world!");
    }
}







Ada: procedure Main is

begin
   
   null;
end Main;