I'm afraid you're in a rather unfortunate situation. You've injured your leg, and are unable to walk, and a number of zombies are shuffling towards you, intent on eating your brains. Luckily, you're a crack shot, and have your trusty rifle to hand.

The zombies start at `range` metres, and move at 0.5 metres per second. Each second, you first shoot one zombie, and then the remaining zombies shamble forwards another 0.5 metres.

If any zombies manage to get to 0 metres, you get eaten. If you run out of ammo before shooting all the zombies, you'll also get eaten. To keep things simple, we can ignore any time spent reloading.

Write a function that accepts the total number of zombies, a range in metres, and the number of bullets you have.

If you shoot all the zombies, return "You shot all X zombies."
If you get eaten before killing all the zombies, and before running out of ammo, return "You shot X zombies before being eaten: overwhelmed."
If you run out of ammo before shooting all the zombies, return "You shot X zombies before being eaten: ran out of ammo."

(If you run out of ammo at the same time as the remaining zombies reach you, return "You shot X zombies before being eaten: overwhelmed.".)

Good luck! (I think you're going to need it.)

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Game theory

Starter code: def zombie_shootout(zombies, distance, ammo):
	

Solutions:
def zombie_shootout(zombies, distance, ammo, shot=0):
	if not zombies:
		return f'You shot all {shot} zombies.'
	if distance <= 0:
		return f'You shot {shot} zombies before being eaten: overwhelmed.'
	if not ammo:
		return f'You shot {shot} zombies before being eaten: ran out of ammo.'
	return zombie_shootout(zombies - 1, distance - 0.5, ammo - 1, shot + 1)


def zombie_shootout(z, r, a):
	s = min(r * 2, a)
	return f'You shot all {z} zombies.' if s >= z else f"You shot {s} zombies before being eaten: {('overwhelmed' if s == 2 * r else 'ran out of ammo')}."


def zombie_shootout(zombies, distance, ammo):
	if distance * 2 < min(ammo + 1, zombies):
		return f'You shot {distance * 2} zombies before being eaten: overwhelmed.'
	if ammo < zombies:
		return f'You shot {ammo} zombies before being eaten: ran out of ammo.'
	return f'You shot all {zombies} zombies.'


def zombie_shootout(zombies, range, ammo):
	distance = range * 2
	if zombies > ammo < distance:
		return f'You shot {ammo} zombies before being eaten: ran out of ammo.'
	elif zombies > distance:
		return f'You shot {distance} zombies before being eaten: overwhelmed.'
	return f'You shot all {zombies} zombies.'


def zombie_shootout(zombies, distance, ammo):
	sa = ammo - zombies
	sd = distance * 2 - zombies
	if sa < 0 or sd < 0:
		if sd > sa:
			return f'You shot {ammo} zombies before being eaten: ran out of ammo.'
		else:
			return f'You shot {distance * 2} zombies before being eaten: overwhelmed.'
	return f'You shot all {zombies} zombies.'


def zombie_shootout(zombies, distance, ammo):
	results = {f'You shot all {zombies} zombies.': zombies, f'You shot {distance * 2} zombies before being eaten: overwhelmed.': distance * 2, f'You shot {ammo} zombies before being eaten: ran out of ammo.': ammo}
	return min(results, key=results.get)


def zombie_shootout(zombies, distance, ammo):
	shot = 0
	while distance > 0 and ammo > 0 and (zombies > 0):
		zombies -= 1
		ammo -= 1
		distance -= 0.5
		shot += 1
	if zombies == 0:
		return 'You shot all ' + str(shot) + ' zombies.'
	elif distance == 0:
		return 'You shot ' + str(shot) + ' zombies before being eaten: overwhelmed.'
	else:
		return 'You shot ' + str(shot) + ' zombies before being eaten: ran out of ammo.'


def zombie_shootout(zombies, distance, ammo):
	if ammo >= zombies and 2 * distance >= zombies:
		return 'You shot all {} zombies.'.format(zombies)
	elif ammo < zombies and ammo < 2 * distance:
		return 'You shot {} zombies before being eaten: ran out of ammo.'.format(ammo)
	else:
		return 'You shot {} zombies before being eaten: overwhelmed.'.format(2 * distance)