WeixiangYan/CodeTransOcean huggingface dataset:
Task: Number reversal game



C#: using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var r = new Random();

        var tries = 1;
        var sorted = Enumerable.Range(1, 9).ToList();
        var values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();

        while (Enumerable.SequenceEqual(sorted, values)) {
            values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();
        }

        

        while (!Enumerable.SequenceEqual(sorted, values))
        {
            Console.Write("# {0}: LIST: {1} - Flip how many? ", tries, String.Join(" ", values));

            values.Reverse(0, int.Parse(Console.ReadLine()));
            tries += 1;
        }

        Console.WriteLine("\nYou took {0} attempts to put the digits in order!", tries - 1);
        Console.ReadLine();
    }
}









Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Numerics.Discrete_Random;
 
procedure NumberReverse is
 
   subtype RandRange is Integer range 1..9;
   type NumArrayType is array (Integer range 1..9) of Integer;
 
   package RandNumbers is new Ada.Numerics.Discrete_Random(RandRange);
   use RandNumbers;

   G : Generator;

   procedure FillArray (A : in out NumArrayType) is
      Temp : RandRange;
   begin
      A := (others => 0);
      for I in 1..9 loop
         Temp := Random(G);
         while A(Temp) /= 0 loop
            Temp := Random(G);
         end loop;
         A(Temp) := I;
      end loop;
   end FillArray;
 
   procedure Put(A : in NumArrayType) is
   begin
      for I in 1..9 loop
         Put(A(I), 0);
         Put(" ");
      end loop;
   end Put;
 
   procedure Prompt (Index : out Integer) is
   begin
      New_Line;
      Put("How many numbers would you like to reverse: ");
      Get(Index);
   end Prompt;
 
   procedure ReverseArray(Arr : in out NumArrayType;
                          Index : in Integer) is
      Temp : RandRange;
   begin
      for I in 1..Index/2 loop
         Temp := Arr(I);
         Arr(I) := Arr(Index + 1 - I);
         Arr(Index + 1 - I) := Temp;
      end loop;
   end ReverseArray;
 
   Sorted : constant NumArrayType := (1,2,3,4,5,6,7,8,9);
   Arr    : NumArrayType;
   Index  : Integer;
   Count  : Integer := 0;
begin
   Reset(G);
   loop
      FillArray(Arr);
      exit when Sorted /= Arr;
   end loop;
   loop
      Put(Arr);
      Prompt(Index);
      Count := Count + 1;
      ReverseArray(Arr, Index);
      exit when Sorted = Arr;
   end loop;
   Put(Arr);
   New_Line;
   Put("Congratulations! You win. It took " & 
         Integer'Image(Count) & " tries.");
end NumberReverse;
Task: Number reversal game

C: void number_reversal_game()
{
    printf("Number Reversal Game. Type a number to flip the first n numbers.");
    printf("Win by sorting the numbers in ascending order.\n");
    printf("Anything besides numbers are ignored.\n");
    printf("\t  |1__2__3__4__5__6__7__8__9|\n");
    int list[9] = {1,2,3,4,5,6,7,8,9};
    shuffle_list(list,9);

    int tries=0;
    unsigned int i;
    int input;

    while(!check_array(list, 9))
    {
        ((tries<10) ? printf("Round %d :  ", tries) : printf("Round %d : ", tries));
        for(i=0;i<9;i++)printf("%d  ",list[i]);
        printf("  Gimme that number:");
        while(1)
        {
            
            scanf("%d", &input);
            if(input>1&&input<10)
                break;

            printf("\n%d - Please enter a number between 2 and 9:", (int)input);
        }
        tries++;
        do_flip(list, 9, input);
    }
    printf("Hurray! You solved it in %d moves!\n", tries);
}











Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Numerics.Discrete_Random;
 
procedure NumberReverse is
 
   subtype RandRange is Integer range 1..9;
   type NumArrayType is array (Integer range 1..9) of Integer;
 
   package RandNumbers is new Ada.Numerics.Discrete_Random(RandRange);
   use RandNumbers;

   G : Generator;

   procedure FillArray (A : in out NumArrayType) is
      Temp : RandRange;
   begin
      A := (others => 0);
      for I in 1..9 loop
         Temp := Random(G);
         while A(Temp) /= 0 loop
            Temp := Random(G);
         end loop;
         A(Temp) := I;
      end loop;
   end FillArray;
 
   procedure Put(A : in NumArrayType) is
   begin
      for I in 1..9 loop
         Put(A(I), 0);
         Put(" ");
      end loop;
   end Put;
 
   procedure Prompt (Index : out Integer) is
   begin
      New_Line;
      Put("How many numbers would you like to reverse: ");
      Get(Index);
   end Prompt;
 
   procedure ReverseArray(Arr : in out NumArrayType;
                          Index : in Integer) is
      Temp : RandRange;
   begin
      for I in 1..Index/2 loop
         Temp := Arr(I);
         Arr(I) := Arr(Index + 1 - I);
         Arr(Index + 1 - I) := Temp;
      end loop;
   end ReverseArray;
 
   Sorted : constant NumArrayType := (1,2,3,4,5,6,7,8,9);
   Arr    : NumArrayType;
   Index  : Integer;
   Count  : Integer := 0;
begin
   Reset(G);
   loop
      FillArray(Arr);
      exit when Sorted /= Arr;
   end loop;
   loop
      Put(Arr);
      Prompt(Index);
      Count := Count + 1;
      ReverseArray(Arr, Index);
      exit when Sorted = Arr;
   end loop;
   Put(Arr);
   New_Line;
   Put("Congratulations! You win. It took " & 
         Integer'Image(Count) & " tries.");
end NumberReverse;
Task: Number reversal game


C++: void number_reversal_game()
{
	cout << "Number Reversal Game. Type a number to flip the first n numbers.";
	cout << "You win by sorting the numbers in ascending order.";
	cout << "Anything besides numbers are ignored.\n";
	cout << "\t  |1__2__3__4__5__6__7__8__9|\n";
	int list[9] = {1,2,3,4,5,6,7,8,9};
        do
        {
	  shuffle_list(list,9);
        } while(check_array(list, 9));

	int tries=0;
	unsigned int i;
	int input;

	while(!check_array(list, 9))
	{
		cout << "Round " << tries << ((tries<10) ? " :  " : " : ");
		for(i=0;i<9;i++)cout << list[i] << "  ";
		cout << "  Gimme that number:";
		while(1)
		{
			cin >> input;
			if(input>1&&input<10)
				break;

			cout << "\nPlease enter a number between 2 and 9:";
		}
		tries++;
		do_flip(list, 9, input);
	}
	cout << "Hurray! You solved it in %d moves!\n";
}










Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Numerics.Discrete_Random;
 
procedure NumberReverse is
 
   subtype RandRange is Integer range 1..9;
   type NumArrayType is array (Integer range 1..9) of Integer;
 
   package RandNumbers is new Ada.Numerics.Discrete_Random(RandRange);
   use RandNumbers;

   G : Generator;

   procedure FillArray (A : in out NumArrayType) is
      Temp : RandRange;
   begin
      A := (others => 0);
      for I in 1..9 loop
         Temp := Random(G);
         while A(Temp) /= 0 loop
            Temp := Random(G);
         end loop;
         A(Temp) := I;
      end loop;
   end FillArray;
 
   procedure Put(A : in NumArrayType) is
   begin
      for I in 1..9 loop
         Put(A(I), 0);
         Put(" ");
      end loop;
   end Put;
 
   procedure Prompt (Index : out Integer) is
   begin
      New_Line;
      Put("How many numbers would you like to reverse: ");
      Get(Index);
   end Prompt;
 
   procedure ReverseArray(Arr : in out NumArrayType;
                          Index : in Integer) is
      Temp : RandRange;
   begin
      for I in 1..Index/2 loop
         Temp := Arr(I);
         Arr(I) := Arr(Index + 1 - I);
         Arr(Index + 1 - I) := Temp;
      end loop;
   end ReverseArray;
 
   Sorted : constant NumArrayType := (1,2,3,4,5,6,7,8,9);
   Arr    : NumArrayType;
   Index  : Integer;
   Count  : Integer := 0;
begin
   Reset(G);
   loop
      FillArray(Arr);
      exit when Sorted /= Arr;
   end loop;
   loop
      Put(Arr);
      Prompt(Index);
      Count := Count + 1;
      ReverseArray(Arr, Index);
      exit when Sorted = Arr;
   end loop;
   Put(Arr);
   New_Line;
   Put("Congratulations! You win. It took " & 
         Integer'Image(Count) & " tries.");
end NumberReverse;
Task: Number reversal game




Go: package main

import (
    "fmt"
    "math/rand"
    "sort"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())
    var k []int
    for {
        k = rand.Perm(9)
        for i, r := range k {
            if r == 0 {
                k[i] = 9
            }
        }
        if !sort.IntsAreSorted(k) {
            break
        }
    }
    fmt.Println("Sort digits by reversing a number of digits on the left.")
    var n, score int
    for {
        fmt.Print("Digits: ", k, ". How many to reverse? ")
        i, _ := fmt.Scanln(&n)
        score++
        if i == 0 || n < 2 || n > 9 {
            fmt.Println("\n(Enter a number from 2 to 9)")
            continue
        }
        for l, r := 0, n-1; l < r; l, r = l+1, r-1 {
            k[l], k[r] = k[r], k[l]
        }
        if sort.IntsAreSorted(k) {
            fmt.Print("Digits: ", k, ".\n")
            fmt.Print("Your score: ", score, ".  Good job.\n")
            return
        }
    }
}








Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Numerics.Discrete_Random;
 
procedure NumberReverse is
 
   subtype RandRange is Integer range 1..9;
   type NumArrayType is array (Integer range 1..9) of Integer;
 
   package RandNumbers is new Ada.Numerics.Discrete_Random(RandRange);
   use RandNumbers;

   G : Generator;

   procedure FillArray (A : in out NumArrayType) is
      Temp : RandRange;
   begin
      A := (others => 0);
      for I in 1..9 loop
         Temp := Random(G);
         while A(Temp) /= 0 loop
            Temp := Random(G);
         end loop;
         A(Temp) := I;
      end loop;
   end FillArray;
 
   procedure Put(A : in NumArrayType) is
   begin
      for I in 1..9 loop
         Put(A(I), 0);
         Put(" ");
      end loop;
   end Put;
 
   procedure Prompt (Index : out Integer) is
   begin
      New_Line;
      Put("How many numbers would you like to reverse: ");
      Get(Index);
   end Prompt;
 
   procedure ReverseArray(Arr : in out NumArrayType;
                          Index : in Integer) is
      Temp : RandRange;
   begin
      for I in 1..Index/2 loop
         Temp := Arr(I);
         Arr(I) := Arr(Index + 1 - I);
         Arr(Index + 1 - I) := Temp;
      end loop;
   end ReverseArray;
 
   Sorted : constant NumArrayType := (1,2,3,4,5,6,7,8,9);
   Arr    : NumArrayType;
   Index  : Integer;
   Count  : Integer := 0;
begin
   Reset(G);
   loop
      FillArray(Arr);
      exit when Sorted /= Arr;
   end loop;
   loop
      Put(Arr);
      Prompt(Index);
      Count := Count + 1;
      ReverseArray(Arr, Index);
      exit when Sorted = Arr;
   end loop;
   Put(Arr);
   New_Line;
   Put("Congratulations! You win. It took " & 
         Integer'Image(Count) & " tries.");
end NumberReverse;
Task: Number reversal game







Python: print '

var data, trials = list(1..9), 0

while data == sort data:
    random.shuffle data

while data != sort data:
    trials += 1
    flip = int input '
    data[:flip] = reverse data[:flip]

print '\nYou took ${trials} attempts to put digits in order!'





Ada: with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Numerics.Discrete_Random;
 
procedure NumberReverse is
 
   subtype RandRange is Integer range 1..9;
   type NumArrayType is array (Integer range 1..9) of Integer;
 
   package RandNumbers is new Ada.Numerics.Discrete_Random(RandRange);
   use RandNumbers;

   G : Generator;

   procedure FillArray (A : in out NumArrayType) is
      Temp : RandRange;
   begin
      A := (others => 0);
      for I in 1..9 loop
         Temp := Random(G);
         while A(Temp) /= 0 loop
            Temp := Random(G);
         end loop;
         A(Temp) := I;
      end loop;
   end FillArray;
 
   procedure Put(A : in NumArrayType) is
   begin
      for I in 1..9 loop
         Put(A(I), 0);
         Put(" ");
      end loop;
   end Put;
 
   procedure Prompt (Index : out Integer) is
   begin
      New_Line;
      Put("How many numbers would you like to reverse: ");
      Get(Index);
   end Prompt;
 
   procedure ReverseArray(Arr : in out NumArrayType;
                          Index : in Integer) is
      Temp : RandRange;
   begin
      for I in 1..Index/2 loop
         Temp := Arr(I);
         Arr(I) := Arr(Index + 1 - I);
         Arr(Index + 1 - I) := Temp;
      end loop;
   end ReverseArray;
 
   Sorted : constant NumArrayType := (1,2,3,4,5,6,7,8,9);
   Arr    : NumArrayType;
   Index  : Integer;
   Count  : Integer := 0;
begin
   Reset(G);
   loop
      FillArray(Arr);
      exit when Sorted /= Arr;
   end loop;
   loop
      Put(Arr);
      Prompt(Index);
      Count := Count + 1;
      ReverseArray(Arr, Index);
      exit when Sorted = Arr;
   end loop;
   Put(Arr);
   New_Line;
   Put("Congratulations! You win. It took " & 
         Integer'Image(Count) & " tries.");
end NumberReverse;
Task: Number reversal game

C: void number_reversal_game()
{
    printf("Number Reversal Game. Type a number to flip the first n numbers.");
    printf("Win by sorting the numbers in ascending order.\n");
    printf("Anything besides numbers are ignored.\n");
    printf("\t  |1__2__3__4__5__6__7__8__9|\n");
    int list[9] = {1,2,3,4,5,6,7,8,9};
    shuffle_list(list,9);

    int tries=0;
    unsigned int i;
    int input;

    while(!check_array(list, 9))
    {
        ((tries<10) ? printf("Round %d :  ", tries) : printf("Round %d : ", tries));
        for(i=0;i<9;i++)printf("%d  ",list[i]);
        printf("  Gimme that number:");
        while(1)
        {
            
            scanf("%d", &input);
            if(input>1&&input<10)
                break;

            printf("\n%d - Please enter a number between 2 and 9:", (int)input);
        }
        tries++;
        do_flip(list, 9, input);
    }
    printf("Hurray! You solved it in %d moves!\n", tries);
}
Task: Number reversal game



C#: using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var r = new Random();

        var tries = 1;
        var sorted = Enumerable.Range(1, 9).ToList();
        var values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();

        while (Enumerable.SequenceEqual(sorted, values)) {
            values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();
        }

        

        while (!Enumerable.SequenceEqual(sorted, values))
        {
            Console.Write("# {0}: LIST: {1} - Flip how many? ", tries, String.Join(" ", values));

            values.Reverse(0, int.Parse(Console.ReadLine()));
            tries += 1;
        }

        Console.WriteLine("\nYou took {0} attempts to put the digits in order!", tries - 1);
        Console.ReadLine();
    }
}
Task: Number reversal game


C++: void number_reversal_game()
{
	cout << "Number Reversal Game. Type a number to flip the first n numbers.";
	cout << "You win by sorting the numbers in ascending order.";
	cout << "Anything besides numbers are ignored.\n";
	cout << "\t  |1__2__3__4__5__6__7__8__9|\n";
	int list[9] = {1,2,3,4,5,6,7,8,9};
        do
        {
	  shuffle_list(list,9);
        } while(check_array(list, 9));

	int tries=0;
	unsigned int i;
	int input;

	while(!check_array(list, 9))
	{
		cout << "Round " << tries << ((tries<10) ? " :  " : " : ");
		for(i=0;i<9;i++)cout << list[i] << "  ";
		cout << "  Gimme that number:";
		while(1)
		{
			cin >> input;
			if(input>1&&input<10)
				break;

			cout << "\nPlease enter a number between 2 and 9:";
		}
		tries++;
		do_flip(list, 9, input);
	}
	cout << "Hurray! You solved it in %d moves!\n";
}
Task: Number reversal game





Java: import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;


public class ReversalGame {
    private List<Integer> gameList;

    public ReversalGame() {
        initialize();
    }

    public void play() throws Exception {
        int i = 0;
        int moveCount = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println(gameList);
            System.out.println("Please enter a index to reverse from 2 to 9. Enter 99 to quit");
            i = scanner.nextInt();
            if (i == 99) {
                break;
            }
            if (i < 2 || i > 9) {
                System.out.println("Invalid input");
            } else {
                moveCount++;
                reverse(i);
                if (isSorted()) {
                    System.out.println("Congratulations you solved this in " + moveCount + " moves!");
                    break;
                }
            }

        }
        scanner.close();
    }

    private void reverse(int position) {
        Collections.reverse(gameList.subList(0, position));
    }

    private boolean isSorted() {
        for (int i=0; i < gameList.size() - 1; ++i) {
            if (gameList.get(i).compareTo(gameList.get(i + 1)) > 0) {
                return false;
            }
        }
        return true;
    }

    private void initialize() {
        this.gameList = new ArrayList<Integer>(9);
        for (int i=1; i < 10; ++i) {
            gameList.add(i);
        }
        while (isSorted()) {
            Collections.shuffle(gameList);
        }
    }


    public static void main(String[] args) {
        try {
            ReversalGame game = new ReversalGame();
            game.play();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
Task: Number reversal game







Python: print '

var data, trials = list(1..9), 0

while data == sort data:
    random.shuffle data

while data != sort data:
    trials += 1
    flip = int input '
    data[:flip] = reverse data[:flip]

print '\nYou took ${trials} attempts to put digits in order!'
Task: Number reversal game








VB: Private Function shuffle(ByVal a As Variant) As Variant
    Dim t As Variant, i As Integer
    For i = UBound(a) To LBound(a) + 1 Step -1
        j = Int((UBound(a) - LBound(a) + 1) * Rnd + LBound(a))
        t = a(i)
        a(i) = a(j)
        a(j) = t
    Next i
    shuffle = a
End Function
Private Sub reverse(ByRef a As Variant, n As Integer)
    Dim b As Variant
    b = a
    For i = 1 To n
        a(i) = b(n + 1 - i)
    Next i
End Sub
Public Sub game()
    Debug.Print "Given a jumbled list of the numbers 1 to 9"
    Debug.Print "you must select how many digits from the left to reverse."
    Debug.Print "Your goal is to get the digits in order with 1 on the left and 9 on the right."
     
    inums = [{1,2,3,4,5,6,7,8,9}]
    Dim nums As Variant
    Dim turns As Integer, flip As Integer
     
    shuffled = False
    Do While Not shuffled
        nums = shuffle(inums)
        For i = LBound(nums) To UBound(nums)
            If nums(i) <> inums(i) Then
                shuffled = True
                Exit For
            End If
        Next i
    Loop

     
    Do While True
        Debug.Print turns; ":";
        For Each x In nums: Debug.Print x;: Next x
        Debug.Print
        flag = False
        For i = LBound(nums) To UBound(nums)
            If nums(i) <> inums(i) Then
                flag = True
                Exit For
            End If
        Next i
        If flag Then
            flip = InputBox(" -- How many numbers should be flipped? ")
            reverse nums, flip
            turns = turns + 1
        Else
            Exit Do
        End If
    Loop
     
    Debug.Print "You took"; turns; "turns to put the digits in order."
End Sub
Task: Number reversal game




Go: package main

import (
    "fmt"
    "math/rand"
    "sort"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())
    var k []int
    for {
        k = rand.Perm(9)
        for i, r := range k {
            if r == 0 {
                k[i] = 9
            }
        }
        if !sort.IntsAreSorted(k) {
            break
        }
    }
    fmt.Println("Sort digits by reversing a number of digits on the left.")
    var n, score int
    for {
        fmt.Print("Digits: ", k, ". How many to reverse? ")
        i, _ := fmt.Scanln(&n)
        score++
        if i == 0 || n < 2 || n > 9 {
            fmt.Println("\n(Enter a number from 2 to 9)")
            continue
        }
        for l, r := 0, n-1; l < r; l, r = l+1, r-1 {
            k[l], k[r] = k[r], k[l]
        }
        if sort.IntsAreSorted(k) {
            fmt.Print("Digits: ", k, ".\n")
            fmt.Print("Your score: ", score, ".  Good job.\n")
            return
        }
    }
}
Task: Number reversal game



C#: using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var r = new Random();

        var tries = 1;
        var sorted = Enumerable.Range(1, 9).ToList();
        var values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();

        while (Enumerable.SequenceEqual(sorted, values)) {
            values = sorted.OrderBy(x => r.Next(-1, 1)).ToList();
        }

        

        while (!Enumerable.SequenceEqual(sorted, values))
        {
            Console.Write("# {0}: LIST: {1} - Flip how many? ", tries, String.Join(" ", values));

            values.Reverse(0, int.Parse(Console.ReadLine()));
            tries += 1;
        }

        Console.WriteLine("\nYou took {0} attempts to put the digits in order!", tries - 1);
        Console.ReadLine();
    }
}








Fortran: program Reversal_game
  implicit none
 
  integer :: list(9) = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9 /)
  integer :: pos, attempts = 0
    
  call random_seed
  do while (sorted(list))
    call Shuffle(list)
  end do
  write(*, "(9i5)") list
  write(*,*)
  do while (.not. Sorted(list))
    write(*, "(a)", advance="no") "How many numbers from the left do you want to reverse? : "
    read*, pos
    attempts = attempts + 1
    list(1:pos) = list(pos:1:-1)
    write(*, "(9i5)") list
    write(*,*)
  end do
  write(*,*)
  write(*, "(a,i0,a)") "Congratulations

contains

subroutine Shuffle(a)
  integer, intent(inout) :: a(:)
  integer :: i, randpos, temp
  real :: r

  do i = size(a), 2, -1
    call random_number(r)
    randpos = int(r * i) + 1
    temp = a(randpos)
    a(randpos) = a(i)
    a(i) = temp
  end do
end subroutine

function Sorted(a)
  logical :: Sorted
  integer, intent(in) :: a(:)
  integer :: i
  
  do i = 1, size(a)-1
    if(list(i+1) /= list(i)+1) then
      Sorted = .false.
      return
    end if
  end do
  Sorted = .true.
end function
  
end program