Skip to content

Instantly share code, notes, and snippets.

@JimmyMow
Last active March 29, 2016 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimmyMow/528e92964a3b1cb96109 to your computer and use it in GitHub Desktop.
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?
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