Flash has invited his nemesis The Turtle (He actually was a real villain! ) to play his favourite card game, SNAP. In this game a 52 card deck is dealt out so both Flash and the Turtle get 26 random cards. 

Each players cards will be represented by an array like below

Flash’s pile:
```[ 'A', '5', 'Q', 'Q', '6', '2', 'A', '9', '10', '6', '4', '3', '10', '9', '3', '8', 'K', 'J', 'J', 'K', '7', '9', '5', 'J', '7', '2' ]```

Turtle’s pile:
```[ '8', 'A', '2', 'Q', 'K', '8', 'J', '6', '4', '8', '7', 'A', '5', 'K', '3', 'Q', '6', '9', '4', '3', '4', '10', '2', '7', '10', '5' ]```

The players take it in turn to take the top card from their deck (the first element in their array) and place it in a face up pile in the middle. Flash goes first.

When a card is placed on the face up pile that matches the card it is placed on top of the first player to shout ‘SNAP!’ wins that round. Due to Flash's speed he wins every round.

Face up pile in the middle:
```[ 'A', '8', '5', 'A', 'Q', '2', 'Q', 'Q',```  =>  SNAP!

The face up pile of cards in the middle are added to the bottom of Flash's pile.

Flash’s pile after one round:
```['6', '2', 'A', '9', '10', '6', '4', '3', '10', '9', '3', '8', 'K', 'J', 'J', 'K', '7', '9', '5', 'J', '7', '2', 'A', '8', '5', 'A', 'Q', '2', 'Q', 'Q' ]```

Flash then starts the next round by putting down the next card.

When Turtle runs out of cards the game is over.

How many times does Flash get to call Snap before Turtle runs out of cards?

If both the player put down all their cards into the middle without any matches then the game ends a draw and Flash calls SNAP 0 times.

Difficulty: EASY
Tags: Fundamentals

Data structures

Complete search
Skill types: Data structures

Complete search

Starter code:
def snap(flash_pile, turtle_pile):
	

Solutions:
def round(flash_pile, turtle_pile):
	faceup_pile = []
	while turtle_pile:
		for pile in (flash_pile, turtle_pile):
			faceup_pile.append(pile.pop(0))
			if len(faceup_pile) >= 2 and faceup_pile[-1] == faceup_pile[-2]:
				flash_pile.extend(faceup_pile)
				return True

def snap(flash_pile, turtle_pile):
	for i in range(26):
		if not round(flash_pile, turtle_pile):
			return i


def snap(flash_pile, turtle_pile):
	middle_pile = []
	snap = 0
	while turtle_pile != []:
		middle_pile.append(flash_pile.pop(0))
		if len(middle_pile) > 1 and middle_pile[-2] == middle_pile[-1]:
			snap += 1
			flash_pile.extend(middle_pile)
			middle_pile = []
			continue
		middle_pile.append(turtle_pile.pop(0))
		if len(middle_pile) > 1 and middle_pile[-2] == middle_pile[-1]:
			snap += 1
			flash_pile.extend(middle_pile)
			middle_pile = []
	return snap


from collections import deque

def snap(f, t):
	(f, t) = list(map(deque, (f, t)))
	(m, snap) = ([], 0)
	while t:
		for dq in (f, t):
			m.append(dq.popleft())
			if len(m) > 1 and m[-2] == m[-1]:
				snap += 1
				f.extend(m)
				m.clear()
				break
	return snap


def snap(flash_pile, turtle_pile, snapper=0):
	new_pile = ['']
	if len(turtle_pile) <= 0:
		return snapper
	for x in range(len(turtle_pile)):
		new_pile.append(flash_pile[x])
		if isSnap(new_pile):
			return snap(flash_pile[x + 1:] + new_pile[1:], turtle_pile[x:], snapper + 1)
		new_pile.append(turtle_pile[x])
		if isSnap(new_pile):
			return snap(flash_pile[x + 1:] + new_pile[1:], turtle_pile[x + 1:], snapper + 1)
	return snapper

def isSnap(pile):
	return pile[-1] == pile[-2]


def snap(flash, bot):
	(face_up, c, turn) = ([], 0, 1)
	while bot:
		face_up.append(flash.pop(0) if turn else bot.pop(0))
		turn ^= 1
		if len(face_up) > 1 and face_up[-1] == face_up[-2]:
			flash.extend(face_up)
			face_up = []
			c += 1
			turn = 1
	return c


def snap(F, T, S=None, P=True):
	if not T:
		return 0
	if S is None:
		S = []
	S.append(F.pop(0) if P else T.pop(0))
	if len(S) > 1 and S[-1] == S[-2]:
		F.extend(S)
		return 1 + snap(F, T)
	return snap(F, T, S, not P)


def snap(flash, turtle):
	snaps = 0
	pile = []
	players = (flash, turtle)
	i = [0, 0]
	p = 0
	while i[1] < len(turtle):
		card = players[p][i[p]]
		i[p] += 1
		if pile and card == pile[-1]:
			flash.extend(pile)
			flash.append(card)
			snaps += 1
			pile = []
			p = 0
		else:
			pile.append(card)
			p ^= 1
	return snaps


def snap(f, t, n=0, l=None):
	if l is None:
		l = []
	if t == []:
		return n
	l += [f[0]]
	if len(l) > 1 and l[-2] == l[-1]:
		return snap(f[1:] + l, t, n + 1, None)
	l += [t[0]]
	if l[-2] == l[-1]:
		return snap(f[1:] + l, t[1:], n + 1, None)
	return snap(f[1:], t[1:], n, l)


def snap(flash_pile, turtle_pile):
	centre_pile = []
	i = 0
	while turtle_pile:
		card = turtle_pile.pop(0) if i % 2 else flash_pile.pop(0)
		centre_pile.append(card)
		if i > 0 and centre_pile[-2] == centre_pile[-1]:
			return 1 + snap(flash_pile + centre_pile, turtle_pile)
		i += 1
	return 0


def snap(a, b):
	(c, p, t) = (0, 1, [])
	while b:
		if p == 1:
			x = a.pop(0)
			if not t:
				t.append(x)
			elif t[-1] == x:
				c += 1
				a.extend(t + [x])
				t = [a.pop(0)]
			else:
				t.append(x)
		if p == -1:
			y = b.pop(0)
			if not t:
				t.append(y)
			elif t[-1] == y:
				c += 1
				a.extend(t + [y])
				t = []
			else:
				t.append(y)
		p *= -1
	return c