WeixiangYan/CodeTransOcean huggingface dataset:
Task: Find words with alternating vowels and consonants




Go: package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "strings"
    "unicode/utf8"
)

func isVowel(c rune) bool { return strings.ContainsRune("aeiou", c) }

func main() {
    wordList := "unixdict.txt"
    b, err := ioutil.ReadFile(wordList)
    if err != nil {
        log.Fatal("Error reading file")
    }
    bwords := bytes.Fields(b)
    var words []string
    for _, bword := range bwords {
        s := string(bword)
        if utf8.RuneCountInString(s) > 9 {
            words = append(words, s)
        }
    }
    count := 0
    fmt.Println("Words with alternate consonants and vowels in", wordList, "\b:\n")
    for _, word := range words {
        found := true
        for i, c := range word {
            if (i%2 == 0 && isVowel(c)) || (i%2 == 1 && !isVowel(c)) {
                found = false
                break
            }
        }
        if !found {
            found = true
            for i, c := range word {
                if (i%2 == 0 && !isVowel(c)) || (i%2 == 1 && isVowel(c)) {
                    found = false
                    break
                }
            }
        }
        if found {
            count++
            fmt.Printf("%2d: %s\n", count, word)
        }
    }
}








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

procedure Find_Alternating is
   use Ada.Text_Io;

   function Is_Vowel (Letter : Character) return Boolean
   is (Ada.Strings.Fixed.Index ("aeiou", "" & Letter) /= 0);

   Filename : constant String := "unixdict.txt";
   File     : File_Type;
begin
   Open (File, In_File, Filename);
   while not End_Of_File (File) loop
      declare
         Word   : constant String  := Get_Line (File);
         Failed : Boolean := False;
         Vowel  : Boolean := not Is_Vowel (Word (Word'First));
      begin
         for Letter of Word loop
            if Vowel = Is_Vowel (letter) then
               Failed := True;
               exit;
            end if;
            Vowel := not Vowel;
         end loop;
         if not Failed and Word'Length > 9 then
            Put_Line (Word);
         end if;
      end;
   end loop;
   Close (File);
end Find_Alternating;
Task: Input_Output for lines of text




Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
)

func main() {
	
	
	
	var lines int
	n, err := fmt.Scanln(&lines)
	if n != 1 || err != nil {
		log.Fatal(err)
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines) 
	for ; scanner.Scan() && lines > 0; lines-- {
		doStuff(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
	
	if lines > 0 {
		log.Fatalln("early", io.EOF)
	}
}

func doStuff(line string) {
	fmt.Println(line)
}








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: Numbers divisible by their individual digits, but not by the product of their digits.




Go: package main

import (
    "fmt"
    "rcu"
)

func main() {
    var res []int
    for n := 1; n < 1000; n++ {
        digits := rcu.Digits(n, 10)
        var all = true
        for _, d := range digits {
            if d == 0 || n%d != 0 {
                all = false
                break
            }
        }
        if all {
            prod := 1
            for _, d := range digits {
                prod *= d
            }
            if prod > 0 && n%prod != 0 {
                res = append(res, n)
            }
        }
    }
    fmt.Println("Numbers < 1000 divisible by their digits, but not by the product thereof:")
    for i, n := range res {
        fmt.Printf("%4d", n)
        if (i+1)%9 == 0 {
            fmt.Println()
        }
    }
    fmt.Printf("\n%d such numbers found\n", len(res))
}








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

procedure Numbers_Divisible is

   function Is_Divisible (N : Natural) return Boolean is

      function To_Decimal (C : Character) return Natural
      is ( Character'Pos (C) - Character'Pos ('0'));

      Image : constant String := N'Image;
      Digit : Natural;
      Prod  : Natural := 1;
   begin
      for A in Image'First + 1 .. Image'Last loop
         Digit := To_Decimal (Image (A));
         if Digit = 0 then
            return False;
         end if;
         if N mod Digit /= 0 then
            return False;
         end if;
         Prod := Prod * Digit;
      end loop;
      return N mod Prod /= 0;
   end Is_Divisible;

   Count : Natural := 0;
begin
   for N in 1 .. 999 loop
      if Is_Divisible (N) then
         Count := Count + 1;
         Ada.Integer_Text_Io.Put (N, Width => 5);
         if Count mod 15 = 0 then
            Ada.Text_Io.New_Line;
         end if;
      end if;
   end loop;
end Numbers_Divisible;
Task: Largest five adjacent number




Go: package main

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

func main() {
    rand.Seed(time.Now().UnixNano())
    var sb strings.Builder
    for i := 0; i < 1000; i++ {
        sb.WriteByte(byte(rand.Intn(10) + 48))
    }
    number := sb.String()
    for i := 99999; i >= 0; i-- {
        quintet := fmt.Sprintf("%05d", i)
        if strings.Contains(number, quintet) {
            ci := rcu.Commatize(i)
            fmt.Printf("The largest  number formed from 5 adjacent digits (%s) is: %6s\n", quintet, ci)
            break
        }
    }
    for i := 0; i <= 99999; i++ {
        quintet := fmt.Sprintf("%05d", i)
        if strings.Contains(number, quintet) {
            ci := rcu.Commatize(i)
            fmt.Printf("The smallest number formed from 5 adjacent digits (%s) is: %6s\n", quintet, ci)
            return
        }
    }
}








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

procedure Adjacent_Numbers is

   Adjacent_Length : constant := 5;

   subtype Digit is Character range '0' .. '9';
   package Random_Digits
   is new Ada.Numerics.Discrete_Random (Digit);

   Gen   : Random_Digits.Generator;
   Line  : String (1 .. 1000);
   Large : Natural := Natural'First;
   Small : Natural := Natural'Last;
begin
   Random_Digits.Reset (Gen);
   Line := (others => Random_Digits.Random (Gen));

   for I in Line'First .. Line'Last - Adjacent_Length + 1 loop
      declare
         Window : String renames Line (I .. I + Adjacent_Length - 1);
      begin
         Large := Natural'Max (Large, Natural'Value (Window));
         Small := Natural'Min (Small, Natural'Value (Window));
      end;
   end loop;
   Ada.Text_Io.Put_Line ("The largest number : " & Natural'Image (Large));
   Ada.Text_Io.Put_Line ("The smallest number: " & Natural'Image (Small));
end Adjacent_Numbers;
Task: Length of an arc between two angles




Go: package main

import (
    "fmt"
    "math"
)

func arcLength(radius, angle1, angle2 float64) float64 {
    return (360 - math.Abs(angle2-angle1)) * math.Pi * radius / 180
}

func main() {
    fmt.Println(arcLength(10, 10, 120))
}








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: Last list item




Go: package main

import (
    "fmt"
    "sort"
)

func main() {
    a := []int{6, 81, 243, 14, 25, 49, 123, 69, 11}
    for len(a) > 1 {
        sort.Ints(a)
        fmt.Println("Sorted list:", a)
        sum := a[0] + a[1]
        fmt.Printf("Two smallest: %d + %d = %d\n", a[0], a[1], sum)
        a = append(a, sum)
        a = a[2:]
    }
    fmt.Println("Last item is", a[0], "\b.")
}








Ada: with Ada.Text_IO;
with Ada.Containers.Vectors;

procedure Last_List_Item_Sorted is

   package Integer_IO is new Ada.Text_IO.Integer_IO (Integer);

   package Vectors is
     new Ada.Containers.Vectors (Index_Type   => Positive,
                                 Element_Type => Integer);
   package Vector_Sorting is
     new Vectors.Generic_Sorting;
   use Ada.Containers, Vectors, Ada.Text_IO, Integer_IO;

   procedure Put (List : Vector) is
   begin
      Put ("[");
      for V of List loop
         Put (" "); Put (V, Width => 0);
      end loop;
      Put ("]");
   end Put;

   List : Vector := 6 & 81 & 243 & 14 & 25 & 49 & 123 & 69 & 11;

begin
   Default_Width := 0;
   while List.Length >= 2 loop
      Vector_Sorting.Sort (List);
      Put (List);
      declare
         Small_1 : constant Integer := List (List.First_Index + 0);
         Small_2 : constant Integer := List (List.First_Index + 1);
         Sum     : constant Integer := Small_1 + Small_2;
      begin
         List.Delete_First (2);
         Put (". Smallest: ");  Put (Small_1);  Put (" and ");  Put (Small_2);
         Put (". Sum: ");  Put (Sum);  New_Line;
         List.Append (Sum);
      end;
   end loop;
   Put (List); New_Line;
end Last_List_Item_Sorted;
Task: Shift list elements to left by 3




Go: package main

import "fmt"


func lshift(l []int) {
    n := len(l)
    if n < 2 {
        return
    }
    f := l[0]
    for i := 0; i < n-1; i++ {
        l[i] = l[i+1]
    }
    l[n-1] = f
}

func main() {
    l := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println("Original list     :", l)
    for i := 0; i < 3; i++ {
        lshift(l)
    }
    fmt.Println("Shifted left by 3 :", l)
}








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: Numbers with same digit set in base 10 and base 16




Go: package main

import (
    "fmt"
    "rcu"
    "strconv"
)

func equalSets(s1, s2 map[rune]bool) bool {
    if len(s1) != len(s2) {
        return false
    }
    for k, _ := range s1 {
        _, ok := s2[k]
        if !ok {
            return false
        }
    }
    return true
}

func main() {
    const limit = 100_000
    count := 0
    fmt.Println("Numbers under 100,000 which use the same digits in decimal or hex:")
    for n := 0; n < limit; n++ {
        h := strconv.FormatInt(int64(n), 16)
        hs := make(map[rune]bool)
        for _, c := range h {
            hs[c] = true
        }
        ns := make(map[rune]bool)
        for _, c := range strconv.Itoa(n) {
            ns[c] = true
        }
        if equalSets(hs, ns) {
            count++
            fmt.Printf("%6s ", rcu.Commatize(n))
            if count%10 == 0 {
                fmt.Println()
            }
        }
    }
    fmt.Printf("\n\n%d such numbers found.\n", count)
}








Ada: with Ada.Text_Io;

procedure Same_Digits is

   Columns : constant := 10;
   Width   : constant := 8;

   type Set_Type is array (0 .. 15) of Boolean;

   function Digit_Set_Of (N : Natural; Base : Natural) return Set_Type is
      Nn  : Natural  := N;
      Set : Set_Type := (others => False);
   begin
      while Nn /= 0 loop
         Set (Nn mod Base) := True;
         Nn := Nn / Base;
      end loop;
      return Set;
   end Digit_Set_Of;

   package Natural_Io is new Ada.Text_Io.Integer_Io (Natural);
   use Ada.Text_Io, Natural_Io;

   Count : Natural := 0;
begin
   for N in 0 .. 99_999 loop
      if Digit_Set_Of (N, Base => 10) = Digit_Set_Of (N, Base => 16) then
         Count := Count + 1;
         Put (N, Width => Width);
         if Count mod Columns = 0 then
            New_Line;
         end if;
      end if;
   end loop;
   New_Line;
   Put ("Total numbers: "); Put (Count, Width => 3); New_Line;
end Same_Digits;
Task: Air mass




Go: package main

import (
    "fmt"
    "math"
)

const (
    RE  = 6371000 
    DD  = 0.001   
    FIN = 1e7     
)


func rho(a float64) float64 { return math.Exp(-a / 8500) }


func radians(degrees float64) float64 { return degrees * math.Pi / 180 }




func height(a, z, d float64) float64 {
    aa := RE + a
    hh := math.Sqrt(aa*aa + d*d - 2*d*aa*math.Cos(radians(180-z)))
    return hh - RE
}


func columnDensity(a, z float64) float64 {
    sum := 0.0
    d := 0.0
    for d < FIN {
        delta := math.Max(DD, DD*d) 
        sum += rho(height(a, z, d+0.5*delta)) * delta
        d += delta
    }
    return sum
}

func airmass(a, z float64) float64 {
    return columnDensity(a, z) / columnDensity(a, 0)
}

func main() {
    fmt.Println("Angle     0 m              13700 m")
    fmt.Println("------------------------------------")
    for z := 0; z <= 90; z += 5 {
        fz := float64(z)
        fmt.Printf("%2d      %11.8f      %11.8f\n", z, airmass(0, fz), airmass(13700, fz))
    }
}








Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   subtype double is Long_Float;
   package double_io is new Ada.Text_IO.Float_IO (double);
   use double_io;
   package Elementary_Double is new Ada.Numerics.Generic_Elementary_Functions
     (Float_Type => double);
   use Elementary_Double;

   
   Deg : constant := 0.017_453_292_519_943_295_769_236_907_684_886_127_134;

   
   Re : constant := 6_371_000.0;

   
   Dd : constant := 0.001;

   
   Fin : constant := 10_000_000.0;

   function rho (a : double) return double is (Exp (-a / 8_500.0));

   function height (a : double; z : double; d : double) return double is
      aa : double := Re + a;
      hh : double :=
        Sqrt (aa * aa + d * d - 2.0 * d * aa * Cos ((180.0 - z) * Deg));
   begin
      return hh - Re;
   end height;

   function column_density (a : double; z : double) return double is
      sum     : double := 0.0;
      d       : double := 0.0;
      d_delta : double;
   begin
      while d < Fin loop
         
         d_delta := Dd * d;
         if d_delta < Dd then
            d_delta := Dd;
         end if;
         sum := sum + rho (height (a, z, d + 0.5 * d_delta)) * d_delta;
         d   := d + d_delta;
      end loop;
      return sum;
   end column_density;

   function air_mass (a : double; z : double) return double is
     (column_density (a, z) / column_density (a, 0.0));

   z : double := 0.0;
begin
   Put_Line ("Angle     0 m              13700 m");
   Put_Line ("
   while z <= 90.0 loop
      Put(Item => Integer(z), Width => 2);
      Put (Item => air_mass (0.0, z), Fore => 8, Aft => 8, Exp => 0);
      Put (Item => air_mass (13_700.0, z), Fore => 8, Aft => 8, Exp => 0);
      New_Line;
      z := z + 5.0;
   end loop;

end Main;
Task: Matrix with two diagonals




Go: package main

import "fmt"

func specialMatrix(n uint) {
    for i := uint(0); i < n; i++ {
        for j := uint(0); j < n; j++ {
            if i == j || i+j == n-1 {
                fmt.Printf("%d ", 1)
            } else {
                fmt.Printf("%d ", 0)
            }
        }
        fmt.Println()
    }
}

func main() {
    specialMatrix(8) 
    fmt.Println()
    specialMatrix(9) 
}








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




Go: package main

import (
    "fmt"
    "image"
    "image/color"
    "image/draw"
    "math/rand"
    "time"
)

func main() {
    rect := image.Rect(0, 0, 640, 480)
    img := image.NewRGBA(rect)

    
    blue := color.RGBA{0, 0, 255, 255}
    draw.Draw(img, rect, &image.Uniform{blue}, image.ZP, draw.Src)

    
    yellow := color.RGBA{255, 255, 0, 255}
    width := img.Bounds().Dx()
    height := img.Bounds().Dy()
    rand.Seed(time.Now().UnixNano())
    x := rand.Intn(width)
    y := rand.Intn(height)
    img.Set(x, y, yellow)

    
    cmap := map[color.Color]string{blue: "blue", yellow: "yellow"}
    for i := 0; i < width; i++ {
        for j := 0; j < height; j++ {
            c := img.At(i, j)
            if cmap[c] == "yellow" {
                fmt.Printf("The color of the pixel at (%d, %d) is yellow\n", i, j)
            }
        }
    }
}








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: Words containing _the_ substring




Go: package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "strings"
    "unicode/utf8"
)

func main() {
    wordList := "unixdict.txt"
    b, err := ioutil.ReadFile(wordList)
    if err != nil {
        log.Fatal("Error reading file")
    }
    bwords := bytes.Fields(b)
    var words []string
    for _, bword := range bwords {
        s := string(bword)
        if utf8.RuneCountInString(s) > 11 {
            words = append(words, s)
        }
    }
    count := 0
    fmt.Println("Words containing 'the' having a length > 11 in", wordList, "\b:")
    for _, word := range words {
        if strings.Contains(word, "the") {
            count++
            fmt.Printf("%2d: %s\n", count, word)
        }
    }
}








Ada: with Ada.Text_IO;            use Ada.Text_IO;
with Ada.Strings.Fixed;      use Ada.Strings.Fixed;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;

procedure Main is
   type col_count is mod 6;
   package AF renames Ada.Strings.Fixed;

   file_name : String             := "unixdict.txt";
   The_File  : File_Type;
   Inpt_Str  : String (1 .. 40);
   Length    : Natural;
   pattern   : String             := "the";
   Columns   : col_count          := 0;
   Tally     : Natural            := 0;
   sep       : constant Character := HT;
begin

   Open (File => The_File, Mode => In_File, Name => file_name);

   while not End_Of_File (The_File) loop
      Get_Line (File => The_File, Item => Inpt_Str, Last => Length);

      if Length > 11
        and then
          AF.Count (Source => Inpt_Str (1 .. Length), Pattern => pattern) > 0
      then
         Tally   := Tally + 1;
         Columns := Columns + 1;
         Put (Inpt_Str (1 .. Length) & sep);
         if Columns = 0 then
            New_Line;
         end if;
      end if;
   end loop;
   New_Line;
   Put_Line ("Found" & Tally'Image & " ""the"" words");
   Close (The_File);
end Main;
Task: Here document




Go: var m = `    leading spaces

and blank lines`








Ada: with Ada.Containers.Indefinite_Vectors, Ada.Text_IO;

procedure Here_Doc is

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

   use type String_Vec.Vector;

   Document: String_Vec.Vector := String_Vec.Empty_Vector
    & "This is a vector of strings with the following properties:"
    & "  - indention is preserved, and"
    & "  - a quotation mark '""' must be ""escaped"" by a double-quote '""""'.";
begin
   Document := Document & "Have fun!";
   for I in Document.First_Index .. Document.Last_Index loop
      Ada.Text_IO.Put_Line(Document.Element(I));
   end loop;
end Here_Doc;