Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 48
2+3 == 7
False
#2+3 == 7 5 == 5
True
[1..100]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
for i in [5,6,7]: print 2*i +17
27 29 31
n = 1000000 count0 = 0.0 count1 = 0.0 count2 = 0.0 for i in [1..n]: guess = randint(0,2) if guess == 0: #write a comment here count0 = count0 + 1 if guess == 1: #write a comment here count1 = count1 + 1 if guess == 2: #write a comment here count2 = count2 + 1 print("The percentage of 0s, 1s, 2s is {}, {}, {}").format(count0/n,count1/n,count2/n)
The percentage of 0s, 1s, 2s is 0.333392000000000, 0.333392000000000, 0.333216000000000
L=[0,1,2] L.remove(1) L
[0, 2]
L=[5,6,7] L[0] L[2]
5 7
L
[5, 6, 7]
9 in L
False
5 in L
True
doors=[0,2] doors.remove(2) doors doors[0]
[0] 0
#represent the 3 doors with the Integers 0, 1 and 2 # let Sage choose the location of the car by randomly choosing one of 0,1 or 2 car = randint(0,2) guess1 = 2 #represent the doors with list [0,1,2], remove the doors = [0,1,2] doors.remove(guess1) if car in doors: doors.remove(car) monty = doors[0] else: monty = doors[randint(0,1)] print("Monty opens door {}").format(monty)
Monty opens door 1
guess2 = 0 if car == guess2: print("You win! You get the car") else: print("You lose!")
You win! You get the car
#here's a version that makes the contestants' guess *randomly* #represent the 3 doors with the Integers 0, 1 and 2 # let Sage choose the location of the car by randomly choosing one of 0,1 or 2 car = randint(0,2) #let's print where the car is to see how things are looking print("The car is really behind door {}".format(car)) guess1 = randint(0,2) #let's print the guess to see how things are looking print("I guess the car is behind door {}".format(guess1)) #represent the doors with list [0,1,2], remove the doors with the contestant's guess and the one with the car doors = [0,1,2] doors.remove(guess1) #use the variable *monty* to represent the door that monty opnes if car in doors: doors.remove(car) monty = doors[0] else: monty = doors[randint(0,1)] print("Monty opens door {}").format(monty) #here is where we implement the strategy #Strategy 1 was to *not* switch the guess - so we stick with guess1 guess2 = guess1 if car == guess2: print("You win! You get the car") else: print("You lose!")
The car is really behind door 2 I guess the car is behind door 1 Monty opens door 0 You lose!
#now lets try Strategy 2 #here's a version that makes the contestants' guess *randomly* #represent the 3 doors with the Integers 0, 1 and 2 # let Sage choose the location of the car by randomly choosing one of 0,1 or 2 car = randint(0,2) #let's print where the car is to see how things are looking print("The car is really behind door {}".format(car)) guess1 = randint(0,2) #let's print the guess to see how things are looking print("I guess the car is behind door {}".format(guess1)) #represent the doors with list [0,1,2], remove the doors with the contestant's guess and the one with the car doors = [0,1,2] doors.remove(guess1) #use the variable *monty* to represent the door that monty opnes if car in doors: doors.remove(car) monty = doors[0] else: monty = doors[randint(0,1)] print("Monty opens door {}").format(monty) #here is where we implement the strategy #Strategy 2 was to *switch* the guess - so we stick with guess1 doors = [0,1,2] doors.remove(guess1) doors.remove(monty) guess2 = doors[0] #let's print the guess to make sure this does what we want print("I switch my guess to door {}".format(guess2)) if car == guess2: print("You win! You get the car") else: print("You lose!")
The car is really behind door 1 I guess the car is behind door 2 Monty opens door 0 I switch my guess to door 1 You win! You get the car
#now let's try Strategy #1 n times and see how we do in percentage #we remove all print statements as we don't want to print them possible thousands of times! n = 1000000 #we'll define a counter *count* to record the number of wins count = 0 #we need a *loop* in order to get the n repetitions for i in [1..n]: #represent the 3 doors with the Integers 0, 1 and 2 # let Sage choose the location of the car by randomly choosing one of 0,1 or 2 car = randint(0,2) guess1 = randint(0,2) #represent the doors with list [0,1,2], remove the doors with the contestant's guess and the one with the car doors = [0,1,2] doors.remove(guess1) #use the variable *monty* to represent the door that monty opnes if car in doors: doors.remove(car) monty = doors[0] else: monty = doors[randint(0,1)] #here is where we implement the strategy #Strategy 1 was to *not* switch the guess - so we stick with guess1 guess2 = guess1 if car == guess2: count = count + 1 print("The percentage of success using Strategy #1 with {} trials is {}".format(n,count/n*100.0))
The percentage of success using Strategy #1 with 1000000 trials is 33.3023000000000
#now let's try Strategy #2 n times and see how we do in percentage #we remove all print statements as we don't want to print them possible thousands of times! n = 1000000 #we'll define a counter *count* to record the number of wins count = 0 #we need a *loop* in order to get the n repetitions for i in [1..n]: #represent the 3 doors with the Integers 0, 1 and 2 # let Sage choose the location of the car by randomly choosing one of 0,1 or 2 car = randint(0,2) guess1 = randint(0,2) #represent the doors with list [0,1,2], remove the doors with the contestant's guess and the one with the car doors = [0,1,2] doors.remove(guess1) #use the variable *monty* to represent the door that monty opnes if car in doors: doors.remove(car) monty = doors[0] else: monty = doors[randint(0,1)] #here is where we implement the strategy #Strategy 2 was to *switch* the guess - so we stick with guess1 doors = [0,1,2] doors.remove(guess1) doors.remove(monty) guess2 = doors[0] if car == guess2: count = count + 1 print("The percentage of success using Strategy #1 with {} trials is {}".format(n,count/n*100.0))
The percentage of success using Strategy #1 with 1000000 trials is 66.6084000000000
#Strategy 3 #place the car randomly behind one of 3 doors car = randint(0,2) #we assume the contestant always starts by guessing the car is behind door 0 #monty will open one of door 1 or 2 #if the car is behind door 1, monty opens door 2 #if the car is behind door 2, monty opens door 1 #if the car is behind door 0, monty randomly selects one of doors 1 or 2 #if monty opens door 1 we switch our guess, #if he opens door 2 we stick with door 0 (this was a TYPO, the worksheet strategy is really original strategy 2) if car == 0: monty = randint(1,2) if monty == 1: guess = 2 #note: we fail if monty == 2: guess = 0 #note: we win if car == 1: guess = 0 #note: we fail if car == 2: guess = 2 #note: we win if car == guess: print "you win!"
you win!
#repeat this experiment n times, find the ratio of successes n=1000 successes = 0 for i in [1..n]: car = randint(0,2) if car == 0: monty = randint(1,2) if monty == 1: guess = 2 #note: we fail if monty == 2: guess = 0 #note: we win if car == 1: guess = 0 #note: we fail if car == 2: guess = 2 #note: we win if car == guess: successes = successes+1 print successes/n*100.0
51.2000000000000