Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Port the provided Swift code into Go while preserving the original functionality. | import Foundation
func montyHall(doors: Int = 3, guess: Int, switch: Bool) -> Bool {
guard doors > 2, guess > 0, guess <= doors else { fatalError() }
let winningDoor = Int.random(in: 1...doors)
return winningDoor == guess ? !`switch` : `switch`
}
var switchResults = [Bool]()
for _ in 0..<1_000 {
let guess ... | package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
games := 100000
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var switcherWins, keeperWins, shown int
for i := 0; i < games; i++ {
doors := []int{0, 0, 0}
doors[r.Intn(3)] = 1
choice := r.Intn(3)
for shown = r.Intn(3); shown == ch... |
Transform the following Tcl implementation into C, maintaining the same output and logic. | set stay 0; set change 0; set total 10000
for {set i 0} {$i<$total} {incr i} {
if {int(rand()*3) == int(rand()*3)} {
incr stay
} else {
incr change
}
}
puts "Estimate: $stay/$total wins for staying strategy"
puts "Estimate: $change/$total wins for changing strategy"
|
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#define NumSim 1000000000
void main() {
unsigned long int i,stay=0;
int ChosenDoor,WinningDoor;
bool door[3]={0,0,0};
srand(time(NULL));
for(i=0;i<=NumSim;i++){
WinningD... |
Can you help me rewrite this code in C# instead of Tcl, keeping it the same logically? | set stay 0; set change 0; set total 10000
for {set i 0} {$i<$total} {incr i} {
if {int(rand()*3) == int(rand()*3)} {
incr stay
} else {
incr change
}
}
puts "Estimate: $stay/$total wins for staying strategy"
puts "Estimate: $change/$total wins for changing strategy"
| using System;
class Program
{
static void Main(string[] args)
{
int switchWins = 0;
int stayWins = 0;
Random gen = new Random();
for(int plays = 0; plays < 1000000; plays++ )
{
int[] doors = {0,0,0};
var winner = gen.Next(3);
... |
Convert the following code from Tcl to C++, ensuring the logic remains intact. | set stay 0; set change 0; set total 10000
for {set i 0} {$i<$total} {incr i} {
if {int(rand()*3) == int(rand()*3)} {
incr stay
} else {
incr change
}
}
puts "Estimate: $stay/$total wins for staying strategy"
puts "Estimate: $change/$total wins for changing strategy"
| #include <iostream>
#include <cstdlib>
#include <ctime>
int randint(int n)
{
return (1.0*n*std::rand())/(1.0+RAND_MAX);
}
int other(int doorA, int doorB)
{
int doorC;
if (doorA == doorB)
{
doorC = randint(2);
if (doorC >= doorA)
++doorC;
}
else
{
for (doorC = 0; doorC == doorA || door... |
Transform the following Tcl implementation into Java, maintaining the same output and logic. | set stay 0; set change 0; set total 10000
for {set i 0} {$i<$total} {incr i} {
if {int(rand()*3) == int(rand()*3)} {
incr stay
} else {
incr change
}
}
puts "Estimate: $stay/$total wins for staying strategy"
puts "Estimate: $change/$total wins for changing strategy"
| import java.util.Random;
public class Monty{
public static void main(String[] args){
int switchWins = 0;
int stayWins = 0;
Random gen = new Random();
for(int plays = 0;plays < 32768;plays++ ){
int[] doors = {0,0,0};
doors[gen.nextInt(3)] = 1;
int choice = gen.nextInt(3);
int shown;
do{
sho... |
Write the same algorithm in Python as shown in this Tcl implementation. | set stay 0; set change 0; set total 10000
for {set i 0} {$i<$total} {incr i} {
if {int(rand()*3) == int(rand()*3)} {
incr stay
} else {
incr change
}
}
puts "Estimate: $stay/$total wins for staying strategy"
puts "Estimate: $change/$total wins for changing strategy"
|
from random import randrange
doors, iterations = 3,100000
def monty_hall(choice, switch=False, doorCount=doors):
door = [False]*doorCount
door[randrange(doorCount)] = True
chosen = door[choice]
unpicked = door
del unpicked[choice]
alternative = True in unpicked
if switch:
retur... |
Change the programming language of this snippet from Tcl to Go without modifying what it does. | set stay 0; set change 0; set total 10000
for {set i 0} {$i<$total} {incr i} {
if {int(rand()*3) == int(rand()*3)} {
incr stay
} else {
incr change
}
}
puts "Estimate: $stay/$total wins for staying strategy"
puts "Estimate: $change/$total wins for changing strategy"
| package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
games := 100000
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var switcherWins, keeperWins, shown int
for i := 0; i < games; i++ {
doors := []int{0, 0, 0}
doors[r.Intn(3)] = 1
choice := r.Intn(3)
for shown = r.Intn(3); shown == ch... |
Ensure the translated PHP code behaves exactly like the original Rust snippet. | extern crate rand;
use rand::Rng;
use rand::seq::SliceRandom;
#[derive(Clone, Copy, PartialEq)]
enum Prize {Goat , Car}
const GAMES: usize = 3_000_000;
fn main() {
let mut switch_wins = 0;
let mut rng = rand::thread_rng();
for _ in 0..GAMES {
let mut doors = [Prize::Goat; 3];
*doors.c... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Maintain the same structure and functionality when rewriting this code in PHP. |
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with ada.Numerics.Discrete_Random;
procedure Monty_Stats is
Num_Iterations : Positive := 100000;
type Action_Type is (Stay, Switch);
type Prize_Type is (Goat, Pig, Car);
type Door_Index is range 1..3;
package Random_Priz... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert this Arturo snippet to PHP and keep its semantics consistent. | stay: 0
swit: 0
loop 1..1000 'i [
lst: shuffle new [1 0 0]
rand: random 0 2
user: lst\[rand]
remove 'lst rand
huh: 0
loop lst 'i [
if zero? i [
remove 'lst huh
break
]
huh: huh + 1
]
if user=1 -> stay: stay+1
if and? [0 < size ls... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Write the same algorithm in PHP as shown in this AWK implementation. |
BEGIN {
srand()
doors = 3
iterations = 10000
EMPTY = "empty"; PRIZE = "prize"
KEEP = "keep"; SWITCH="switch"; RAND="random";
}
function monty_hall( choice, algorithm ) {
for ( i=0; i<doors; i++ ) {
door[i] = EMPTY
}
door[int(rand()*doors)] = PRIZE
chosen = door[choice]
del door[choice... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Can you help me rewrite this code in PHP instead of BBC_Basic, keeping it the same logically? | total% = 10000
FOR trial% = 1 TO total%
prize_door% = RND(3) :
guess_door% = RND(3) :
IF prize_door% = guess_door% THEN
reveal_door% = RND(2)
IF prize_door% = 1 reveal_door% += 1
IF prize_door% = 2 AND reveal_door% = 2 reveal_door% = 3
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert the following code from Clojure to PHP, ensuring the logic remains intact. | (ns monty-hall-problem
(:use [clojure.contrib.seq :only (shuffle)]))
(defn play-game [staying]
(let [doors (shuffle [:goat :goat :car])
choice (rand-int 3)
[a b] (filter #(not= choice %) (range 3))
alternative (if (= :goat (nth doors a)) b a)]
(= :car (nth doors (if staying choice alter... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert this Common_Lisp block to PHP, preserving its control flow and logic. | (defun make-round ()
(let ((array (make-array 3
:element-type 'bit
:initial-element 0)))
(setf (bit array (random 3)) 1)
array))
(defun show-goat (initial-choice array)
(loop for i = (random 3)
when (and (/= initial-choice i)
(... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Please provide an equivalent version of this D code in PHP. | import std.stdio, std.random;
void main() {
int switchWins, stayWins;
while (switchWins + stayWins < 100_000) {
immutable carPos = uniform(0, 3);
immutable pickPos = uniform(0, 3);
int openPos;
do {
openPos = uniform(0, 3);
} while(openPos == pickPos || openPos == carPos... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Write the same algorithm in PHP as shown in this Delphi implementation. | program MontyHall;
uses
System.SysUtils;
const
numGames = 1000000;
var
switchWins, stayWins, plays: Int64;
doors: array[0..2] of Integer;
i, winner, choice, shown: Integer;
begin
switchWins := 0;
stayWins := 0;
for plays := 1 to numGames do
begin
for i := 0 to 2 do
doors[i] ... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Write the same code in PHP as shown below in Elixir. | defmodule MontyHall do
def simulate(n) do
{stay, switch} = simulate(n, 0, 0)
:io.format "Staying wins ~w times (~.3f%)~n", [stay, 100 * stay / n]
:io.format "Switching wins ~w times (~.3f%)~n", [switch, 100 * switch / n]
end
defp simulate(0, stay, switch), do: {stay, switch}
defp simulat... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert the following code from Erlang to PHP, ensuring the logic remains intact. | -module(monty_hall).
-export([main/0]).
main() ->
random:seed(now()),
{WinStay, WinSwitch} = experiment(100000, 0, 0),
io:format("Switching wins ~p times.\n", [WinSwitch]),
io:format("Staying wins ~p times.\n", [WinStay]).
experiment(0, WinStay, WinSwitch) ->
{WinStay, WinSwitch};
experiment(N, WinStay, WinSwit... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert this F# snippet to PHP and keep its semantics consistent. | open System
let monty nSims =
let rnd = new Random()
let SwitchGame() =
let winner, pick = rnd.Next(0,3), rnd.Next(0,3)
if winner <> pick then 1 else 0
let StayGame() =
let winner, pick = rnd.Next(0,3), rnd.Next(0,3)
if winner = pick then 1 else 0
let Wins (f:unit -> in... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Port the following code from Forth to PHP with equivalent syntax and logic. | include random.fs
variable stay-wins
variable switch-wins
: trial
3 random 3 random
= if 1 stay-wins +!
else 1 switch-wins +!
then ;
: trials
0 stay-wins ! 0 switch-wins !
dup 0 do trial loop
cr stay-wins @ . [char] / emit dup . ." staying wins"
cr switch-wins @ . [char] / emit . ." switching... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Rewrite the snippet below in PHP so it works the same as the original Fortran code. | PROGRAM MONTYHALL
IMPLICIT NONE
INTEGER, PARAMETER :: trials = 10000
INTEGER :: i, choice, prize, remaining, show, staycount = 0, switchcount = 0
LOGICAL :: door(3)
REAL :: rnum
CALL RANDOM_SEED
DO i = 1, trials
door = .FALSE.
CALL RANDOM_NUMBER(rnum)
prize ... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Write the same code in PHP as shown below in Haskell. | import System.Random (StdGen, getStdGen, randomR)
trials :: Int
trials = 10000
data Door = Car | Goat deriving Eq
play :: Bool -> StdGen -> (Door, StdGen)
play switch g = (prize, new_g)
where (n, new_g) = randomR (0, 2) g
d1 = [Car, Goat, Goat] !! n
prize = case switch of
False -> d1
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Generate a PHP translation of this Icon snippet without changing its computational steps. | procedure main(arglist)
rounds := integer(arglist[1]) | 10000
doors := '123'
strategy1 := strategy2 := 0
every 1 to rounds do {
goats := doors -- ( car := ?doors )
guess1 := ?doors
show := goats -- guess1
if guess1 == car then strategy1 +:= 1
else strategy2 +:= 1
}
write("Monty Hall simulation... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Produce a language-to-language conversion: from J to PHP, same semantics. | pick=: {~ ?@#
| <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert the following code from Julia to PHP, ensuring the logic remains intact. | using Printf
function play_mh_literal{T<:Integer}(ncur::T=3, ncar::T=1)
ncar < ncur || throw(DomainError())
curtains = shuffle(collect(1:ncur))
cars = curtains[1:ncar]
goats = curtains[(ncar+1):end]
pick = rand(1:ncur)
isstickwin = pick in cars
deleteat!(curtains, findin(curtains, pick))
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Port the following code from Lua to PHP with equivalent syntax and logic. | function playgame(player)
local car = math.random(3)
local pchoice = player.choice()
local function neither(a, b)
local el = math.random(3)
return (el ~= a and el ~= b) and el or neither(a, b)
end
local el = neither(car, pchoice)
if(player.switch) then pchoice = neither(pchoice, el) end
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Generate an equivalent PHP version of this Mathematica code. | montyHall[nGames_] :=
Module[{r, winningDoors, firstChoices, nStayWins, nSwitchWins, s},
r := RandomInteger[{1, 3}, nGames];
winningDoors = r;
firstChoices = r;
nStayWins = Count[Transpose[{winningDoors, firstChoices}], {d_, d_}];
nSwitchWins = nGames - nStayWins;
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Ensure the translated PHP code behaves exactly like the original MATLAB snippet. | function montyHall(numDoors,numSimulations)
assert(numDoors > 2);
function num = randInt(n)
num = floor( n*rand()+1 );
end
switchedDoors = [0 0];
stayed = [0 0];
for i = (1:numSimulations)
availableDoors = (1:numDoors);
winningDoor = randInt(numDoo... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Keep all operations the same but rewrite the snippet in PHP. | import random
randomize()
proc shuffle[T](x: var seq[T]) =
for i in countdown(x.high, 0):
let j = rand(i)
swap(x[i], x[j])
var
stay = 0
switch = 0
for i in 1..1000:
var lst = @[1,0,0]
shuffle(lst)
let ran = rand(2 )
let user = lst[ran]
del lst, ran
var huh = 0
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert this OCaml block to PHP, preserving its control flow and logic. | let trials = 10000
type door = Car | Goat
let play switch =
let n = Random.int 3 in
let d1 = [|Car; Goat; Goat|].(n) in
if not switch then d1
else match d1 with
Car -> Goat
| Goat -> Car
let cars n switch =
let total = ref 0 in
for i = 1 to n do
let prize = play switch in
if priz... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Maintain the same structure and functionality when rewriting this code in PHP. | program MontyHall;
uses
sysutils;
const
NumGames = 1000;
function PickDoor(): Integer;
begin
Exit(Trunc(Random * 3));
end;
var
i: Integer;
PrizeDoor: Integer;
ChosenDoor: Integer;
WinsChangingDoors: Integer = 0;
WinsNotChangingDoors: Integer = 0;
begin
Randomize;
for i := 0 to NumGames - 1 do
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Translate this program into PHP but keep the logic exactly as in Perl. |
use strict;
my $trials = 10000;
my $stay = 0;
my $switch = 0;
foreach (1 .. $trials)
{
my $prize = int(rand 3);
my $chosen = int(rand 3);
my $show;
do { $show = int(rand 3) } while $show == $chosen || $show == $prize;
$stay++ if $prize == $chosen;
$switch++ if $prize == 3 -... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Write the same algorithm in PHP as shown in this PowerShell implementation. |
$intIterations = 10000
$intKept = 0
$intSwitched = 0
Function Play-MontyHall()
{
$objRandom = New-Object -TypeName System.Random
$intWin = $objRandom.Next(1,4)
$intChoice = $objRandom.Next(1,4)
$intLose = $objRandom.Next(1,4)
While (($intLose ... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Generate an equivalent PHP version of this Racket code. | #lang racket
(define (get-last-door a b)
(vector-ref '#(- 2 1
2 - 0
1 0 -)
(+ a (* 3 b))))
(define (run-game strategy)
(define car-door (random 3))
(define first-choice (random 3))
(define revealed-goat
(if (= car-door first-choice)
(let ([r (random 2... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Generate an equivalent PHP version of this COBOL code. | IDENTIFICATION DIVISION.
PROGRAM-ID. monty-hall.
DATA DIVISION.
WORKING-STORAGE SECTION.
78 Num-Games VALUE 1000000.
01 One PIC 9 VALUE 1.
01 Three PIC 9 VALUE 3.
01 doors-area.
... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Produce a functionally identical PHP code for the snippet given in REXX. |
* 30.08.2013 Walter Pachl translated from Java/REXX/PL/I
**********************************************************************/
options replace format comments java crossref savelog symbols nobinary
doors = create_doors
switchWins = 0
stayWins = 0
shown=0
Loop plays=1 To 1000000
doors=0
r=r3()
doors[r]=1
ch... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Change the following Ruby code into PHP without altering its purpose. | n = 10_000
stay = switch = 0
n.times do
doors = [ :goat, :goat, :car ].shuffle
guess = rand(3)
begin shown = rand(3) end while shown == guess || doors[shown] == :car
if doors[guess] == :car
stay += 1
else
switch += 1
end... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert this Scala snippet to PHP and keep its semantics consistent. |
import java.util.Random
fun montyHall(games: Int) {
var switchWins = 0
var stayWins = 0
val rnd = Random()
(1..games).forEach {
val doors = IntArray(3)
doors[rnd.nextInt(3)] = 1
val choice = rnd.nextInt(3)
var shown: Int
do {
shown... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Please provide an equivalent version of this Swift code in PHP. | import Foundation
func montyHall(doors: Int = 3, guess: Int, switch: Bool) -> Bool {
guard doors > 2, guess > 0, guess <= doors else { fatalError() }
let winningDoor = Int.random(in: 1...doors)
return winningDoor == guess ? !`switch` : `switch`
}
var switchResults = [Bool]()
for _ in 0..<1_000 {
let guess ... | <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Convert this Tcl block to PHP, preserving its control flow and logic. | set stay 0; set change 0; set total 10000
for {set i 0} {$i<$total} {incr i} {
if {int(rand()*3) == int(rand()*3)} {
incr stay
} else {
incr change
}
}
puts "Estimate: $stay/$total wins for staying strategy"
puts "Estimate: $change/$total wins for changing strategy"
| <?php
function montyhall($iterations){
$switch_win = 0;
$stay_win = 0;
foreach (range(1, $iterations) as $i){
$doors = array(0, 0, 0);
$doors[array_rand($doors)] = 1;
$choice = array_rand($doors);
do {
$shown = array_rand($doors);
} while($shown == $choice || $doors[$shown] == 1);
$stay_win += ... |
Port the following code from C to Rust with equivalent syntax and logic. |
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#define NumSim 1000000000
void main() {
unsigned long int i,stay=0;
int ChosenDoor,WinningDoor;
bool door[3]={0,0,0};
srand(time(NULL));
for(i=0;i<=NumSim;i++){
WinningD... | extern crate rand;
use rand::Rng;
use rand::seq::SliceRandom;
#[derive(Clone, Copy, PartialEq)]
enum Prize {Goat , Car}
const GAMES: usize = 3_000_000;
fn main() {
let mut switch_wins = 0;
let mut rng = rand::thread_rng();
for _ in 0..GAMES {
let mut doors = [Prize::Goat; 3];
*doors.c... |
Produce a language-to-language conversion: from Java to Rust, same semantics. | import java.util.Random;
public class Monty{
public static void main(String[] args){
int switchWins = 0;
int stayWins = 0;
Random gen = new Random();
for(int plays = 0;plays < 32768;plays++ ){
int[] doors = {0,0,0};
doors[gen.nextInt(3)] = 1;
int choice = gen.nextInt(3);
int shown;
do{
sho... | extern crate rand;
use rand::Rng;
use rand::seq::SliceRandom;
#[derive(Clone, Copy, PartialEq)]
enum Prize {Goat , Car}
const GAMES: usize = 3_000_000;
fn main() {
let mut switch_wins = 0;
let mut rng = rand::thread_rng();
for _ in 0..GAMES {
let mut doors = [Prize::Goat; 3];
*doors.c... |
Port the provided Rust code into Python while preserving the original functionality. | extern crate rand;
use rand::Rng;
use rand::seq::SliceRandom;
#[derive(Clone, Copy, PartialEq)]
enum Prize {Goat , Car}
const GAMES: usize = 3_000_000;
fn main() {
let mut switch_wins = 0;
let mut rng = rand::thread_rng();
for _ in 0..GAMES {
let mut doors = [Prize::Goat; 3];
*doors.c... |
from random import randrange
doors, iterations = 3,100000
def monty_hall(choice, switch=False, doorCount=doors):
door = [False]*doorCount
door[randrange(doorCount)] = True
chosen = door[choice]
unpicked = door
del unpicked[choice]
alternative = True in unpicked
if switch:
retur... |
Write a version of this C++ function in Rust with identical behavior. | #include <iostream>
#include <cstdlib>
#include <ctime>
int randint(int n)
{
return (1.0*n*std::rand())/(1.0+RAND_MAX);
}
int other(int doorA, int doorB)
{
int doorC;
if (doorA == doorB)
{
doorC = randint(2);
if (doorC >= doorA)
++doorC;
}
else
{
for (doorC = 0; doorC == doorA || door... | extern crate rand;
use rand::Rng;
use rand::seq::SliceRandom;
#[derive(Clone, Copy, PartialEq)]
enum Prize {Goat , Car}
const GAMES: usize = 3_000_000;
fn main() {
let mut switch_wins = 0;
let mut rng = rand::thread_rng();
for _ in 0..GAMES {
let mut doors = [Prize::Goat; 3];
*doors.c... |
Generate an equivalent Rust version of this C# code. | using System;
class Program
{
static void Main(string[] args)
{
int switchWins = 0;
int stayWins = 0;
Random gen = new Random();
for(int plays = 0; plays < 1000000; plays++ )
{
int[] doors = {0,0,0};
var winner = gen.Next(3);
... | extern crate rand;
use rand::Rng;
use rand::seq::SliceRandom;
#[derive(Clone, Copy, PartialEq)]
enum Prize {Goat , Car}
const GAMES: usize = 3_000_000;
fn main() {
let mut switch_wins = 0;
let mut rng = rand::thread_rng();
for _ in 0..GAMES {
let mut doors = [Prize::Goat; 3];
*doors.c... |
Preserve the algorithm and functionality while converting the code from Go to Rust. | package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
games := 100000
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var switcherWins, keeperWins, shown int
for i := 0; i < games; i++ {
doors := []int{0, 0, 0}
doors[r.Intn(3)] = 1
choice := r.Intn(3)
for shown = r.Intn(3); shown == ch... | extern crate rand;
use rand::Rng;
use rand::seq::SliceRandom;
#[derive(Clone, Copy, PartialEq)]
enum Prize {Goat , Car}
const GAMES: usize = 3_000_000;
fn main() {
let mut switch_wins = 0;
let mut rng = rand::thread_rng();
for _ in 0..GAMES {
let mut doors = [Prize::Goat; 3];
*doors.c... |
Maintain the same structure and functionality when rewriting this code in C#. | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Polymorphic_Copy is
package Base is
type T is tagged null record;
type T_ptr is access all T'Class;
function Name (X : T) return String;
end Base;
use Base;
package body Base is
function Name (X : T) return String is
begin... | using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Convert this Ada snippet to C and keep its semantics consistent. | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Polymorphic_Copy is
package Base is
type T is tagged null record;
type T_ptr is access all T'Class;
function Name (X : T) return String;
end Base;
use Base;
package body Base is
function Name (X : T) return String is
begin... | int x;
int y = x;
|
Please provide an equivalent version of this Ada code in C++. | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Polymorphic_Copy is
package Base is
type T is tagged null record;
type T_ptr is access all T'Class;
function Name (X : T) return String;
end Base;
use Base;
package body Base is
function Name (X : T) return String is
begin... | #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Translate the given Ada code snippet into Go without altering its behavior. | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Polymorphic_Copy is
package Base is
type T is tagged null record;
type T_ptr is access all T'Class;
function Name (X : T) return String;
end Base;
use Base;
package body Base is
function Name (X : T) return String is
begin... | package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Produce a functionally identical Java code for the snippet given in Ada. | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Polymorphic_Copy is
package Base is
type T is tagged null record;
type T_ptr is access all T'Class;
function Name (X : T) return String;
end Base;
use Base;
package body Base is
function Name (X : T) return String is
begin... | class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Translate the given Ada code snippet into Python without altering its behavior. | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Polymorphic_Copy is
package Base is
type T is tagged null record;
type T_ptr is access all T'Class;
function Name (X : T) return String;
end Base;
use Base;
package body Base is
function Name (X : T) return String is
begin... | import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Write a version of this BBC_Basic function in C with identical behavior. | INSTALL @lib$ + "CLASSLIB"
DIM classT{array#(0), setval, retval}
DEF classT.setval (n%,v) classT.array#(n%) = v : ENDPROC
DEF classT.retval (n%) = classT.array#(n%)
PROC_class(classT{})
RunTimeSize% = RND(100)
DIM classS{array#(RunTimeSize%)}
P... | int x;
int y = x;
|
Transform the following BBC_Basic implementation into C#, maintaining the same output and logic. | INSTALL @lib$ + "CLASSLIB"
DIM classT{array#(0), setval, retval}
DEF classT.setval (n%,v) classT.array#(n%) = v : ENDPROC
DEF classT.retval (n%) = classT.array#(n%)
PROC_class(classT{})
RunTimeSize% = RND(100)
DIM classS{array#(RunTimeSize%)}
P... | using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Port the following code from BBC_Basic to C++ with equivalent syntax and logic. | INSTALL @lib$ + "CLASSLIB"
DIM classT{array#(0), setval, retval}
DEF classT.setval (n%,v) classT.array#(n%) = v : ENDPROC
DEF classT.retval (n%) = classT.array#(n%)
PROC_class(classT{})
RunTimeSize% = RND(100)
DIM classS{array#(RunTimeSize%)}
P... | #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Translate the given BBC_Basic code snippet into Java without altering its behavior. | INSTALL @lib$ + "CLASSLIB"
DIM classT{array#(0), setval, retval}
DEF classT.setval (n%,v) classT.array#(n%) = v : ENDPROC
DEF classT.retval (n%) = classT.array#(n%)
PROC_class(classT{})
RunTimeSize% = RND(100)
DIM classS{array#(RunTimeSize%)}
P... | class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Transform the following BBC_Basic implementation into Python, maintaining the same output and logic. | INSTALL @lib$ + "CLASSLIB"
DIM classT{array#(0), setval, retval}
DEF classT.setval (n%,v) classT.array#(n%) = v : ENDPROC
DEF classT.retval (n%) = classT.array#(n%)
PROC_class(classT{})
RunTimeSize% = RND(100)
DIM classS{array#(RunTimeSize%)}
P... | import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Write the same code in Go as shown below in BBC_Basic. | INSTALL @lib$ + "CLASSLIB"
DIM classT{array#(0), setval, retval}
DEF classT.setval (n%,v) classT.array#(n%) = v : ENDPROC
DEF classT.retval (n%) = classT.array#(n%)
PROC_class(classT{})
RunTimeSize% = RND(100)
DIM classS{array#(RunTimeSize%)}
P... | package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Translate the given Common_Lisp code snippet into C without altering its behavior. | (defstruct super foo)
(defstruct (sub (:include super)) bar)
(defgeneric frob (thing))
(defmethod frob ((super super))
(format t "~&Super has foo = ~w." (super-foo super)))
(defmethod frob ((sub sub))
(format t "~&Sub has foo = ~w, bar = ~w."
(sub-foo sub) (sub-bar sub)))
| int x;
int y = x;
|
Generate a C# translation of this Common_Lisp snippet without changing its computational steps. | (defstruct super foo)
(defstruct (sub (:include super)) bar)
(defgeneric frob (thing))
(defmethod frob ((super super))
(format t "~&Super has foo = ~w." (super-foo super)))
(defmethod frob ((sub sub))
(format t "~&Sub has foo = ~w, bar = ~w."
(sub-foo sub) (sub-bar sub)))
| using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Keep all operations the same but rewrite the snippet in C++. | (defstruct super foo)
(defstruct (sub (:include super)) bar)
(defgeneric frob (thing))
(defmethod frob ((super super))
(format t "~&Super has foo = ~w." (super-foo super)))
(defmethod frob ((sub sub))
(format t "~&Sub has foo = ~w, bar = ~w."
(sub-foo sub) (sub-bar sub)))
| #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Change the following Common_Lisp code into Java without altering its purpose. | (defstruct super foo)
(defstruct (sub (:include super)) bar)
(defgeneric frob (thing))
(defmethod frob ((super super))
(format t "~&Super has foo = ~w." (super-foo super)))
(defmethod frob ((sub sub))
(format t "~&Sub has foo = ~w, bar = ~w."
(sub-foo sub) (sub-bar sub)))
| class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Generate an equivalent Python version of this Common_Lisp code. | (defstruct super foo)
(defstruct (sub (:include super)) bar)
(defgeneric frob (thing))
(defmethod frob ((super super))
(format t "~&Super has foo = ~w." (super-foo super)))
(defmethod frob ((sub sub))
(format t "~&Sub has foo = ~w, bar = ~w."
(sub-foo sub) (sub-bar sub)))
| import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Produce a language-to-language conversion: from Common_Lisp to Go, same semantics. | (defstruct super foo)
(defstruct (sub (:include super)) bar)
(defgeneric frob (thing))
(defmethod frob ((super super))
(format t "~&Super has foo = ~w." (super-foo super)))
(defmethod frob ((sub sub))
(format t "~&Sub has foo = ~w, bar = ~w."
(sub-foo sub) (sub-bar sub)))
| package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Port the following code from D to C with equivalent syntax and logic. | class T {
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T; }
}
class S : T {
override string toString() { return "I'm the instance of S"; }
override T duplicate() { return new S; }
}
void main () {
import std.stdio;
T orig = new S;
T copy = orig... | int x;
int y = x;
|
Generate a C# translation of this D snippet without changing its computational steps. | class T {
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T; }
}
class S : T {
override string toString() { return "I'm the instance of S"; }
override T duplicate() { return new S; }
}
void main () {
import std.stdio;
T orig = new S;
T copy = orig... | using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Convert this D block to C++, preserving its control flow and logic. | class T {
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T; }
}
class S : T {
override string toString() { return "I'm the instance of S"; }
override T duplicate() { return new S; }
}
void main () {
import std.stdio;
T orig = new S;
T copy = orig... | #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Port the provided D code into Java while preserving the original functionality. | class T {
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T; }
}
class S : T {
override string toString() { return "I'm the instance of S"; }
override T duplicate() { return new S; }
}
void main () {
import std.stdio;
T orig = new S;
T copy = orig... | class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Rewrite the snippet below in Python so it works the same as the original D code. | class T {
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T; }
}
class S : T {
override string toString() { return "I'm the instance of S"; }
override T duplicate() { return new S; }
}
void main () {
import std.stdio;
T orig = new S;
T copy = orig... | import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Port the following code from D to Go with equivalent syntax and logic. | class T {
override string toString() { return "I'm the instance of T"; }
T duplicate() { return new T; }
}
class S : T {
override string toString() { return "I'm the instance of S"; }
override T duplicate() { return new S; }
}
void main () {
import std.stdio;
T orig = new S;
T copy = orig... | package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Maintain the same structure and functionality when rewriting this code in C. | program PolymorphicCopy;
type
T = class
function Name:String; virtual;
function Clone:T; virtual;
end;
S = class(T)
function Name:String; override;
function Clone:T; override;
end;
function T.Name :String; begin Exit('T') end;
function T.Clone:T; begin Exit(T.Create)end;
function S.... | int x;
int y = x;
|
Write the same code in C# as shown below in Delphi. | program PolymorphicCopy;
type
T = class
function Name:String; virtual;
function Clone:T; virtual;
end;
S = class(T)
function Name:String; override;
function Clone:T; override;
end;
function T.Name :String; begin Exit('T') end;
function T.Clone:T; begin Exit(T.Create)end;
function S.... | using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Generate a C++ translation of this Delphi snippet without changing its computational steps. | program PolymorphicCopy;
type
T = class
function Name:String; virtual;
function Clone:T; virtual;
end;
S = class(T)
function Name:String; override;
function Clone:T; override;
end;
function T.Name :String; begin Exit('T') end;
function T.Clone:T; begin Exit(T.Create)end;
function S.... | #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Write a version of this Delphi function in Java with identical behavior. | program PolymorphicCopy;
type
T = class
function Name:String; virtual;
function Clone:T; virtual;
end;
S = class(T)
function Name:String; override;
function Clone:T; override;
end;
function T.Name :String; begin Exit('T') end;
function T.Clone:T; begin Exit(T.Create)end;
function S.... | class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Generate a Python translation of this Delphi snippet without changing its computational steps. | program PolymorphicCopy;
type
T = class
function Name:String; virtual;
function Clone:T; virtual;
end;
S = class(T)
function Name:String; override;
function Clone:T; override;
end;
function T.Name :String; begin Exit('T') end;
function T.Clone:T; begin Exit(T.Create)end;
function S.... | import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Port the following code from Delphi to Go with equivalent syntax and logic. | program PolymorphicCopy;
type
T = class
function Name:String; virtual;
function Clone:T; virtual;
end;
S = class(T)
function Name:String; override;
function Clone:T; override;
end;
function T.Name :String; begin Exit('T') end;
function T.Clone:T; begin Exit(T.Create)end;
function S.... | package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Convert this F# snippet to C and keep its semantics consistent. | type T() =
member x.Clone() = x.MemberwiseClone() :?> T
abstract Print : unit -> unit
default x.Print() = printfn "I'm a T!"
type S() =
inherit T()
override x.Print() = printfn "I'm an S!"
let s = new S()
let s2 = s.Clone()
s2.Print()
| int x;
int y = x;
|
Generate an equivalent C# version of this F# code. | type T() =
member x.Clone() = x.MemberwiseClone() :?> T
abstract Print : unit -> unit
default x.Print() = printfn "I'm a T!"
type S() =
inherit T()
override x.Print() = printfn "I'm an S!"
let s = new S()
let s2 = s.Clone()
s2.Print()
| using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Maintain the same structure and functionality when rewriting this code in C++. | type T() =
member x.Clone() = x.MemberwiseClone() :?> T
abstract Print : unit -> unit
default x.Print() = printfn "I'm a T!"
type S() =
inherit T()
override x.Print() = printfn "I'm an S!"
let s = new S()
let s2 = s.Clone()
s2.Print()
| #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Write a version of this F# function in Java with identical behavior. | type T() =
member x.Clone() = x.MemberwiseClone() :?> T
abstract Print : unit -> unit
default x.Print() = printfn "I'm a T!"
type S() =
inherit T()
override x.Print() = printfn "I'm an S!"
let s = new S()
let s2 = s.Clone()
s2.Print()
| class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Convert the following code from F# to Python, ensuring the logic remains intact. | type T() =
member x.Clone() = x.MemberwiseClone() :?> T
abstract Print : unit -> unit
default x.Print() = printfn "I'm a T!"
type S() =
inherit T()
override x.Print() = printfn "I'm an S!"
let s = new S()
let s2 = s.Clone()
s2.Print()
| import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Rewrite this program in Go while keeping its functionality equivalent to the F# version. | type T() =
member x.Clone() = x.MemberwiseClone() :?> T
abstract Print : unit -> unit
default x.Print() = printfn "I'm a T!"
type S() =
inherit T()
override x.Print() = printfn "I'm an S!"
let s = new S()
let s2 = s.Clone()
s2.Print()
| package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Translate this program into C but keep the logic exactly as in Factor. | USING: classes kernel prettyprint serialize ;
TUPLE: A ;
TUPLE: C < A ;
: serial-clone ( obj -- obj' ) object>bytes bytes>object ;
C new
[ clone ]
[ serial-clone ] bi [ class . ] bi@
| int x;
int y = x;
|
Can you help me rewrite this code in C# instead of Factor, keeping it the same logically? | USING: classes kernel prettyprint serialize ;
TUPLE: A ;
TUPLE: C < A ;
: serial-clone ( obj -- obj' ) object>bytes bytes>object ;
C new
[ clone ]
[ serial-clone ] bi [ class . ] bi@
| using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Maintain the same structure and functionality when rewriting this code in C++. | USING: classes kernel prettyprint serialize ;
TUPLE: A ;
TUPLE: C < A ;
: serial-clone ( obj -- obj' ) object>bytes bytes>object ;
C new
[ clone ]
[ serial-clone ] bi [ class . ] bi@
| #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Produce a functionally identical Java code for the snippet given in Factor. | USING: classes kernel prettyprint serialize ;
TUPLE: A ;
TUPLE: C < A ;
: serial-clone ( obj -- obj' ) object>bytes bytes>object ;
C new
[ clone ]
[ serial-clone ] bi [ class . ] bi@
| class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Rewrite this program in Python while keeping its functionality equivalent to the Factor version. | USING: classes kernel prettyprint serialize ;
TUPLE: A ;
TUPLE: C < A ;
: serial-clone ( obj -- obj' ) object>bytes bytes>object ;
C new
[ clone ]
[ serial-clone ] bi [ class . ] bi@
| import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Change the programming language of this snippet from Factor to Go without modifying what it does. | USING: classes kernel prettyprint serialize ;
TUPLE: A ;
TUPLE: C < A ;
: serial-clone ( obj -- obj' ) object>bytes bytes>object ;
C new
[ clone ]
[ serial-clone ] bi [ class . ] bi@
| package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Transform the following Forth implementation into C, maintaining the same output and logic. | include lib/memcell.4th
include 4pp/lib/foos.4pp
:token fork dup allocated dup swap >r swap over r> smove ;
:: T
class
method: print
method: clone
e... | int x;
int y = x;
|
Ensure the translated C# code behaves exactly like the original Forth snippet. | include lib/memcell.4th
include 4pp/lib/foos.4pp
:token fork dup allocated dup swap >r swap over r> smove ;
:: T
class
method: print
method: clone
e... | using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Translate the given Forth code snippet into C++ without altering its behavior. | include lib/memcell.4th
include 4pp/lib/foos.4pp
:token fork dup allocated dup swap >r swap over r> smove ;
:: T
class
method: print
method: clone
e... | #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Generate a Java translation of this Forth snippet without changing its computational steps. | include lib/memcell.4th
include 4pp/lib/foos.4pp
:token fork dup allocated dup swap >r swap over r> smove ;
:: T
class
method: print
method: clone
e... | class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Preserve the algorithm and functionality while converting the code from Forth to Python. | include lib/memcell.4th
include 4pp/lib/foos.4pp
:token fork dup allocated dup swap >r swap over r> smove ;
:: T
class
method: print
method: clone
e... | import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Write a version of this Forth function in Go with identical behavior. | include lib/memcell.4th
include 4pp/lib/foos.4pp
:token fork dup allocated dup swap >r swap over r> smove ;
:: T
class
method: print
method: clone
e... | package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Ensure the translated C# code behaves exactly like the original Fortran snippet. |
module polymorphic_copy_example_module
implicit none
private
public :: T,S
type, abstract :: T
contains
procedure (T_procedure1), deferred, pass :: identify
procedure (T_procedure2), deferred, pass :: duplicate
end type T
abstract interface
subroutine T_procedure1(this)
... | using System;
class T
{
public virtual string Name()
{
return "T";
}
public virtual T Clone()
{
return new T();
}
}
class S : T
{
public override string Name()
{
return "S";
}
public override T Clone()
{
return new S();
}
}
class Progr... |
Write the same algorithm in C++ as shown in this Fortran implementation. |
module polymorphic_copy_example_module
implicit none
private
public :: T,S
type, abstract :: T
contains
procedure (T_procedure1), deferred, pass :: identify
procedure (T_procedure2), deferred, pass :: duplicate
end type T
abstract interface
subroutine T_procedure1(this)
... | #include <iostream>
class T
{
public:
virtual void identify() { std::cout << "I am a genuine T" << std::endl; }
virtual T* clone() { return new T(*this); }
virtual ~T() {}
};
class S: public T
{
public:
virtual void identify() { std::cout << "I am an S" << std::endl; }
virtual S* clone() { return new S(*thi... |
Change the following Fortran code into C without altering its purpose. |
module polymorphic_copy_example_module
implicit none
private
public :: T,S
type, abstract :: T
contains
procedure (T_procedure1), deferred, pass :: identify
procedure (T_procedure2), deferred, pass :: duplicate
end type T
abstract interface
subroutine T_procedure1(this)
... | int x;
int y = x;
|
Port the following code from Fortran to Go with equivalent syntax and logic. |
module polymorphic_copy_example_module
implicit none
private
public :: T,S
type, abstract :: T
contains
procedure (T_procedure1), deferred, pass :: identify
procedure (T_procedure2), deferred, pass :: duplicate
end type T
abstract interface
subroutine T_procedure1(this)
... | package main
import (
"fmt"
"reflect"
)
type i interface {
identify() string
}
type t float64
type s struct {
t
kōan string
}
type r struct {
t
ch chan int
}
func (x t) identify() string {
return "I'm a t!"
}
func (x s) identify() string {
return "I'm an s!"
}
f... |
Preserve the algorithm and functionality while converting the code from Fortran to Java. |
module polymorphic_copy_example_module
implicit none
private
public :: T,S
type, abstract :: T
contains
procedure (T_procedure1), deferred, pass :: identify
procedure (T_procedure2), deferred, pass :: duplicate
end type T
abstract interface
subroutine T_procedure1(this)
... | class T implements Cloneable {
public String name() { return "T"; }
public T copy() {
try {
return (T)super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
class S extends T {
public String name() { return "S"; }
}
public class Polym... |
Translate this program into Python but keep the logic exactly as in Fortran. |
module polymorphic_copy_example_module
implicit none
private
public :: T,S
type, abstract :: T
contains
procedure (T_procedure1), deferred, pass :: identify
procedure (T_procedure2), deferred, pass :: duplicate
end type T
abstract interface
subroutine T_procedure1(this)
... | import copy
class T:
def classname(self):
return self.__class__.__name__
def __init__(self):
self.myValue = "I'm a T."
def speak(self):
print self.classname(), 'Hello', self.myValue
def clone(self):
return copy.copy(self)
class S1(T):
def speak(self):
print self.classn... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.