WeixiangYan/CodeTransOcean huggingface dataset:
Task: Nim game


C++: #include <iostream>
#include <limits>

using namespace std;

void showTokens(int tokens) {
    cout << "Tokens remaining " << tokens << endl << endl;
}

int main() {
    int tokens = 12;
    while (true) {
        showTokens(tokens);
        cout << "  How many tokens 1, 2 or 3? ";
        int t;
        cin >> t;
        if (cin.fail()) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << endl << "Invalid input, try again." << endl << endl;
        } else if (t < 1 || t > 3) {
            cout << endl << "Must be a number between 1 and 3, try again." << endl << endl;
        } else {
            int ct = 4 - t;
            string s  = (ct > 1) ? "s" : "";
            cout << "  Computer takes " << ct << " token" << s << endl << endl;
            tokens -= 4;
        }
        if (tokens == 0) {
            showTokens(0);
            cout << "  Computer wins!" << endl;
            return 0;
        }
    }
}










Ada: with Ada.Text_IO;

procedure Nim is
   subtype Token_Range is Positive range 1 .. 3;

   package TIO renames Ada.Text_IO;
   package Token_IO is new TIO.Integer_IO(Token_Range);

   procedure Get_Tokens(remaining : in Natural; how_many : out Token_Range) is
   begin
      loop
         TIO.Put("How many tokens would you like to take? ");
         begin
            Token_IO.Get(TIO.Standard_Input, how_many);
            exit when how_many < remaining;
            raise Constraint_Error;
         exception
            when TIO.Data_Error | Constraint_Error =>
               if not TIO.End_Of_Line(TIO.Standard_Input) then
                  TIO.Skip_Line(TIO.Standard_Input);
               end if;
               TIO.Put_Line("Invalid input.");
         end;
      end loop;
   end;

    tokens : Natural := 12;
    how_many : Token_Range;
begin
   loop
      TIO.Put_Line(tokens'Img & " tokens remain.");
      
      Get_Tokens(tokens, how_many);
      TIO.Put_Line("Human takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      
      how_many := tokens mod 4;
      TIO.Put_Line("Computer takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      Ada.Text_IO.New_Line;
      exit when tokens = 0;
   end loop;

   TIO.Put_Line("Computer won!");
end Nim;
Task: Nim game




Go: package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

func showTokens(tokens int) {
    fmt.Println("Tokens remaining", tokens, "\n")    
}

func main() {
    tokens := 12
    scanner := bufio.NewScanner(os.Stdin)
    for {
        showTokens(tokens)
        fmt.Print("  How many tokens 1, 2 or 3? ")
        scanner.Scan()
        if scerr := scanner.Err(); scerr != nil {
            fmt.Println("Error reading standard input:", scerr)
            return
        }
        t, err := strconv.Atoi(scanner.Text())
        if err != nil || t < 1 || t > 3 {
            fmt.Println("\nMust be a number between 1 and 3, try again.\n")
        } else {
            ct := 4 - t
            s := "s"
            if ct == 1 {
                s = ""
            }
            fmt.Print("  Computer takes ", ct, " token", s, "\n\n")
            tokens -= 4
        }
        if tokens == 0 {
            showTokens(0)
            fmt.Println("  Computer wins!")
            return
        }
    }
}








Ada: with Ada.Text_IO;

procedure Nim is
   subtype Token_Range is Positive range 1 .. 3;

   package TIO renames Ada.Text_IO;
   package Token_IO is new TIO.Integer_IO(Token_Range);

   procedure Get_Tokens(remaining : in Natural; how_many : out Token_Range) is
   begin
      loop
         TIO.Put("How many tokens would you like to take? ");
         begin
            Token_IO.Get(TIO.Standard_Input, how_many);
            exit when how_many < remaining;
            raise Constraint_Error;
         exception
            when TIO.Data_Error | Constraint_Error =>
               if not TIO.End_Of_Line(TIO.Standard_Input) then
                  TIO.Skip_Line(TIO.Standard_Input);
               end if;
               TIO.Put_Line("Invalid input.");
         end;
      end loop;
   end;

    tokens : Natural := 12;
    how_many : Token_Range;
begin
   loop
      TIO.Put_Line(tokens'Img & " tokens remain.");
      
      Get_Tokens(tokens, how_many);
      TIO.Put_Line("Human takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      
      how_many := tokens mod 4;
      TIO.Put_Line("Computer takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      Ada.Text_IO.New_Line;
      exit when tokens = 0;
   end loop;

   TIO.Put_Line("Computer won!");
end Nim;
Task: Nim game





Java: import java.util.Scanner;

public class NimGame {

    public static void main(String[] args) {
        runGame(12);
    }
    
    private static void runGame(int tokens) {
        System.out.printf("Nim game.%n%n");

        Scanner in = new Scanner(System.in);;
        
        do {            
            boolean humanInputOk = false;
            int humanTokens = 0;
            while ( ! humanInputOk ) {
                System.out.printf("Human takes how many tokens?  ");
                String input = in.next();
                try {
                    humanTokens = Integer.parseInt(input);
                    if ( humanTokens >= 1 && humanTokens <= 3 ) {
                        humanInputOk = true;
                    }
                    else {
                        System.out.printf("Try a number between 1 and 3.%n");
                    }
                }
                catch (NumberFormatException e) {
                    System.out.printf("Invalid input.  Try a number between 1 and 3.%n");
                }
            }
            
            tokens -= humanTokens;
            
            System.out.printf("You take %d token%s.%n%d token%s remaining.%n%n", humanTokens, humanTokens > 1 ? "s" : "", tokens, tokens != 1 ? "s" : "");
            if ( tokens == 0 ) {
                System.out.printf("You win!!.%n%n");
                break;
            }
            int computerTokens = 4 - humanTokens;
            tokens -= computerTokens;
            
            System.out.printf("Computer takes %d token%s.%n%d token%s remaining.%n%n", computerTokens, computerTokens != 1 ? "s" : "", tokens, tokens != 1 ? "s" : "");
            if ( tokens == 0 ) {
                System.out.printf("Computer wins!!.%n%n");
            }

        } while (tokens > 0);

        in.close();
    }

}







Ada: with Ada.Text_IO;

procedure Nim is
   subtype Token_Range is Positive range 1 .. 3;

   package TIO renames Ada.Text_IO;
   package Token_IO is new TIO.Integer_IO(Token_Range);

   procedure Get_Tokens(remaining : in Natural; how_many : out Token_Range) is
   begin
      loop
         TIO.Put("How many tokens would you like to take? ");
         begin
            Token_IO.Get(TIO.Standard_Input, how_many);
            exit when how_many < remaining;
            raise Constraint_Error;
         exception
            when TIO.Data_Error | Constraint_Error =>
               if not TIO.End_Of_Line(TIO.Standard_Input) then
                  TIO.Skip_Line(TIO.Standard_Input);
               end if;
               TIO.Put_Line("Invalid input.");
         end;
      end loop;
   end;

    tokens : Natural := 12;
    how_many : Token_Range;
begin
   loop
      TIO.Put_Line(tokens'Img & " tokens remain.");
      
      Get_Tokens(tokens, how_many);
      TIO.Put_Line("Human takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      
      how_many := tokens mod 4;
      TIO.Put_Line("Computer takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      Ada.Text_IO.New_Line;
      exit when tokens = 0;
   end loop;

   TIO.Put_Line("Computer won!");
end Nim;
Task: Nim game







Python: print("Py Nim\n")

def getTokens(curTokens):
	global tokens
	
	print("How many tokens would you like to take? ", end='')
	take = int(input())
	
	if (take < 1 or take > 3):
		print("Number must be between 1 and 3.\n")
		getTokens(curTokens)
		return
	
	tokens = curTokens - take
	print(f'You take {take} tokens.')
	print(f'{tokens} tokens remaining.\n')

def compTurn(curTokens):
	global tokens
	
	take = curTokens % 4
	tokens = curTokens - take
	print (f'Computer takes {take} tokens.')
	print (f'{tokens} tokens remaining.\n')
	

tokens = 12
while (tokens > 0):
	getTokens(tokens)
	compTurn(tokens)

print("Computer wins!")





Ada: with Ada.Text_IO;

procedure Nim is
   subtype Token_Range is Positive range 1 .. 3;

   package TIO renames Ada.Text_IO;
   package Token_IO is new TIO.Integer_IO(Token_Range);

   procedure Get_Tokens(remaining : in Natural; how_many : out Token_Range) is
   begin
      loop
         TIO.Put("How many tokens would you like to take? ");
         begin
            Token_IO.Get(TIO.Standard_Input, how_many);
            exit when how_many < remaining;
            raise Constraint_Error;
         exception
            when TIO.Data_Error | Constraint_Error =>
               if not TIO.End_Of_Line(TIO.Standard_Input) then
                  TIO.Skip_Line(TIO.Standard_Input);
               end if;
               TIO.Put_Line("Invalid input.");
         end;
      end loop;
   end;

    tokens : Natural := 12;
    how_many : Token_Range;
begin
   loop
      TIO.Put_Line(tokens'Img & " tokens remain.");
      
      Get_Tokens(tokens, how_many);
      TIO.Put_Line("Human takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      
      how_many := tokens mod 4;
      TIO.Put_Line("Computer takes" & how_many'Img & " tokens.");
      tokens := tokens - how_many;
      Ada.Text_IO.New_Line;
      exit when tokens = 0;
   end loop;

   TIO.Put_Line("Computer won!");
end Nim;
Task: Nim game

C: #include  <stdio.h>

int playerTurn(int numTokens, int take);
int computerTurn(int numTokens);

int main(void)
{
	printf("Nim Game\n\n");
	
	int Tokens = 12;
	
	while(Tokens > 0)
	{
		printf("How many tokens would you like to take?: ");
		
		int uin;
		scanf("%i", &uin);
		
		int nextTokens = playerTurn(Tokens, uin);
		
		if (nextTokens == Tokens)
		{
			continue;
		}
		
		Tokens = nextTokens;
		
		Tokens = computerTurn(Tokens);
	}
	printf("Computer wins.");
	
	return 0;
}

int playerTurn(int numTokens, int take)
{
	if (take < 1 || take > 3)
	{
		printf("\nTake must be between 1 and 3.\n\n");
		return numTokens;
	}
	int remainingTokens = numTokens - take;
	
	printf("\nPlayer takes %i tokens.\n", take);
	printf("%i tokens remaining.\n\n", remainingTokens);
	
	return remainingTokens;
}

int computerTurn(int numTokens)
{
	int take = numTokens % 4;
	int remainingTokens = numTokens - take;
	
	printf("Computer takes %u tokens.\n", take);
	printf("%i tokens remaining.\n\n", remainingTokens);
	
	return remainingTokens;
}
Task: Nim game



C#: using System;

namespace nimGame
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("There are twelve tokens.\n" +
                    "You can take 1, 2, or 3 on your turn.\n" +
                    "Whoever takes the last token wins.\n");

            int tokens = 12;

            while (tokens > 0)
            {
                Console.WriteLine("There are " + tokens + " remaining.");
                Console.WriteLine("How many do you take?");
                int playertake = Convert.ToInt32(Console.ReadLine());

                if (playertake < 1 | playertake > 3)
                {
                    Console.WriteLine("1, 2, or 3 only.");
                }
                else
                {
                    tokens -= playertake;
                    Console.WriteLine("I take " + (4 - playertake) + ".");
                    tokens -= (4 - playertake);
                }
            }
            Console.WriteLine("I win again.");
            Console.ReadLine();
        }

    }
}
Task: Nim game


C++: #include <iostream>
#include <limits>

using namespace std;

void showTokens(int tokens) {
    cout << "Tokens remaining " << tokens << endl << endl;
}

int main() {
    int tokens = 12;
    while (true) {
        showTokens(tokens);
        cout << "  How many tokens 1, 2 or 3? ";
        int t;
        cin >> t;
        if (cin.fail()) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << endl << "Invalid input, try again." << endl << endl;
        } else if (t < 1 || t > 3) {
            cout << endl << "Must be a number between 1 and 3, try again." << endl << endl;
        } else {
            int ct = 4 - t;
            string s  = (ct > 1) ? "s" : "";
            cout << "  Computer takes " << ct << " token" << s << endl << endl;
            tokens -= 4;
        }
        if (tokens == 0) {
            showTokens(0);
            cout << "  Computer wins!" << endl;
            return 0;
        }
    }
}
Task: Nim game





Java: import java.util.Scanner;

public class NimGame {

    public static void main(String[] args) {
        runGame(12);
    }
    
    private static void runGame(int tokens) {
        System.out.printf("Nim game.%n%n");

        Scanner in = new Scanner(System.in);;
        
        do {            
            boolean humanInputOk = false;
            int humanTokens = 0;
            while ( ! humanInputOk ) {
                System.out.printf("Human takes how many tokens?  ");
                String input = in.next();
                try {
                    humanTokens = Integer.parseInt(input);
                    if ( humanTokens >= 1 && humanTokens <= 3 ) {
                        humanInputOk = true;
                    }
                    else {
                        System.out.printf("Try a number between 1 and 3.%n");
                    }
                }
                catch (NumberFormatException e) {
                    System.out.printf("Invalid input.  Try a number between 1 and 3.%n");
                }
            }
            
            tokens -= humanTokens;
            
            System.out.printf("You take %d token%s.%n%d token%s remaining.%n%n", humanTokens, humanTokens > 1 ? "s" : "", tokens, tokens != 1 ? "s" : "");
            if ( tokens == 0 ) {
                System.out.printf("You win!!.%n%n");
                break;
            }
            int computerTokens = 4 - humanTokens;
            tokens -= computerTokens;
            
            System.out.printf("Computer takes %d token%s.%n%d token%s remaining.%n%n", computerTokens, computerTokens != 1 ? "s" : "", tokens, tokens != 1 ? "s" : "");
            if ( tokens == 0 ) {
                System.out.printf("Computer wins!!.%n%n");
            }

        } while (tokens > 0);

        in.close();
    }

}
Task: Nim game







Python: print("Py Nim\n")

def getTokens(curTokens):
	global tokens
	
	print("How many tokens would you like to take? ", end='')
	take = int(input())
	
	if (take < 1 or take > 3):
		print("Number must be between 1 and 3.\n")
		getTokens(curTokens)
		return
	
	tokens = curTokens - take
	print(f'You take {take} tokens.')
	print(f'{tokens} tokens remaining.\n')

def compTurn(curTokens):
	global tokens
	
	take = curTokens % 4
	tokens = curTokens - take
	print (f'Computer takes {take} tokens.')
	print (f'{tokens} tokens remaining.\n')
	

tokens = 12
while (tokens > 0):
	getTokens(tokens)
	compTurn(tokens)

print("Computer wins!")
Task: Nim game








VB: monton = 12
llevar = 0

while monton > 0
    input "There are "; monton; " tokens remaining. How many would you like to take? "; llevar
    while llevar = 0 or llevar > 3
        input "You must take 1, 2, or 3 tokens. How many would you like to take "; llevar
    wend

    print "On my turn I will take "; 4 - llevar; " token(s)."
    monton = monton - 4
wend

print
print "I got the last token. I win! Better luck next time."
end
Task: Nim game




Go: package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
)

func showTokens(tokens int) {
    fmt.Println("Tokens remaining", tokens, "\n")    
}

func main() {
    tokens := 12
    scanner := bufio.NewScanner(os.Stdin)
    for {
        showTokens(tokens)
        fmt.Print("  How many tokens 1, 2 or 3? ")
        scanner.Scan()
        if scerr := scanner.Err(); scerr != nil {
            fmt.Println("Error reading standard input:", scerr)
            return
        }
        t, err := strconv.Atoi(scanner.Text())
        if err != nil || t < 1 || t > 3 {
            fmt.Println("\nMust be a number between 1 and 3, try again.\n")
        } else {
            ct := 4 - t
            s := "s"
            if ct == 1 {
                s = ""
            }
            fmt.Print("  Computer takes ", ct, " token", s, "\n\n")
            tokens -= 4
        }
        if tokens == 0 {
            showTokens(0)
            fmt.Println("  Computer wins!")
            return
        }
    }
}
Task: XML_Output



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static string CreateXML(Dictionary<string, string> characterRemarks)
    {
        var remarks = characterRemarks.Select(r => new XElement("Character", r.Value, new XAttribute("Name", r.Key)));
        var xml = new XElement("CharacterRemarks", remarks);
        return xml.ToString();
    }

    static void Main(string[] args)
    {
        var characterRemarks = new Dictionary<string, string>
        {
            { "April", "Bubbly: I'm > Tam and <= Emily" },
            { "Tam O'Shanter", "Burns: \"When chapman billies leave the street ...\"" },
            { "Emily", "Short & shrift" }
        };

        string xml = CreateXML(characterRemarks);
        Console.WriteLine(xml);
    }
}









Ada: with Ada.Strings.Unbounded;
with Ada.Text_IO.Text_Streams;
with DOM.Core.Documents;
with DOM.Core.Elements;
with DOM.Core.Nodes;

procedure Character_Remarks is
   package DC renames DOM.Core;
   package IO renames Ada.Text_IO;
   package US renames Ada.Strings.Unbounded;
   type Remarks is record
      Name : US.Unbounded_String;
      Text : US.Unbounded_String;
   end record;
   type Remark_List is array (Positive range <>) of Remarks;
   My_Remarks : Remark_List :=
      ((US.To_Unbounded_String ("April"),
        US.To_Unbounded_String ("Bubbly: I'm > Tam and <= Emily")),
       (US.To_Unbounded_String ("Tam O'Shanter"),
        US.To_Unbounded_String ("Burns: ""When chapman billies leave the street ...""")),
       (US.To_Unbounded_String ("Emily"),
        US.To_Unbounded_String ("Short & shrift")));
   My_Implementation : DC.DOM_Implementation;
   My_Document       : DC.Document := DC.Create_Document (My_Implementation);
   My_Root_Node      : DC.Element  := DC.Nodes.Append_Child (My_Document,
                                         DC.Documents.Create_Element (My_Document, "CharacterRemarks"));
   My_Element_Node   : DC.Element;
   My_Text_Node      : DC.Text;
begin
   for I in My_Remarks'Range loop
      My_Element_Node := DC.Nodes.Append_Child (My_Root_Node,
                            DC.Documents.Create_Element (My_Document, "Character"));
      DC.Elements.Set_Attribute (My_Element_Node, "Name", US.To_String (My_Remarks (I).Name));
      My_Text_Node    := DC.Nodes.Append_Child (My_Element_Node,
                            DC.Documents.Create_Text_Node (My_Document, US.To_String (My_Remarks (I).Text)));
   end loop;
   DC.Nodes.Write (IO.Text_Streams.Stream (IO.Standard_Output),
                   N => My_Document,
                   Pretty_Print => True);
end Character_Remarks;
Task: XML_Output

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

const char *names[] = {
  "April", "Tam O'Shanter", "Emily", NULL
};
const char *remarks[] = {
  "Bubbly: I'm > Tam and <= Emily",
  "Burns: \"When chapman billies leave the street ...\"",
  "Short & shrift", NULL
};

int main()
{
  xmlDoc *doc = NULL;
  xmlNode *root = NULL, *node;
  const char **next;
  int a;

  doc = xmlNewDoc("1.0");
  root = xmlNewNode(NULL, "CharacterRemarks");
  xmlDocSetRootElement(doc, root);

  for(next = names, a = 0; *next != NULL; next++, a++) {
    node = xmlNewNode(NULL, "Character");
    (void)xmlNewProp(node, "name", *next);
    xmlAddChild(node, xmlNewText(remarks[a]));
    xmlAddChild(root, node);
  }

  xmlElemDump(stdout, doc, root);

  xmlFreeDoc(doc);
  xmlCleanupParser();
  
  return EXIT_SUCCESS;
}











Ada: with Ada.Strings.Unbounded;
with Ada.Text_IO.Text_Streams;
with DOM.Core.Documents;
with DOM.Core.Elements;
with DOM.Core.Nodes;

procedure Character_Remarks is
   package DC renames DOM.Core;
   package IO renames Ada.Text_IO;
   package US renames Ada.Strings.Unbounded;
   type Remarks is record
      Name : US.Unbounded_String;
      Text : US.Unbounded_String;
   end record;
   type Remark_List is array (Positive range <>) of Remarks;
   My_Remarks : Remark_List :=
      ((US.To_Unbounded_String ("April"),
        US.To_Unbounded_String ("Bubbly: I'm > Tam and <= Emily")),
       (US.To_Unbounded_String ("Tam O'Shanter"),
        US.To_Unbounded_String ("Burns: ""When chapman billies leave the street ...""")),
       (US.To_Unbounded_String ("Emily"),
        US.To_Unbounded_String ("Short & shrift")));
   My_Implementation : DC.DOM_Implementation;
   My_Document       : DC.Document := DC.Create_Document (My_Implementation);
   My_Root_Node      : DC.Element  := DC.Nodes.Append_Child (My_Document,
                                         DC.Documents.Create_Element (My_Document, "CharacterRemarks"));
   My_Element_Node   : DC.Element;
   My_Text_Node      : DC.Text;
begin
   for I in My_Remarks'Range loop
      My_Element_Node := DC.Nodes.Append_Child (My_Root_Node,
                            DC.Documents.Create_Element (My_Document, "Character"));
      DC.Elements.Set_Attribute (My_Element_Node, "Name", US.To_String (My_Remarks (I).Name));
      My_Text_Node    := DC.Nodes.Append_Child (My_Element_Node,
                            DC.Documents.Create_Text_Node (My_Document, US.To_String (My_Remarks (I).Text)));
   end loop;
   DC.Nodes.Write (IO.Text_Streams.Stream (IO.Standard_Output),
                   N => My_Document,
                   Pretty_Print => True);
end Character_Remarks;