Last active
March 2, 2018 19:11
-
-
Save dilanshah/6695db9aa49375fbe4ced69ccf3acceb to your computer and use it in GitHub Desktop.
write a function to tell if a full deck of cards is a single riffle of two other halves, half1 and half2
This file contains 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
# write a function to tell if a full deck of cards is a single riffle of two other halves, half1 and half2 | |
import random | |
from collections import deque | |
h1 = deque(random.sample(range(1, 27), 26)) | |
h2 = deque(random.sample(range(27, 53), 26)) | |
print(h1) | |
print(h2) | |
shuffled_deck = deque(random.sample(range(1, 53), 52)) | |
def riffle(half1, half2): | |
""" | |
:param half1: deque | |
:param half2: deque | |
:return: return bool | |
""" | |
print(shuffled_deck) | |
print(half1) | |
if len(shuffled_deck) == 0: | |
return True | |
else: | |
stored_value = shuffled_deck.popleft() | |
# check which half was used first | |
if stored_value == half1[0]: | |
half1.popleft() | |
return riffle(half1, half2) | |
elif stored_value == half2[0]: | |
half2.popleft() | |
return riffle(half1, half2) | |
else: | |
return False | |
print(riffle(h1, h2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment