Advertisement
makispaiktis

52 consecutive cards

Jul 17th, 2022 (edited)
1,298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. from random import shuffle
  2.  
  3. # Function 0 - Create a cards deck
  4. def createCards():
  5.     cards = list()
  6.     for i in range(1, 14):
  7.         for j in range(4):
  8.             cards.append(i)
  9.     return cards
  10.  
  11.  
  12. # Function 1 - Decide how many times I will shuffle the deck
  13. def SHUFFLE(cards, n):
  14.     for i in range(n):
  15.         shuffle(cards)
  16.     return cards
  17.  
  18.  
  19. # Function 2 - Decide if there are any  consecutive cards
  20. def consecutive(cards):
  21.     flag = True
  22.     index = -1000
  23.     for i in range(len(cards) - 1):
  24.         if cards[i] == cards[i+1]:
  25.             flag = False
  26.             index = i
  27.             break
  28.     return flag, index
  29.  
  30.  
  31. # Function 3 - Simulate rounds with a given n = how many shuffles
  32. def simulate(LIMIT, n):
  33.     good = 0
  34.     for round in range(1, LIMIT+1):
  35.         cards = createCards()
  36.         cards = SHUFFLE(cards, n)
  37.         flag, index = consecutive(cards)
  38.         if flag == True:
  39.             good += 1
  40.     return good
  41.  
  42.  
  43.  
  44. # MAIN FUNCTION
  45. LIMIT = 10**4
  46. nList = list(range(1, 5))
  47. for n in nList:
  48.     good = simulate(LIMIT, n)
  49.     percentage100 = 100 * good / LIMIT
  50.     print(str(n) + " shuffles ---> " + str(good) + " successes ---> " + str(percentage100) + "%")
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement