Last active
March 29, 2016 16:11
-
-
Save JimmyMow/528e92964a3b1cb96109 to your computer and use it in GitHub Desktop.
Given the length of a deck of cards, how many perfect shuffles does it take to get to it's original order?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shuffle(cards): | |
return [i for sublst in zip(cards[:len(cards) // 2], cards[len(cards) // 2:]) for i in sublst] | |
def main(deck_len): | |
cards = list(range(deck_len)) | |
start = shuffle(cards) | |
counter = 1 | |
while start != cards: | |
start = shuffle(start) | |
counter += 1 | |
print("A {} card deck will take {} perfect shuffles".format(deck_len,counter)) | |
return | |
# The main() function returns the amount of perfect shuffles it takes to get back to the original deck. | |
# Just pass it the size of the deck of cards | |
main(52) | |
# Returns: A 52 card deck will take 8 perfect shuffles | |
main(10000) | |
# Returns: A 10000 card deck will take 300 perfect shuffles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment