WeixiangYan/CodeTransOcean huggingface dataset:
Task: Strange numbers




Go: package main

import "fmt"

func isPrime(n int) bool {
    if n < 0 {
        n = -n
    }
    return n == 2 || n == 3 || n == 5 || n == 7
}

func main() {
    count := 0
    var d []int
    fmt.Println("Strange numbers in the open interval (100, 500) are:\n")
    for i := 101; i < 500; i++ {
        d = d[:0]
        j := i
        for j > 0 {
            d = append(d, j%10)
            j /= 10
        }
        if isPrime(d[0]-d[1]) && isPrime(d[1]-d[2]) {
            fmt.Printf("%d ", i)
            count++
            if count%10 == 0 {
                fmt.Println()
            }
        }
    }
    if count%10 != 0 {
        fmt.Println()
    }
    fmt.Printf("\n%d strange numbers in all.\n", count)
}








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

procedure Strange_Numbers is

   function Is_Strange (A : Natural) return Boolean is
      Last     : Natural := A mod 10;
      Reminder : Natural := A / 10;
      Current  : Natural;
   begin
      while Reminder /= 0 loop
         Current := Reminder mod 10;
         exit when abs (Current - Last) not in 2 | 3 | 5 | 7;
         Last     := Current;
         Reminder := Reminder / 10;
      end loop;
      return Reminder = 0;
   end Is_Strange;

   Count : Natural := 0;
begin
   for A in 101 .. 499 loop
      if Is_Strange (A) then
         Put (A'Image);
         Count := Count + 1;
         if Count mod 10 = 0 then
            New_Line;
         end if;
      end if;
   end loop;
   New_Line;
   Put_Line ("Strange numbers in range 101 .. 499: " & Count'Image);
   New_Line;

   Count := 0;
   for A in 1_000_000_000 .. 1_999_999_999 loop
      if Is_Strange (A) then
         Count := Count + 1;
      end if;
   end loop;
   Put_Line ("Strange numbers in range 1_000_000_000 .. 1_999_999_999: " & Count'Image);
end Strange_Numbers;
Task: Create an object at a given address




Go: package main

import(
	"fmt"
	"unsafe"
	"reflect"
)

func pointer() {
	fmt.Printf("Pointer:\n")

	
	
	
	
	var i int
	p := &i

	fmt.Printf("Before:\n\t%v: %v, %v\n", p, *p, i)

	*p = 3

	fmt.Printf("After:\n\t%v: %v, %v\n", p, *p, i)
}

func slice() {
	fmt.Printf("Slice:\n")

	var a [10]byte

	
	
	
	
	
	var h reflect.SliceHeader
	h.Data = uintptr(unsafe.Pointer(&a)) 
	h.Len = len(a)
	h.Cap = len(a)

	
	s := *(*[]byte)(unsafe.Pointer(&h))

	fmt.Printf("Before:\n\ts: %v\n\ta: %v\n", s, a)

	
	
	copy(s, "A string.")

	fmt.Printf("After:\n\ts: %v\n\ta: %v\n", s, a)
}

func main() {
	pointer()
	fmt.Println()

	slice()
}








Ada: type IO_Port is mod 2**8; 
Device_Port : type IO_Port;
for Device_Port'Address use 16#FFFF_F000#;
Task: Hofstadter Q sequence




Go: package main

import "fmt"

var m map[int]int

func initMap() {
    m = make(map[int]int)
    m[1] = 1
    m[2] = 1
}

func q(n int) (r int) {
    if r = m[n]; r == 0 {
        r = q(n-q(n-1)) + q(n-q(n-2))
        m[n] = r
    }
    return
}

func main() {
    initMap()
    
    for n := 1; n <= 10; n++ {
        showQ(n)
    }
    
    showQ(1000)
    
    count, p := 0, 1
    for n := 2; n <= 1e5; n++ {
        qn := q(n)
        if qn < p {
            count++
        }
        p = qn
    }
    fmt.Println("count:", count)
    
    initMap()
    showQ(1e6)
}

func showQ(n int) {
    fmt.Printf("Q(%d) = %d\n", n, q(n))
}








Ada: with Ada.Text_IO;

procedure Hofstadter_Q_Sequence is

   type Callback is access procedure(N: Positive);

   procedure Q(First, Last: Positive; Q_Proc: Callback) is
   
   

      Q_Store: array(1 .. Last) of Natural := (1 => 1, 2 => 1, others => 0);
      
      
      

      function Q(N: Positive) return Positive is
      begin
         if Q_Store(N) = 0 then
            Q_Store(N) := Q(N - Q(N-1)) + Q(N-Q(N-2));
         end if;
         return Q_Store(N);
      end Q;

   begin
      for I in First .. Last loop
         Q_Proc(Q(I));
      end loop;
   end Q;

   procedure Print(P: Positive) is
   begin
      Ada.Text_IO.Put(Positive'Image(P));
   end Print;

   Decrease_Counter: Natural := 0;
   Previous_Value: Positive := 1;

   procedure Decrease_Count(P: Positive) is
   begin
      if P < Previous_Value then
         Decrease_Counter := Decrease_Counter + 1;
      end if;
      Previous_Value := P;
   end Decrease_Count;

begin
   Q(1, 10, Print'Access);
   
   Ada.Text_IO.New_Line;

   Q(1000, 1000,  Print'Access);
   
   Ada.Text_IO.New_Line;

   Q(2, 100_000, Decrease_Count'Access);
   Ada.Text_IO.Put_Line(Integer'Image(Decrease_Counter));
   
   
end Hofstadter_Q_Sequence;
Task: Count occurrences of a substring




Go: package main
import (
        "fmt"
        "strings"
)

func main() {
        fmt.Println(strings.Count("the three truths", "th")) 
        fmt.Println(strings.Count("ababababab", "abab"))     
}








Ada: with Ada.Strings.Fixed, Ada.Integer_Text_IO;

procedure Substrings is
begin
   Ada.Integer_Text_IO.Put (Ada.Strings.Fixed.Count (Source  => "the three truths",
                                                     Pattern => "th"));
   Ada.Integer_Text_IO.Put (Ada.Strings.Fixed.Count (Source  => "ababababab",
                                                     Pattern => "abab"));
end Substrings;
Task: Append numbers at same position in strings




Go: package main

import "fmt"

func main() {
    list1 := [9]int{1, 2, 3, 4, 5, 6, 7, 8, 9}
    list2 := [9]int{10, 11, 12, 13, 14, 15, 16, 17, 18}
    list3 := [9]int{19, 20, 21, 22, 23, 24, 25, 26, 27}
    var list [9]int
    for i := 0; i < 9; i++ {
        list[i] = list1[i]*1e4 + list2[i]*1e2 + list3[i]
    }
    fmt.Println(list)
}








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

procedure Append_Numbers is

   use Ada.Text_Io, Ada.Strings;

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

   procedure Put (List : List_Type) is
   begin
      Put ("[");
      for E of List loop
         Put (Natural'Image (E));
      end loop;
      Put ("]");
   end Put;

   List_1 : constant List_Type := ( 1,  2,  3,  4,  5,  6,  7,  8,  9);
   List_2 : constant List_Type := (10, 11, 12, 13, 14, 15, 16, 17, 18);
   List_3 : constant List_Type := (19, 20, 21, 22, 23, 24, 25, 26, 27);
   List   : List_Type (List_1'Range);
begin
   for A in List'Range loop
      List (A) := Natural'Value
        (Fixed.Trim (Natural'Image (List_1 (A)), Left) &
         Fixed.Trim (Natural'Image (List_2 (A)), Left) &
         Fixed.Trim (Natural'Image (List_3 (A)), Left));
   end loop;

   Put (List);  New_Line;
end Append_Numbers;
Task: Carmichael 3 strong pseudoprimes




Go: package main

import "fmt"


func mod(n, m int) int {
    return ((n % m) + m) % m
}

func isPrime(n int) bool {
    if n < 2 { return false }
    if n % 2 == 0 { return n == 2 }
    if n % 3 == 0 { return n == 3 }
    d := 5
    for d * d <= n {
        if n % d == 0 { return false }
        d += 2
        if n % d == 0 { return false }
        d += 4
    }
    return true
}

func carmichael(p1 int) {
    for h3 := 2; h3 < p1; h3++ {
        for d := 1; d < h3 + p1; d++ {
            if (h3 + p1) * (p1 - 1) % d == 0 && mod(-p1 * p1, h3) == d % h3 {
                p2 := 1 + (p1 - 1) * (h3 + p1) / d
                if !isPrime(p2) { continue }
                p3 := 1 + p1 * p2 / h3
                if !isPrime(p3) { continue }
                if p2 * p3 % (p1 - 1) != 1 { continue }
                c := p1 * p2 * p3
                fmt.Printf("%2d   %4d   %5d     %d\n", p1, p2, p3, c)
            }
        }
    }
}

func main() {
    fmt.Println("The following are Carmichael munbers for p1 <= 61:\n")
    fmt.Println("p1     p2      p3     product")
    fmt.Println("==     ==      ==     =======")

    for p1 := 2; p1 <= 61; p1++ {
        if isPrime(p1) { carmichael(p1) }
    }
}








Ada: with Ada.Text_IO, Miller_Rabin;

procedure Nemesis is

   type Number is range 0 .. 2**40-1; 

   function Is_Prime(N: Number) return Boolean is
      package MR is new Miller_Rabin(Number); use MR;
   begin
      return MR.Is_Prime(N) = Probably_Prime;
   end Is_Prime;

begin
   for P1 in Number(2) .. 61 loop
      if Is_Prime(P1) then
         for H3 in Number(1) .. P1 loop
            declare
               G: Number := H3 + P1;
               P2, P3: Number;
            begin
               Inner:
               for D in 1 .. G-1 loop
                  if ((H3+P1) * (P1-1)) mod D = 0 and then
                    (-(P1 * P1)) mod H3 = D mod H3
                  then
                     P2 := 1 + ((P1-1) * G / D);
                     P3 := 1 +(P1*P2/H3);
                     if Is_Prime(P2) and then Is_Prime(P3)
                       and then (P2*P3) mod (P1-1) = 1
                     then
                       Ada.Text_IO.Put_Line
                        ( Number'Image(P1) & " *"   & Number'Image(P2) & " *" &
                          Number'Image(P3) & "  = " & Number'Image(P1*P2*P3) );
                     end if;
                  end if;
               end loop Inner;
            end;
         end loop;
      end if;
   end loop;
end Nemesis;
Task: Find square difference




Go: package main

import (
    "fmt"
    "math"
)

func squareDiff(k int) int {
    return int(math.Ceil(float64(k+1) * 0.5))
}

func main() {
    fmt.Println(squareDiff(1000))
}








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

procedure Find_Square_Difference is
   Last   : Natural := 0;
   Square : Positive;
   Diff   : Positive;
begin
   for N in 1 .. Positive'Last loop
      Square := N ** 2;
      Diff   := Square - Last;
      Last   := Square;
      if Diff > 1000 then
         Put_Line (N'Image);
         exit;
      end if;
   end loop;
end Find_Square_Difference;
Task: Keyboard input_Flush the keyboard buffer




Go: package main

import (
    "log"

    gc "code.google.com/p/goncurses"
)

func main() {
    _, err := gc.Init()
    if err != nil {
        log.Fatal("init:", err)
    }
    defer gc.End()
    gc.FlushInput()
}








Ada: with Ada.Text_IO;
procedure Flushtest is
   use Text_IO;
begin
   Put_Line ("Type anything for 2 s");
   delay 2.0;
Flush_Input:
   declare
      Ch   : Character;
      More : Boolean;
   begin
      loop
         Get_Immediate (Ch, More);
         exit when not More;
      end loop;
   end Flush_Input;
   New_Line;
   Put_Line ("Okay, thanks. Here is some input from you:");
   Put_Line (Get_Line);
end Flushtest;
Task: Benford's law




Go: package main

import (
    "fmt"
    "math"
)

func Fib1000() []float64 {
    a, b, r := 0., 1., [1000]float64{}
    for i := range r {
        r[i], a, b = b, b, b+a
    }
    return r[:]
}

func main() {
    show(Fib1000(), "First 1000 Fibonacci numbers")
}

func show(c []float64, title string) {
    var f [9]int
    for _, v := range c {
        f[fmt.Sprintf("%g", v)[0]-'1']++
    }
    fmt.Println(title)
    fmt.Println("Digit  Observed  Predicted")
    for i, n := range f {
        fmt.Printf("  %d  %9.3f  %8.3f\n", i+1, float64(n)/float64(len(c)),
            math.Log10(1+1/float64(i+1)))
    }
}








Ada: with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; 

procedure Benford is
   
   subtype Nonzero_Digit is Natural range 1 .. 9;
   function First_Digit(S: String) return Nonzero_Digit is
      (if S(S'First) in '1' .. '9' 
         then Nonzero_Digit'Value(S(S'First .. S'First))
         else First_Digit(S(S'First+1 .. S'Last)));
      
   package N_IO is new Ada.Text_IO.Integer_IO(Natural);  
      
   procedure Print(D: Nonzero_Digit; Counted, Sum: Natural) is
      package Math is new Ada.Numerics.Generic_Elementary_Functions(Float);
      package F_IO is new Ada.Text_IO.Float_IO(Float);
      Actual: constant Float := Float(Counted) / Float(Sum);
      Expected: constant Float := Math.Log(1.0 + 1.0 / Float(D), Base => 10.0);
      Deviation: constant Float := abs(Expected-Actual);
   begin
      N_IO.Put(D, 5);
      N_IO.Put(Counted, 14);
      F_IO.Put(Float(Sum)*Expected, Fore => 16, Aft => 1, Exp => 0);
      F_IO.Put(100.0*Actual, Fore => 9, Aft => 2, Exp => 0); 
      F_IO.Put(100.0*Expected, Fore => 11, Aft => 2, Exp => 0); 
      F_IO.Put(100.0*Deviation, Fore => 13, Aft => 2, Exp => 0);
   end Print;
   
   Cnt: array(Nonzero_Digit) of Natural := (1 .. 9 => 0);   
   D: Nonzero_Digit;
   Sum: Natural := 0;
   Counter: Positive;       
       
begin     
   while not Ada.Text_IO.End_Of_File loop
      
      N_IO.Get(Counter); 
        
      D := First_Digit(Ada.Text_IO.Get_Line); 
        
      Cnt(D) := Cnt(D)+1;
      Sum := Sum + 1;
   end loop;
   Ada.Text_IO.Put_Line(" Digit  Found[total]   Expected[total]    Found[%]" 
                                          & "   Expected[%]   Difference[%]");
   for I in Nonzero_Digit loop
      Print(I, Cnt(I), Sum);
      Ada.Text_IO.New_Line;
   end loop;
end Benford;
Task: Hickerson series of almost integers




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    ln2, _ := new(big.Rat).SetString("0.6931471805599453094172")
    h := big.NewRat(1, 2)
    h.Quo(h, ln2)
    var f big.Rat
    var w big.Int
    for i := int64(1); i <= 17; i++ {
        h.Quo(h.Mul(h, f.SetInt64(i)), ln2)
        w.Quo(h.Num(), h.Denom())
        f.Sub(h, f.SetInt(&w))
        y, _ := f.Float64()
        d := fmt.Sprintf("%.3f", y)
        fmt.Printf("n: %2d  h: %18d%s  Nearly integer: %t\n",
            i, &w, d[1:], d[2] == '0' || d[2] == '9')
    }
}








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

procedure Almost_Integers is

   type Real is new Long_Long_Float;

   package Real_IO is
      new Ada.Text_IO.Float_IO (Real);
   package Integer_IO is
      new Ada.Text_IO.Integer_IO (Integer);

   function Faculty (N : in Long_Long_Integer) return Long_Long_Integer is
      (if N < 2 then N else N * Faculty (N - 1));

   function Hickerson (N : in Integer) return Real
   is
      package Math is
         new Ada.Numerics.Generic_Elementary_Functions (Real);
      LN2         : constant Real := Math.Log (2.0, Base => Ada.Numerics.E);
      Numerator   : constant Real := Real (Faculty (Long_Long_Integer (N)));
      Denominator : constant Real := 2.0 * LN2 ** (N + 1);
   begin
      return Numerator / Denominator;
   end Hickerson;

   function Is_Almost_Integer (N : Real) return Boolean is
      Image : String (1 .. 100);
   begin
      Real_IO.Put (Image, N, Exp => 0, Aft => 2);

      pragma Assert (Image (Image'Last - 2) = '.');
      case Image (Image'Last - 1) is
         when '0' | '9' =>  return True;
         when others    =>  return False;
      end case;
   end Is_Almost_Integer;

   use Ada.Text_IO;
   Placeholder : String := " n                         h(n)  almost";
   Image_N : String renames Placeholder ( 1 ..  2);
   Image_H : String renames Placeholder ( 4 .. 31);
   Image_A : String renames Placeholder (34 .. 39);
begin
   Put_Line (Placeholder);
   Image_N := (others => '-');
   Image_H := (others => '-');
   Image_A := (others => '-');
   Put_Line (Placeholder);

   for N in 1 .. 17 loop
      declare
         H : constant Real    := Hickerson (N);
         I : constant Boolean := Is_Almost_Integer (H);
      begin
         Integer_IO.Put (Image_N, N);
         Real_IO.Put (Image_H, H, Exp => 0, Aft => 4);
         Image_A := (if I then "TRUE " else "FALSE");
         Put_Line (Placeholder);
      end;
   end loop;
end Almost_Integers;
Task: Additive primes




Go: package main

import "fmt"

func isPrime(n int) bool {
    switch {
    case n < 2:
        return false
    case n%2 == 0:
        return n == 2
    case n%3 == 0:
        return n == 3
    default:
        d := 5
        for d*d <= n {
            if n%d == 0 {
                return false
            }
            d += 2
            if n%d == 0 {
                return false
            }
            d += 4
        }
        return true
    }
}

func sumDigits(n int) int {
    sum := 0
    for n > 0 {
        sum += n % 10
        n /= 10
    }
    return sum
}

func main() {
    fmt.Println("Additive primes less than 500:")
    i := 2
    count := 0
    for {
        if isPrime(i) && isPrime(sumDigits(i)) {
            count++
            fmt.Printf("%3d  ", i)
            if count%10 == 0 {
                fmt.Println()
            }
        }
        if i > 2 {
            i += 2
        } else {
            i++
        }
        if i > 499 {
            break
        }
    }
    fmt.Printf("\n\n%d additive primes found.\n", count)
}








Ada: with Ada.Text_Io;

procedure Additive_Primes is

   Last    : constant := 499;
   Columns : constant := 12;

   type Prime_List is array (2 .. Last) of Boolean;

   function Get_Primes return Prime_List is
      Prime : Prime_List := (others => True);
   begin
      for P in Prime'Range loop
         if Prime (P) then
            for N in 2 .. Positive'Last loop
               exit when N * P not in Prime'Range;
               Prime (N * P) := False;
            end loop;
         end if;
      end loop;
      return Prime;
   end Get_Primes;

   function Sum_Of (N : Natural) return Natural is
      Image : constant String := Natural'Image (N);
      Sum   : Natural := 0;
   begin
      for Char of Image loop
         Sum := Sum + (if Char in '0' .. '9'
                       then Natural'Value ("" & Char)
                       else 0);
      end loop;
      return Sum;
   end Sum_Of;

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

   Prime : constant Prime_List := Get_Primes;
   Count : Natural := 0;
begin
   Put_Line ("Additive primes <500:");
   for N in Prime'Range loop
      if Prime (N) and then Prime (Sum_Of (N)) then
         Count := Count + 1;
         Put (N, Width => 5);
         if Count mod Columns = 0 then
            New_Line;
         end if;
      end if;
   end loop;
   New_Line;

   Put ("There are ");
   Put (Count, Width => 2);
   Put (" additive primes.");
   New_Line;
end Additive_Primes;
Task: Case-sensitivity of identifiers




Go: package dogs

import "fmt"



var dog = "Salt"
var Dog = "Pepper"
var DOG = "Mustard"

func PackageSees() map[*string]int {
    
    fmt.Println("Package sees:", dog, Dog, DOG)
    
    
    
    
    return map[*string]int{&dog: 1, &Dog: 1, &DOG: 1}
}








Ada: with Ada.Text_IO;
procedure Dogs is
   Dog : String := "Bernie";
begin
   Ada.Text_IO.Put_Line ("There is just one dog named " & DOG);
end Dogs;
Task: Common sorted list




Go: package main

import (
    "fmt"
    "sort"
)

func distinctSortedUnion(ll [][]int) []int {
    var res []int
    for _, l := range ll {
        res = append(res, l...)
    }
    set := make(map[int]bool)
    for _, e := range res {
        set[e] = true
    }
    res = res[:0]
    for key := range set {
        res = append(res, key)
    }
    sort.Ints(res)
    return res
}

func main() {
    ll := [][]int{{5, 1, 3, 8, 9, 4, 8, 7}, {3, 5, 9, 8, 4}, {1, 3, 7, 9}}
    fmt.Println("Distinct sorted union of", ll, "is:")
    fmt.Println(distinctSortedUnion(ll))
}








Ada: with Ada.Text_Io;
with Ada.Containers.Vectors;

procedure Sorted is

   package Integer_Vectors is
     new Ada.Containers.Vectors (Index_Type   => Positive,
                                 Element_Type => Integer);
   use Integer_Vectors;

   package Vector_Sorting is
     new Integer_Vectors.Generic_Sorting;
   use Vector_Sorting;

   procedure Unique (Vec : in out Vector) is
      Res : Vector;
   begin
      for E of Vec loop
         if Res.Is_Empty or else Res.Last_Element /= E then
            Res.Append (E);
         end if;
      end loop;
      Vec := Res;
   end Unique;

   procedure Put (Vec : Vector) is
      use Ada.Text_Io;
   begin
      Put ("[");
      for E of Vec loop
         Put (E'Image);  Put (" ");
      end loop;
      Put ("]");
      New_Line;
   end Put;

   A : constant Vector := 5 & 1 & 3 & 8 & 9 & 4 & 8 & 7;
   B : constant Vector := 3 & 5 & 9 & 8 & 4;
   C : constant Vector := 1 & 3 & 7 & 9;
   R : Vector          := A & B & C;
begin
   Sort (R);
   Unique (R);
   Put (R);
end Sorted;
Task: Start from a main routine




Go: package main

import "fmt"

var count = 0

func foo() {
    fmt.Println("foo called")
}

func init() {
    fmt.Println("first init called")
    foo()
}

func init() {
    fmt.Println("second init called")
    main()
}

func main() {
    count++
    fmt.Println("main called when count is", count)
}








Ada: with Ada.Text_IO;
procedure Foo is
begin
   Ada.Text_IO.Put_Line("Bar");
end Foo;
Task: Sorting algorithms_Stooge sort




Go: package main

import "fmt"

var a = []int{170, 45, 75, -90, -802, 24, 2, 66}

func main() {
    fmt.Println("before:", a)
    stoogesort(a)
    fmt.Println("after: ", a)
    fmt.Println("nyuk nyuk nyuk")
}

func stoogesort(a []int) {
    last := len(a) - 1
    if a[last] < a[0] {
        a[0], a[last] = a[last], a[0]
    }
    if last > 1 {
        t := len(a) / 3
        stoogesort(a[:len(a)-t])
        stoogesort(a[t:])
        stoogesort(a[:len(a)-t])
    }
}








Ada: with Ada.Text_IO;
procedure Stooge is
   type Integer_Array is array (Positive range <>) of Integer;
   procedure Swap (Left, Right : in out Integer) is
      Temp : Integer := Left;
   begin
      Left  := Right;
      Right := Temp;
   end Swap;
   procedure Stooge_Sort (List : in out Integer_Array) is
      T : Natural := List'Length / 3;
   begin
      if List (List'Last) < List (List'First) then
         Swap (List (List'Last), List (List'First));
      end if;
      if List'Length > 2 then
         Stooge_Sort (List (List'First     .. List'Last - T));
         Stooge_Sort (List (List'First + T .. List'Last));
         Stooge_Sort (List (List'First     .. List'Last - T));
      end if;
   end Stooge_Sort;
   Test_Array : Integer_Array := (1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7);
begin
   Stooge_Sort (Test_Array);
   for I in Test_Array'Range loop
      Ada.Text_IO.Put (Integer'Image (Test_Array (I)));
      if I /= Test_Array'Last then
         Ada.Text_IO.Put (", ");
      end if;
   end loop;
   Ada.Text_IO.New_Line;
end Stooge;
Task: Truncate a file




Go: import (
    "fmt"
    "os"
)

if err := os.Truncate("filename", newSize); err != nil {
    fmt.Println(err)
}








Ada: with Ada.Command_Line, Ada.Sequential_IO, Ada.Directories;

procedure Truncate_File is

   type Byte is mod 256;
   for Byte'Size use 8;

   package Byte_IO is new Ada.Sequential_IO(Byte);

   function Arg(N: Positive) return String renames Ada.Command_Line.Argument;
   function Args return Natural renames Ada.Command_Line.Argument_Count;

begin
   
   if Args < 2 or else Args > 3 then
      raise Program_Error
        with "usage: truncate_file <filename> <length> [<temp_file>]";
   end if;

   
   declare
      File_Name: String := Arg(1);
      Temp_Name: String := (if Args = 2 then Arg(1) & ".tmp" else Arg(3));
                             
      File, Temp: Byte_IO.File_Type;
      Count: Natural := Natural'Value(Arg(2));
      Value: Byte;
   begin
      
      Byte_IO.Open  (File => File, Mode => Byte_IO.In_File,  Name => File_Name);
      Byte_IO.Create(File => Temp, Mode => Byte_IO.Out_File, Name => Temp_Name);

      
      while (not Byte_IO.End_Of_File(File)) and Count > 0 loop
         Byte_IO.Read (File, Value);
         Byte_IO.Write(Temp, Value);
         Count := Count - 1;
      end loop;

      
      Byte_IO.Close(Temp);
      Byte_IO.Close(File);

      if Count = 0 then 
         
         Ada.Directories.Delete_File(Name => File_Name);
         Ada.Directories.Rename(Old_Name => Temp_Name, New_Name => File_Name);
      else 
         
         Ada.Directories.Delete_File(Name => Temp_Name);
         raise Program_Error
           with "Size of """ & File_Name & """ less than " & Arg(2);
      end if;
   end;
end Truncate_File;
Task: Sorting algorithms_Shell sort




Go: package main

import "fmt"

var a = []int{170, 45, 75, -90, -802, 24, 2, 66}

func main() {
    fmt.Println("before:", a)
    for inc := len(a) / 2; inc > 0; inc = (inc + 1) * 5 / 11 {
        for i := inc; i < len(a); i++ {
            j, temp := i, a[i]
            for ; j >= inc && a[j-inc] > temp; j -= inc {
                a[j] = a[j-inc]
            }
            a[j] = temp
        }
    }
    fmt.Println("after: ", a)
}








Ada: generic
   type Element_Type is digits <>;
   type Index_Type is (<>);
   type Array_Type is array(Index_Type range <>) of Element_Type;
package Shell_Sort is
   procedure Sort(Item : in out Array_Type);
end Shell_Sort;