It's the fourth quater of the Super Bowl and your team is down by 4 points. You're 10 yards away from the endzone, if your team doesn't score a touchdown in the next four plays you lose. On a previous play, you were injured and rushed to the hospital. Your hospital room has no internet, tv, or radio and you don't know the results of the game. You look at your phone and see that on your way to the hospital a text message came in from one of your teamates. It contains an array of the last 4 plays in chronological order. In each play element of the array you will receive the yardage of the play and the type of the play. Have your function let you know if you won or not.

# What you know:

* Gaining greater than 10 yds from where you started is a touchdown and you win.
* Yardage of each play will always be a positive number greater than 0.
* There are only four types of plays: "run", "pass", "sack", "turnover".
* Type of plays that will gain yardage are: "run", "pass".
* Type of plays that will lose yardage are: "sack".
* Type of plays that will automatically lose the game are: "turnover".
* When a game ending play occurs the remaining (plays) arrays will be empty.
* If you win return true, if you lose return false.

# Examples:


[[8, "pass"],[5, "sack"],[3, "sack"],[5, "run"]]
`false`
[[12, "pass"],[],[],[]])
`true`
[[2, "run"],[5, "pass"],[3, "sack"],[8, "pass"]]
`true`
[[5, "pass"],[6, "turnover"],[],[]]
`false`

Good Luck!

Difficulty: EASY
Tags: String algorithms

Fundamentals

Data structures

Game theory
Skill types: Data structures

Starter code:
def did_we_win(plays):
	

Solutions:
def did_we_win(plays):
	plays = [p for p in plays if p]
	return all((p != 'turnover' for (y, p) in plays)) and sum((-y if p == 'sack' else y for (y, p) in plays)) > 10


def did_we_win(plays):
	s = 0
	for i in range(4):
		if not plays[i]:
			break
		if plays[i][1] == 'turnover':
			return False
		s += plays[i][0] * (-1) ** (plays[i][1] == 'sack')
	return s > 10


def did_we_win(plays):
	dist = 10
	plays = [t for t in plays if t]
	for (play_dist, play) in plays:
		if play in ['run', 'pass']:
			dist -= play_dist
			if dist < 0:
				return True
		elif play == 'sack':
			dist += play_dist
		else:
			break
	return False


def did_we_win(plays):
	return sum((-100 if p[1][0] == 't' else -p[0] if p[1][0] == 's' else p[0] for p in plays if p)) > 10


def did_we_win(plays):
	if any((play[1] == 'turnover' or not play for play in plays if play)):
		return False
	if not all(plays):
		return True
	return sum((-yards if action == 'sack' else yards for (yards, action) in plays)) > 10


def did_we_win(plays):
	r = 0
	for p in plays:
		if not p:
			continue
		(yd, t) = p
		if t in ('run', 'pass'):
			r += yd
		elif t == 'sack':
			r -= yd
		elif t == 'turnover':
			return False
		if r > 10:
			return True
	return False


def did_we_win(plays):
	yard = 0
	for (number, type_play) in plays:
		if type_play == 'pass':
			win = number
		elif type_play == 'run':
			win = number
		elif type_play == 'sack':
			win = -number
		elif type_play == 'turnover':
			return False
			break
		yard = yard + win
		if yard > 10:
			return True
			break
	if yard > 10:
		return True
	else:
		return False


def did_we_win(plays):
	position = 0
	for play in plays:
		if play[1] == 'run' or play[1] == 'pass':
			position += play[0]
		elif play[1] == 'sack':
			position -= play[0]
		else:
			return False
		if position > 10:
			return True
	return False


def did_we_win(plays):
	yds = 10
	for play in plays:
		if play[1] == 'turnover':
			return False
		if play[1] == 'sack':
			yds += play[0]
		else:
			yds -= play[0]
		if yds < 0:
			return True
	return False


def did_we_win(plays):
	increment = ['pass', 'run']
	decrement = ['sack']
	gameOver = ['turnover']
	score = plays[0][0]
	for x in plays:
		if x:
			if x[1] in increment:
				score += x[0]
			elif x[1] in decrement:
				score -= x[0]
			else:
				return False
	if score - plays[0][0] > 10:
		return True
	else:
		return False