Este es el nivel en el que configurar los problemas de combinatoria se vuelve difícil para mí (especialmente porque no estoy practicando). No podía asegurar con certeza que Justin Rising estaba calculando el valor correcto, así que simulé 10,000,000 de manos … Obtuve [matemáticas] 6.56 \ veces 10 ^ {- 5} [/ matemáticas]. Bien hecho, Justin.
Aquí está mi código organizado pero (estoy seguro) ineficiente:
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5
import random
import time def deal():
deck = ['h','c','s','d']*13
random.shuffle(deck)
hand = [] while len(hand) < 13:
hand.append(deck.pop())
return hand def checksuits(hand):
global suitlist if len(hand) == 1:
return "Sorted" suit1 = hand[0]
suit2 = hand[1] if suit2 != suit1:
if suit2 in suitlist:
return "Not sorted"
if suit2 not in suitlist:
suitlist.append(suit1)
return checksuits(hand[1:]) if suit2 == suit1:
return checksuits(hand[1:]) t0 = time.time()
results = [] # 100 million
for i in range(1,100000001):
suitlist = []
results.append(checksuits(deal())) t1 = time.time()
total = t1-t0
print total
# takes 20 minutes with 10 million simulations (and takes 2 minutes with 1 million) # See how it worked out
sum(x == "Sorted" for x in results)
# 6556 out of 10,000,000, or...
# P = 6.556*10E-5