Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Rewrite this program in PHP while keeping its functionality equivalent to the PowerShell version.
for ($i = 1; $i -le 100; $i++) { if ($i % 15 -eq 0) { "FizzBuzz" } elseif ($i % 5 -eq 0) { "Buzz" } elseif ($i % 3 -eq 0) { "Fizz" } else { $i } }
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Can you help me rewrite this code in PHP instead of Racket, keeping it the same logically?
#lang racket (for ([n (in-range 1 101)]) (displayln (match (gcd n 15) [15 "fizzbuzz"] [3 "fizz"] [5 "buzz"] [_ n])))
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Translate the given COBOL code snippet into PHP without altering its behavior.
* FIZZBUZZ.COB * cobc -x -g FIZZBUZZ.COB * IDENTIFICATION DIVISION. PROGRAM-ID. fizzbuzz. DATA DIVISION. WORKING-STORAGE SECTION. 01 CNT PIC 9(03) VALUE 1. 01 REM PIC 9(03) VALUE 0. 01 QUOTIENT PIC 9(03)...
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Produce a language-to-language conversion: from REXX to PHP, same semantics.
loop j=1 for 100 select when j//15==0 then say 'FizzBuzz' when j//5==0 then say 'Buzz' when j//3==0 then say 'Fizz' otherwise say j.right(4) end end
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Change the following Ruby code into PHP without altering its purpose.
1.upto(100) do |v| p fizz_buzz(v) end def fizz_buzz(value) word = "" word += "fizz" if value % 3 == 0 word += "buzz" if value % 5 == 0 word += value.to_s if word.empty? word end
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Ensure the translated PHP code behaves exactly like the original Scala snippet.
fun fizzBuzz() { for (number in 1..100) { println( when { number % 15 == 0 -> "FizzBuzz" number % 3 == 0 -> "Fizz" number % 5 == 0 -> "Buzz" else -> number } ) } }
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Rewrite this program in PHP while keeping its functionality equivalent to the Swift version.
for i in 1...100 { switch (i % 3, i % 5) { case (0, 0): print("FizzBuzz") case (0, _): print("Fizz") case (_, 0): print("Buzz") default: print(i) } }
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Rewrite this program in PHP while keeping its functionality equivalent to the Tcl version.
for {set i 1} {$i <= 100} {inc i} { set show "" if {[expr $i % 3 == 0]} {set show "Fizz"} if {[expr $i % 5 == 0]} {set show $show"Buzz"} if {[expr [length $show] == 0]} {set show $i} print $show }
<?php for ($i = 1; $i <= 100; $i++) { if (!($i % 15)) echo "FizzBuzz\n"; else if (!($i % 3)) echo "Fizz\n"; else if (!($i % 5)) echo "Buzz\n"; else echo "$i\n"; } ?>
Generate a Rust translation of this C snippet without changing its computational steps.
int i = 0 ; char B[88] ; while ( i++ < 100 ) !sprintf( B, "%s%s", i%3 ? "":"Fizz", i%5 ? "":"Buzz" ) ? sprintf( B, "%d", i ):0, printf( ", %s", B );
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), (_, _) => println!("{}", i), } } }
Rewrite the snippet below in Rust so it works the same as the original C# code.
class Program { public void FizzBuzzGo() { Boolean Fizz = false; Boolean Buzz = false; for (int count = 1; count <= 100; count ++) { Fizz = count % 3 == 0; Buzz = count % 5 == 0; if (Fizz && Buzz) { Console.WriteLine...
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), (_, _) => println!("{}", i), } } }
Translate this program into Rust but keep the logic exactly as in Java.
module FizzBuzz { void run() { @Inject Console console; for (Int x : 1..100) { console.print(switch (x % 3, x % 5) { case (0, 0): "FizzBuzz"; case (0, _): "Fizz"; case (_, 0): "Buzz"; ...
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), (_, _) => println!("{}", i), } } }
Change the programming language of this snippet from Go to Rust without modifying what it does.
package main import "fmt" func main() { for i := 1; i <= 100; i++ { switch { case i%15==0: fmt.Println("FizzBuzz") case i%3==0: fmt.Println("Fizz") case i%5==0: fmt.Println("Buzz") default: fmt.Println(i) } } }
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), (_, _) => println!("{}", i), } } }
Generate an equivalent Python version of this Rust code.
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), (_, _) => println!("{}", i), } } }
for i in xrange(1, 101): if i % 15 == 0: print "FizzBuzz" elif i % 3 == 0: print "Fizz" elif i % 5 == 0: print "Buzz" else: print i
Write a version of this Rust function in VB with identical behavior.
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), (_, _) => println!("{}", i), } } }
Option Explicit Sub FizzBuzz() Dim Tb(1 To 100) As Variant Dim i As Integer For i = 1 To 100 If i Mod 15 = 0 Then Tb(i) = "FizzBuzz" ElseIf i Mod 5 = 0 Then Tb(i) = "Buzz" ElseIf i Mod 3 = 0 Then Tb(i) = "Fizz" Else Tb(i) = i ...
Rewrite this program in Rust while keeping its functionality equivalent to the C++ version.
#include <iostream> #include <chrono> int main() { int fizz = 0, buzz = 0, fizzbuzz = 0; bool isFizz = false; auto startTime = std::chrono::high_resolution_clock::now(); for (unsigned int i = 1; i <= 4000000000; i++) { isFizz = false; if (i % 3 == 0) { isFizz = true; fizz++; } if (i % 5 == 0) {...
fn main() { for i in 1..=100 { match (i % 3, i % 5) { (0, 0) => println!("fizzbuzz"), (0, _) => println!("fizz"), (_, 0) => println!("buzz"), (_, _) => println!("{}", i), } } }
Translate the given Ada code snippet into C# without altering its behavior.
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...
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); ...
Change the following Ada code into C without altering its purpose.
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...
#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...
Ensure the translated C++ code behaves exactly like the original Ada snippet.
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...
#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...
Write the same algorithm in Go as shown in this Ada implementation.
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...
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...
Translate this program into Java but keep the logic exactly as in Ada.
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...
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...
Rewrite the snippet below in Python so it works the same as the original Ada code.
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...
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...
Produce a functionally identical C code for the snippet given in Arturo.
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...
#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...
Produce a language-to-language conversion: from Arturo to C#, same semantics.
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...
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 this Arturo block to C++, preserving its control flow and logic.
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...
#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...
Translate this program into Java but keep the logic exactly as in Arturo.
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...
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 code in Python as shown below in Arturo.
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...
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...
Produce a functionally identical Go code for the snippet given in Arturo.
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...
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...
Preserve the algorithm and functionality while converting the code from AWK to C.
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...
#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...
Translate the given AWK code snippet into C# without altering its behavior.
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...
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); ...
Change the programming language of this snippet from AWK to C++ without modifying what it does.
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...
#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 AWK implementation into Java, maintaining the same output and logic.
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...
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 a version of this AWK function in Python with identical behavior.
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...
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...
Port the provided AWK code into Go while preserving the original functionality.
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...
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...
Please provide an equivalent version of this BBC_Basic code in C.
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 ...
#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...
Please provide an equivalent version of this BBC_Basic code in C#.
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 ...
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); ...
Maintain the same structure and functionality when rewriting this code in C++.
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 ...
#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...
Change the programming language of this snippet from BBC_Basic to Java without modifying what it does.
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 ...
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...
Keep all operations the same but rewrite the snippet in Python.
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 ...
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...
Rewrite the snippet below in Go so it works the same as the original BBC_Basic code.
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 ...
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...
Generate an equivalent C version of this Clojure code.
(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...
#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...
Port the following code from Clojure to C# with equivalent syntax and logic.
(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...
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); ...
Change the following Clojure code into C++ without altering its purpose.
(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...
#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...
Generate an equivalent Java version of this Clojure code.
(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...
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...
Translate the given Clojure code snippet into Python without altering its behavior.
(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...
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 following Clojure code into Go without altering its purpose.
(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...
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...
Write a version of this Common_Lisp function in C with identical behavior.
(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) (...
#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...
Generate an equivalent C# version of this Common_Lisp code.
(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) (...
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); ...
Keep all operations the same but rewrite the snippet in C++.
(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) (...
#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...
Can you help me rewrite this code in Java instead of Common_Lisp, keeping it the same logically?
(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) (...
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...
Rewrite the snippet below in Python so it works the same as the original Common_Lisp code.
(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) (...
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...
Generate a Go translation of this Common_Lisp snippet without changing its computational steps.
(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) (...
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...
Produce a language-to-language conversion: from D to C, same semantics.
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...
#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...
Maintain the same structure and functionality when rewriting this code in C#.
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...
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); ...
Can you help me rewrite this code in C++ instead of D, keeping it the same logically?
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...
#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...
Change the following D code into Java without altering its purpose.
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...
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...
Can you help me rewrite this code in Python instead of D, keeping it the same logically?
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...
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...
Preserve the algorithm and functionality while converting the code from D to Go.
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...
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...
Preserve the algorithm and functionality while converting the code from Delphi to C.
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] ...
#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...
Convert the following code from Delphi to C#, ensuring the logic remains intact.
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] ...
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); ...
Translate the given Delphi code snippet into C++ without altering its behavior.
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] ...
#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...
Generate an equivalent Java version of this Delphi code.
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] ...
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...
Convert this Delphi block to Python, preserving its control flow and logic.
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] ...
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 following Delphi code into Go without altering its purpose.
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] ...
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...
Port the following code from Elixir to C with equivalent syntax and logic.
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...
#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...
Ensure the translated C# code behaves exactly like the original Elixir snippet.
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...
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); ...
Change the programming language of this snippet from Elixir to C++ without modifying what it does.
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...
#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...
Write the same algorithm in Java as shown in this Elixir implementation.
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...
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...
Ensure the translated Python code behaves exactly like the original Elixir snippet.
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...
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...
Ensure the translated Go code behaves exactly like the original Elixir snippet.
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...
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...
Preserve the algorithm and functionality while converting the code from Erlang to C.
-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...
#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...
Produce a functionally identical C# code for the snippet given in Erlang.
-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...
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); ...
Maintain the same structure and functionality when rewriting this code in C++.
-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...
#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...
Please provide an equivalent version of this Erlang code in Java.
-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...
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 a version of this Erlang function in Python with identical behavior.
-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...
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...
Can you help me rewrite this code in Go instead of Erlang, keeping it the same logically?
-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...
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...
Port the provided F# code into C while preserving the original functionality.
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...
#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...
Write the same code in C# as shown below in F#.
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...
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 this F# snippet to C++ 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...
#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...
Translate this program into Java but keep the logic exactly as in F#.
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...
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...
Keep all operations the same but rewrite the snippet in Python.
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...
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...
Convert the following code from F# to Go, ensuring the logic remains intact.
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...
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...
Translate the given Forth code snippet into C without altering its behavior.
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...
#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...
Maintain the same structure and functionality when rewriting this code in C#.
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...
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); ...
Port the following code from Forth to C++ 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...
#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...
Write a version of this Forth function in Java with identical behavior.
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...
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...
Port the following code from Forth to Python 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...
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...
Maintain the same structure and functionality when rewriting this code in Go.
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...
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...
Translate this program into C# but keep the logic exactly as in Fortran.
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 ...
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); ...
Ensure the translated C++ code behaves exactly like the original Fortran snippet.
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 ...
#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...
Ensure the translated C code behaves exactly like the original Fortran snippet.
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 ...
#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...
Keep all operations the same but rewrite the snippet in Go.
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 ...
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...
Translate the given Fortran code snippet into Java without altering its behavior.
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 ...
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...
Port the provided Fortran code into Python while preserving the original functionality.
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 ...
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 the same algorithm in PHP as shown in this Fortran implementation.
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 += ...
Produce a language-to-language conversion: from Haskell to C, same semantics.
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 ...
#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...
Generate an equivalent C# version of this Haskell code.
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 ...
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); ...
Ensure the translated C++ code behaves exactly like the original Haskell snippet.
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 ...
#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...
Translate the given Haskell code snippet into Java without altering its behavior.
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 ...
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...
Convert this Haskell snippet to Python and keep its semantics consistent.
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 ...
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...
Rewrite this program in Go while keeping its functionality equivalent to the Haskell version.
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 ...
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...