WeixiangYan/CodeTransOcean huggingface dataset:
Task: Window creation




Go: package main

import (
    "github.com/mattn/go-gtk/glib"
    "github.com/mattn/go-gtk/gtk"
)

func main() {
    gtk.Init(nil)
    window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
    window.Connect("destroy",
        func(*glib.CallbackContext) { gtk.MainQuit() }, "")
    window.Show()
    gtk.Main()
}





VB: Option Explicit

Sub InsertForm()
Dim myForm As Object, strname As String
    Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    strname = myForm.Name
    VBA.UserForms.Add(strname).Show
End Sub
Task: Window creation

C: 

#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"

int main()
{
  SDL_Surface *screen;
  
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);
  screen = SDL_SetVideoMode( 800, 600, 16, SDL_SWSURFACE | SDL_HWPALETTE );
  
  return 0;
}








VB: Option Explicit

Sub InsertForm()
Dim myForm As Object, strname As String
    Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    strname = myForm.Name
    VBA.UserForms.Add(strname).Show
End Sub
Task: Window creation


C++: #include <QApplication>
#include <QMainWindow>

int main(int argc, char *argv[])
{
 QApplication app(argc, argv);
 QMainWindow window;
 window.show();
 return app.exec();
}



Go: package main

import (
    "github.com/mattn/go-gtk/glib"
    "github.com/mattn/go-gtk/gtk"
)

func main() {
    gtk.Init(nil)
    window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
    window.Connect("destroy",
        func(*glib.CallbackContext) { gtk.MainQuit() }, "")
    window.Show()
    gtk.Main()
}
Task: Window creation





Java: import javax.swing.JFrame;

public class Main {
     public static void main(String[] args) throws Exception {
         JFrame w = new JFrame("Title");
         w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         w.setSize(800,600);
         w.setVisible(true);
     }
}



Python:   import Tkinter
  
  w = Tkinter.Tk()
  w.mainloop()
Task: Window creation







Python:   import Tkinter
  
  w = Tkinter.Tk()
  w.mainloop()


VB: Option Explicit

Sub InsertForm()
Dim myForm As Object, strname As String
    Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    strname = myForm.Name
    VBA.UserForms.Add(strname).Show
End Sub
Task: Window creation

C: 

#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"

int main()
{
  SDL_Surface *screen;
  
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
    return 1;
  }
  atexit(SDL_Quit);
  screen = SDL_SetVideoMode( 800, 600, 16, SDL_SWSURFACE | SDL_HWPALETTE );
  
  return 0;
}







Python:   import Tkinter
  
  w = Tkinter.Tk()
  w.mainloop()
Task: Two identical strings

C: #include <stdio.h>
#include <stdint.h>

uint8_t bit_length(uint32_t n) {
    uint8_t r;
    for (r=0; n; r++) n >>= 1;
    return r;
}

uint32_t concat_bits(uint32_t n) {
    return (n << bit_length(n)) | n;
}

char *bits(uint32_t n) {
    static char buf[33];
    char *ptr = &buf[33];
    *--ptr = 0;
    do {
        *--ptr = '0' + (n & 1);
    } while (n >>= 1);
    return ptr;
}

int main() {
    uint32_t n, r;
    for (n=1; (r = concat_bits(n)) < 1000; n++) {
        printf("%d: %s\n", r, bits(r));
    }
    return 0;
}



C#: #include <iostream>
#include <string>



void base2_increment(std::string& s) {
    size_t z = s.rfind('0');
    if (z != std::string::npos) {
        s[z] = '1';
        size_t count = s.size() - (z + 1);
        s.replace(z + 1, count, count, '0');
    } else {
        s.assign(s.size() + 1, '0');
        s[0] = '1';
    }
}

int main() {
    std::cout << "Decimal\tBinary\n";
    std::string s("1");
    for (unsigned int n = 1; ; ++n) {
        unsigned int i = n + (n << s.size());
        if (i >= 1000)
            break;
        std::cout << i << '\t' << s << s << '\n';
        base2_increment(s);
    }
}
Task: Two identical strings

C: #include <stdio.h>
#include <stdint.h>

uint8_t bit_length(uint32_t n) {
    uint8_t r;
    for (r=0; n; r++) n >>= 1;
    return r;
}

uint32_t concat_bits(uint32_t n) {
    return (n << bit_length(n)) | n;
}

char *bits(uint32_t n) {
    static char buf[33];
    char *ptr = &buf[33];
    *--ptr = 0;
    do {
        *--ptr = '0' + (n & 1);
    } while (n >>= 1);
    return ptr;
}

int main() {
    uint32_t n, r;
    for (n=1; (r = concat_bits(n)) < 1000; n++) {
        printf("%d: %s\n", r, bits(r));
    }
    return 0;
}




Go: package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := int64(1)
    for {
        b2 := strconv.FormatInt(i, 2)
        b2 += b2
        d, _ := strconv.ParseInt(b2, 2, 64)
        if d >= 1000 {
            break
        }
        fmt.Printf("%3d : %s\n", d, b2)
        i++
    }
    fmt.Println("\nFound", i-1, "numbers.")
}
Task: Two identical strings

C: #include <stdio.h>
#include <stdint.h>

uint8_t bit_length(uint32_t n) {
    uint8_t r;
    for (r=0; n; r++) n >>= 1;
    return r;
}

uint32_t concat_bits(uint32_t n) {
    return (n << bit_length(n)) | n;
}

char *bits(uint32_t n) {
    static char buf[33];
    char *ptr = &buf[33];
    *--ptr = 0;
    do {
        *--ptr = '0' + (n & 1);
    } while (n >>= 1);
    return ptr;
}

int main() {
    uint32_t n, r;
    for (n=1; (r = concat_bits(n)) < 1000; n++) {
        printf("%d: %s\n", r, bits(r));
    }
    return 0;
}





Java: public class TwoIdenticalStrings {
    public static void main(String[] args) {
        System.out.println("Decimal Binary");
        for (int i = 0; i < 1_000; i++) {
            String binStr = Integer.toBinaryString(i);
            if (binStr.length() % 2 == 0) {
                int len = binStr.length() / 2;
                if (binStr.substring(0, len).equals(binStr.substring(len))) {
                    System.out.printf("%7d %s%n", i, binStr);
                }
            }
        }
    }
}
Task: Two identical strings

C: #include <stdio.h>
#include <stdint.h>

uint8_t bit_length(uint32_t n) {
    uint8_t r;
    for (r=0; n; r++) n >>= 1;
    return r;
}

uint32_t concat_bits(uint32_t n) {
    return (n << bit_length(n)) | n;
}

char *bits(uint32_t n) {
    static char buf[33];
    char *ptr = &buf[33];
    *--ptr = 0;
    do {
        *--ptr = '0' + (n & 1);
    } while (n >>= 1);
    return ptr;
}

int main() {
    uint32_t n, r;
    for (n=1; (r = concat_bits(n)) < 1000; n++) {
        printf("%d: %s\n", r, bits(r));
    }
    return 0;
}







Python: def bits(n):
    
    r = 0
    while n:
        n >>= 1
        r += 1
    return r
    
def concat(n):
    
    return n << bits(n) | n
    
n = 1
while concat(n) <= 1000:
    print("{0}: {0:b}".format(concat(n)))
    n += 1
Task: Two identical strings


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



void base2_increment(std::string& s) {
    size_t z = s.rfind('0');
    if (z != std::string::npos) {
        s[z] = '1';
        size_t count = s.size() - (z + 1);
        s.replace(z + 1, count, count, '0');
    } else {
        s.assign(s.size() + 1, '0');
        s[0] = '1';
    }
}

int main() {
    std::cout << "Decimal\tBinary\n";
    std::string s("1");
    for (unsigned int n = 1; ; ++n) {
        unsigned int i = n + (n << s.size());
        if (i >= 1000)
            break;
        std::cout << i << '\t' << s << s << '\n';
        base2_increment(s);
    }
}



Go: package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := int64(1)
    for {
        b2 := strconv.FormatInt(i, 2)
        b2 += b2
        d, _ := strconv.ParseInt(b2, 2, 64)
        if d >= 1000 {
            break
        }
        fmt.Printf("%3d : %s\n", d, b2)
        i++
    }
    fmt.Println("\nFound", i-1, "numbers.")
}
Task: Two identical strings


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



void base2_increment(std::string& s) {
    size_t z = s.rfind('0');
    if (z != std::string::npos) {
        s[z] = '1';
        size_t count = s.size() - (z + 1);
        s.replace(z + 1, count, count, '0');
    } else {
        s.assign(s.size() + 1, '0');
        s[0] = '1';
    }
}

int main() {
    std::cout << "Decimal\tBinary\n";
    std::string s("1");
    for (unsigned int n = 1; ; ++n) {
        unsigned int i = n + (n << s.size());
        if (i >= 1000)
            break;
        std::cout << i << '\t' << s << s << '\n';
        base2_increment(s);
    }
}




Java: public class TwoIdenticalStrings {
    public static void main(String[] args) {
        System.out.println("Decimal Binary");
        for (int i = 0; i < 1_000; i++) {
            String binStr = Integer.toBinaryString(i);
            if (binStr.length() % 2 == 0) {
                int len = binStr.length() / 2;
                if (binStr.substring(0, len).equals(binStr.substring(len))) {
                    System.out.printf("%7d %s%n", i, binStr);
                }
            }
        }
    }
}
Task: Two identical strings


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



void base2_increment(std::string& s) {
    size_t z = s.rfind('0');
    if (z != std::string::npos) {
        s[z] = '1';
        size_t count = s.size() - (z + 1);
        s.replace(z + 1, count, count, '0');
    } else {
        s.assign(s.size() + 1, '0');
        s[0] = '1';
    }
}

int main() {
    std::cout << "Decimal\tBinary\n";
    std::string s("1");
    for (unsigned int n = 1; ; ++n) {
        unsigned int i = n + (n << s.size());
        if (i >= 1000)
            break;
        std::cout << i << '\t' << s << s << '\n';
        base2_increment(s);
    }
}






Python: def bits(n):
    
    r = 0
    while n:
        n >>= 1
        r += 1
    return r
    
def concat(n):
    
    return n << bits(n) | n
    
n = 1
while concat(n) <= 1000:
    print("{0}: {0:b}".format(concat(n)))
    n += 1
Task: Two identical strings



C#: using System; using static System.Console;
class Program { static void Main() { int c = 0, lmt = 1000;
    for (int n = 1, p = 2, k; n <= lmt; n++)
      if ((k = n + n * (p += n >= p ? p : 0)) > lmt) break;
      else Console.Write("{0,3} ({1,-10})  {2}", k,
          Convert.ToString(k, 2), ++c % 5 == 0 ? "\n" : "");
    Write("\nFound {0} numbers whose base 2 representation is the " +
      "concatenation of two identical binary strings.", c); } }


Go: package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := int64(1)
    for {
        b2 := strconv.FormatInt(i, 2)
        b2 += b2
        d, _ := strconv.ParseInt(b2, 2, 64)
        if d >= 1000 {
            break
        }
        fmt.Printf("%3d : %s\n", d, b2)
        i++
    }
    fmt.Println("\nFound", i-1, "numbers.")
}
Task: Two identical strings



C#: using System; using static System.Console;
class Program { static void Main() { int c = 0, lmt = 1000;
    for (int n = 1, p = 2, k; n <= lmt; n++)
      if ((k = n + n * (p += n >= p ? p : 0)) > lmt) break;
      else Console.Write("{0,3} ({1,-10})  {2}", k,
          Convert.ToString(k, 2), ++c % 5 == 0 ? "\n" : "");
    Write("\nFound {0} numbers whose base 2 representation is the " +
      "concatenation of two identical binary strings.", c); } }



Java: public class TwoIdenticalStrings {
    public static void main(String[] args) {
        System.out.println("Decimal Binary");
        for (int i = 0; i < 1_000; i++) {
            String binStr = Integer.toBinaryString(i);
            if (binStr.length() % 2 == 0) {
                int len = binStr.length() / 2;
                if (binStr.substring(0, len).equals(binStr.substring(len))) {
                    System.out.printf("%7d %s%n", i, binStr);
                }
            }
        }
    }
}
Task: Two identical strings




Go: package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := int64(1)
    for {
        b2 := strconv.FormatInt(i, 2)
        b2 += b2
        d, _ := strconv.ParseInt(b2, 2, 64)
        if d >= 1000 {
            break
        }
        fmt.Printf("%3d : %s\n", d, b2)
        i++
    }
    fmt.Println("\nFound", i-1, "numbers.")
}


Java: public class TwoIdenticalStrings {
    public static void main(String[] args) {
        System.out.println("Decimal Binary");
        for (int i = 0; i < 1_000; i++) {
            String binStr = Integer.toBinaryString(i);
            if (binStr.length() % 2 == 0) {
                int len = binStr.length() / 2;
                if (binStr.substring(0, len).equals(binStr.substring(len))) {
                    System.out.printf("%7d %s%n", i, binStr);
                }
            }
        }
    }
}
Task: Two identical strings





Java: public class TwoIdenticalStrings {
    public static void main(String[] args) {
        System.out.println("Decimal Binary");
        for (int i = 0; i < 1_000; i++) {
            String binStr = Integer.toBinaryString(i);
            if (binStr.length() % 2 == 0) {
                int len = binStr.length() / 2;
                if (binStr.substring(0, len).equals(binStr.substring(len))) {
                    System.out.printf("%7d %s%n", i, binStr);
                }
            }
        }
    }
}



Python: def bits(n):
    
    r = 0
    while n:
        n >>= 1
        r += 1
    return r
    
def concat(n):
    
    return n << bits(n) | n
    
n = 1
while concat(n) <= 1000:
    print("{0}: {0:b}".format(concat(n)))
    n += 1
Task: Two identical strings




Go: package main

import (
    "fmt"
    "strconv"
)

func main() {
    i := int64(1)
    for {
        b2 := strconv.FormatInt(i, 2)
        b2 += b2
        d, _ := strconv.ParseInt(b2, 2, 64)
        if d >= 1000 {
            break
        }
        fmt.Printf("%3d : %s\n", d, b2)
        i++
    }
    fmt.Println("\nFound", i-1, "numbers.")
}




Python: def bits(n):
    
    r = 0
    while n:
        n >>= 1
        r += 1
    return r
    
def concat(n):
    
    return n << bits(n) | n
    
n = 1
while concat(n) <= 1000:
    print("{0}: {0:b}".format(concat(n)))
    n += 1
Task: Two identical strings

C: #include <stdio.h>
#include <stdint.h>

uint8_t bit_length(uint32_t n) {
    uint8_t r;
    for (r=0; n; r++) n >>= 1;
    return r;
}

uint32_t concat_bits(uint32_t n) {
    return (n << bit_length(n)) | n;
}

char *bits(uint32_t n) {
    static char buf[33];
    char *ptr = &buf[33];
    *--ptr = 0;
    do {
        *--ptr = '0' + (n & 1);
    } while (n >>= 1);
    return ptr;
}

int main() {
    uint32_t n, r;
    for (n=1; (r = concat_bits(n)) < 1000; n++) {
        printf("%d: %s\n", r, bits(r));
    }
    return 0;
}


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



void base2_increment(std::string& s) {
    size_t z = s.rfind('0');
    if (z != std::string::npos) {
        s[z] = '1';
        size_t count = s.size() - (z + 1);
        s.replace(z + 1, count, count, '0');
    } else {
        s.assign(s.size() + 1, '0');
        s[0] = '1';
    }
}

int main() {
    std::cout << "Decimal\tBinary\n";
    std::string s("1");
    for (unsigned int n = 1; ; ++n) {
        unsigned int i = n + (n << s.size());
        if (i >= 1000)
            break;
        std::cout << i << '\t' << s << s << '\n';
        base2_increment(s);
    }
}
Task: Two identical strings


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



void base2_increment(std::string& s) {
    size_t z = s.rfind('0');
    if (z != std::string::npos) {
        s[z] = '1';
        size_t count = s.size() - (z + 1);
        s.replace(z + 1, count, count, '0');
    } else {
        s.assign(s.size() + 1, '0');
        s[0] = '1';
    }
}

int main() {
    std::cout << "Decimal\tBinary\n";
    std::string s("1");
    for (unsigned int n = 1; ; ++n) {
        unsigned int i = n + (n << s.size());
        if (i >= 1000)
            break;
        std::cout << i << '\t' << s << s << '\n';
        base2_increment(s);
    }
}


C#: using System; using static System.Console;
class Program { static void Main() { int c = 0, lmt = 1000;
    for (int n = 1, p = 2, k; n <= lmt; n++)
      if ((k = n + n * (p += n >= p ? p : 0)) > lmt) break;
      else Console.Write("{0,3} ({1,-10})  {2}", k,
          Convert.ToString(k, 2), ++c % 5 == 0 ? "\n" : "");
    Write("\nFound {0} numbers whose base 2 representation is the " +
      "concatenation of two identical binary strings.", c); } }
Task: Two identical strings



C#: using System; using static System.Console;
class Program { static void Main() { int c = 0, lmt = 1000;
    for (int n = 1, p = 2, k; n <= lmt; n++)
      if ((k = n + n * (p += n >= p ? p : 0)) > lmt) break;
      else Console.Write("{0,3} ({1,-10})  {2}", k,
          Convert.ToString(k, 2), ++c % 5 == 0 ? "\n" : "");
    Write("\nFound {0} numbers whose base 2 representation is the " +
      "concatenation of two identical binary strings.", c); } }





Python: def bits(n):
    
    r = 0
    while n:
        n >>= 1
        r += 1
    return r
    
def concat(n):
    
    return n << bits(n) | n
    
n = 1
while concat(n) <= 1000:
    print("{0}: {0:b}".format(concat(n)))
    n += 1
Task: Pig the dice game

C: #include <stdio.h>
#include <stdlib.h>
#include <time.h>


const int NUM_PLAYERS = 2;
const int MAX_POINTS = 100;



int randrange(int min, int max){
    return (rand() % (max - min + 1)) + min;
}



void ResetScores(int *scores){
    for(int i = 0; i < NUM_PLAYERS; i++){
        scores[i] = 0;
    }
}


void Play(int *scores){
    int scoredPoints = 0;
    int diceResult;
    int choice;

    for(int i = 0; i < NUM_PLAYERS; i++){
        while(1){
            printf("Player %d - You have %d total points and %d points this turn \nWhat do you want to do (1)roll or (2)hold: ", i + 1, scores[i], scoredPoints);
            scanf("%d", &choice);

            if(choice == 1){
                diceResult = randrange(1, 6);
                printf("\nYou rolled a %d\n", diceResult);

                if(diceResult != 1){
                    scoredPoints += diceResult;
                }
                else{
                    printf("You loose all your points from this turn\n\n");
                    scoredPoints = 0;
                    break;
                }
            }
            else if(choice == 2){
                scores[i] += scoredPoints;
                printf("\nYou holded, you have %d points\n\n", scores[i]);

                break;
            }
        }

        scoredPoints = 0;
        CheckForWin(scores[i], i + 1);

    }
}


void CheckForWin(int playerScore, int playerNum){
    if(playerScore >= MAX_POINTS){
        printf("\n\nCONGRATULATIONS PLAYER %d, YOU WIN\n\n!", playerNum);

        exit(EXIT_SUCCESS);
    }
}


int main()
{
    srand(time(0));

    int scores[NUM_PLAYERS];
    ResetScores(scores);

    while(1){
        Play(scores);
    }

    return 0;
}







Python: 



from random import randint

playercount = 2
maxscore = 100
safescore = [0] * playercount
player = 0
score=0

while max(safescore) < maxscore:
    rolling = input("Player %i: (%i, %i) Rolling? (Y) "
                    % (player, safescore[player], score)).strip().lower() in {'yes', 'y', ''}
    if rolling:
        rolled = randint(1, 6)
        print('  Rolled %i' % rolled)
        if rolled == 1:
            print('  Bust! you lose %i but still keep your previous %i'
                  % (score, safescore[player]))
            score, player = 0, (player + 1) % playercount
        else:
            score += rolled
    else:
        safescore[player] += score
        if safescore[player] >= maxscore:
            break
        print('  Sticking with %i' % safescore[player])
        score, player = 0, (player + 1) % playercount
        
print('\nPlayer %i wins with a score of %i' %(player, safescore[player]))
Task: Pig the dice game





Java: import java.util.*;

public class PigDice {

    public static void main(String[] args) {
        final int maxScore = 100;
        final int playerCount = 2;
        final String[] yesses = {"y", "Y", ""};

        int[] safeScore = new int[2];
        int player = 0, score = 0;

        Scanner sc = new Scanner(System.in);
        Random rnd = new Random();

        while (true) {
            System.out.printf(" Player %d: (%d, %d) Rolling? (y/n) ", player,
                    safeScore[player], score);
            if (safeScore[player] + score < maxScore
                    && Arrays.asList(yesses).contains(sc.nextLine())) {
                final int rolled = rnd.nextInt(6) + 1;
                System.out.printf(" Rolled %d\n", rolled);
                if (rolled == 1) {
                    System.out.printf(" Bust! You lose %d but keep %d\n\n",
                            score, safeScore[player]);
                } else {
                    score += rolled;
                    continue;
                }
            } else {
                safeScore[player] += score;
                if (safeScore[player] >= maxScore)
                    break;
                System.out.printf(" Sticking with %d\n\n", safeScore[player]);
            }
            score = 0;
            player = (player + 1) % playerCount;
        }
        System.out.printf("\n\nPlayer %d wins with a score of %d",
                player, safeScore[player]);
    }
}




VB: Option Explicit

Sub Main_Pig()
Dim Scs() As Byte, Ask As Integer, Np As Boolean, Go As Boolean
Dim Cp As Byte, Rd As Byte, NbP As Byte, ScBT As Byte
    
    Const INPTXT As String = "Enter number of players : "
    Const INPTITL As String = "Numeric only"
    Const ROL As String = "Player ¤¤¤¤ rolls the die."
    Const MSG As String = "Do you want to ""hold"" : "
    Const TITL As String = "Total if you keep : "
    Const RES As String = "The die give you : ¤¤¤¤ points."
    Const ONE As String = "The die give you : 1 point. Sorry!" & vbCrLf & "Next player."
    Const WIN As String = "Player ¤¤¤¤ win the Pig Dice Game!"
    Const STW As Byte = 100

    Randomize Timer
    NbP = Application.InputBox(INPTXT, INPTITL, 2, Type:=1)
    ReDim Scs(1 To NbP)
    Cp = 1
    Do
        ScBT = 0
        Do
            MsgBox Replace(ROL, "¤¤¤¤", Cp)
            Rd = Int((Rnd * 6) + 1)
            If Rd > 1 Then
                MsgBox Replace(RES, "¤¤¤¤", Rd)
                ScBT = ScBT + Rd
                If Scs(Cp) + ScBT >= STW Then
                    Go = True
                    Exit Do
                End If
                Ask = MsgBox(MSG & ScBT, vbYesNo, TITL & Scs(Cp) + ScBT)
                If Ask = vbYes Then
                    Scs(Cp) = Scs(Cp) + ScBT
                    Np = True
                End If
            Else
                MsgBox ONE
                Np = True
            End If
        Loop Until Np
        If Not Go Then
            Np = False
            Cp = Cp + 1
            If Cp > NbP Then Cp = 1
        End If
    Loop Until Go
    MsgBox Replace(WIN, "¤¤¤¤", Cp)
End Sub