Skip to content

Instantly share code, notes, and snippets.

@luther9
Created March 5, 2018 21:42
Show Gist options
  • Save luther9/b11dd370b4427489255641601a580c8a to your computer and use it in GitHub Desktop.
Save luther9/b11dd370b4427489255641601a580c8a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# John's version.
def shuffle(deck):
return [e for l in [a for a in zip(deck[:(len(deck)//2)], deck[(len(deck)//2):])] for e in l]
# Slightly simplified. Above, the inner comprehension has just "a for a" with no
# "if" at the end, so we can reduce it down to just the iterator.
def shuffle1(deck):
half = len(deck) // 2
return [e for l in zip(deck[:half], deck[half:]) for e in l]
deck = range(52)
shuffled = shuffle1(deck)
print(shuffled)
assert shuffle(deck) == shuffled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment