Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,37 @@
import math
randomize()
proc shuffle[T](x: var seq[T]) =
for i in countdown(x.high, 0):
let j = random(i + 1)
swap(x[i], x[j])
# 1 represents a car
# 0 represent a goat
var
stay = 0 # amount won if stay in the same position
switch = 0 # amount won if you switch
for i in 1..1000:
var lst = @[1,0,0] # one car and two goats
shuffle(lst) # shuffles the list randomly
let ran = random(3) # gets a random number for the random guess
let user = lst[ran] # storing the random guess
del lst, ran # deleting the random guess
var huh = 0
for i in lst: # getting a value 0 and deleting it
if i == 0:
del lst, huh # deletes a goat when it finds it
break
inc huh
if user == 1: # if the original choice is 1 then stay adds 1
inc stay
if lst[0] == 1: # if the switched value is 1 then switch adds 1
inc switch
echo "Stay = ",stay
echo "Switch = ",switch

View file

@ -0,0 +1,15 @@
integer swapWins = 0, stayWins = 0, winner, choice, reveal, other
atom t0 = time()
for game=1 to 1_000_000 do
winner = rand(3)
choice = rand(3)
while 1 do
reveal = rand(3)
if reveal!=winner and reveal!=choice then exit end if
end while
stayWins += (choice==winner)
other = 6-choice-reveal -- (as 1+2+3=6, and reveal!=choice)
swapWins += (other==winner)
end for
printf(1, "Stay: %,d\nSwap: %,d\nTime: %3.2fs\n",{stayWins,swapWins,time()-t0})

View file

@ -0,0 +1,6 @@
montyHall(n) ==
wd:=[1+random(3) for j in 1..n]
fc:=[1+random(3) for j in 1..n]
st:=reduce(_+,[1 for j in 1..n | wd.j=fc.j])
p:=(st/n)::DoubleFloat
FORMAT(nil,"stay: ~A, switch: ~A",p,1-p)$Lisp

View file

@ -0,0 +1,21 @@
var n = 1000; # number of times to play
var switchWins = (var stayWins = 0); # sum of each strategy's wins
n.times { # play the game n times
var prize = 3.rand.int;
var chosen = 3.rand.int;
var show;
do {
show = 3.rand.int
} while (show ~~ [chosen, prize]);
given(chosen) {
when (prize) { stayWins += 1 }
when ([3 - show - prize]) { switchWins += 1 }
default { die "~ error ~" }
}
}
say ("Staying wins %.2f%% of the time." % (100.0 * stayWins / n));
say ("Switching wins %.2f%% of the time." % (100.0 * switchWins / n));