Skip to content

Instantly share code, notes, and snippets.

@hnq90
Created July 29, 2015 02:31
Show Gist options
  • Select an option

  • Save hnq90/1bf1245171a7b898a00d to your computer and use it in GitHub Desktop.

Select an option

Save hnq90/1bf1245171a7b898a00d to your computer and use it in GitHub Desktop.
def is_single_riffle_recursive_optimized(half1, half2, shuffled_deck, shuffled_deck_index=0, half1_index=0, half2_index=0):
# base case we've hit the end of shuffled_deck:
if shuffled_deck_index + 1 > len(shuffled_deck):
return True
# if we still have cards in half1
# and the "top" card in half1 is the same
# as the top card in shuffled_deck
if (not half1_index + 1 > len(half1)) and \
half1[half1_index] == shuffled_deck[shuffled_deck_index]:
half1_index += 1
# if we still have cards in half2
# and the "top" card in half2 is the same
# as the top card in shuffled_deck
elif (not half2_index + 1 > len(half2)) and \
half2[half2_index] == shuffled_deck[shuffled_deck_index]:
half2_index += 1
# if the top card in shuffled_deck doesn't match the top
# card in half1 or half2, this isn't a single riffle.
else:
return False
# the current card in shuffled_deck has now been "accounted for."
# so move on to the next one
shuffled_deck_index+=1
return is_single_riffle_recursive_optimized(half1, half2, shuffled_deck, shuffled_deck_index=shuffled_deck_index, half1_index=half1_index, half2_index=half2_index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment