Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import shuffle
- from timeit import default_timer as timer
- # 1. First, I will create the cards deck
- symbols = ["A", "B", "C", "D"]
- numbers = [str(i) for i in range(1, 14)]
- cards = list()
- for symbol in symbols:
- for number in numbers:
- card = symbol + number
- cards.append(card)
- # Function 1 - Checks if a card matches with another card
- def cardMatch(card, cardToBeCompared):
- # I will compare the variables: "card" and "cardToBeCompared"
- # First, of all we have to check if they have the same symbol
- sym1 = card[0]
- sym2 = cardToBeCompared[0]
- num1 = int(card[1:])
- num2 = int(cardToBeCompared[1:])
- if sym1 != sym2:
- return False
- else:
- # Now, I have to check if num1, num2 are consecutive
- if (num1 == 1 and num2 == 13) or (num1 == 13 and num2 == 1):
- return True
- else:
- # Here, we don't have the numbers combination 1-13
- if (num1 == num2 + 1) or (num1 == num2 - 1):
- return True
- else:
- return False
- # Function 2 - Checks if a card matches with 1 of the down cards (in our problem we will have 7 down cards)
- def cardMatchSynolo(card, SYNOLO):
- N = len(SYNOLO)
- noHits = 0
- for i in range(N):
- cardToBeCompared = SYNOLO[i]
- flag = cardMatch(card, cardToBeCompared)
- if flag == False:
- noHits += 1
- # So, we have made N "comparisons" of our card. If noHits = unsuccessful_comparisons == N ----> I want to return False
- if noHits == N:
- return False
- else:
- return True
- # Function 3 - It "breaks" the cards into a shuffled one firstly and then it will return a tuple with 2 elements
- # element1 = the first 7 cards of the shuffled deck
- # element2 = all the other cards (45)
- def breakCards(N):
- CARDS = cards.copy()
- shuffle(CARDS)
- first = CARDS[0:N]
- rest = CARDS[N:]
- return first, rest
- # Function 4 - It will simulate the game
- def simulate(number):
- first, rest = breakCards(number)
- # print(first)
- # print(rest)
- N = len(first)
- M = len(rest)
- # If number = 7, we have 45 (for example) cards in our "rest" variable. I will walk in steps of 3.
- # Each card I take in the steps by 3 form the deck of "rest" deck, it will be the card that I will compare
- # "N" times with the SYNOLO = first.
- # I walk in 3's, so it will be M/3 comparisons
- comparisons = M / 3
- sum = 0
- for i in range(0, M, 3):
- # First, I will not play with cards 1 and 2, but with card 3
- card = rest[i+2]
- # print(card)
- # I will compare this "card", with the SYNOLO = "first"
- flag = cardMatchSynolo(card, first)
- if flag == False:
- sum += 1
- # Now, the variable "sum" will tell me how many times, we had NO-SUCCESS
- # If this "sum" == comparisons, then my function returns False
- if sum == comparisons:
- print(first)
- print(rest)
- print()
- return False
- else:
- return True
- # MAIN FUNCTION
- sum = 0
- numOfSimulations = 10000
- start = timer()
- for i in range(numOfSimulations):
- if simulate(7) == False:
- sum += 1
- end = timer()
- percentage = 100 * sum / numOfSimulations
- percentage = round(percentage, 4)
- elapsed = end - start
- elapsed = round(elapsed, 2)
- print()
- print("****************************************************")
- print("The solitaire did not success in " + str(sum) + " / " + str(numOfSimulations) + " tries.")
- print("Failure rate = " + str(percentage) + " %")
- print("Execution time for " + str(numOfSimulations) + " simulations ----> " + str(elapsed) + " seconds.")
- print("****************************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement