You've arrived at a carnival and head straight for the duck shooting tent. Why wouldn't you?

You will be given a set amount of ammo, and an aim rating of between 1 and 0. No your aim is not always perfect - hey maybe someone fiddled with the sights on the gun...

Anyway your task is to calculate how many successful shots you will be able to make given the available ammo and your aim score, then return a string representing the pool of ducks, with those ducks shot marked with 'X' and those that survived left unchanged. You will always shoot left to right.

Example of start and end duck string with two successful shots:

Start ---> |~~~~~22~2~~~~~|

**Bang!! Bang!!**

End  --->  |~~~~~XX~2~~~~~|

All inputs will be correct type and never empty.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: def duck_shoot(ammo, aim, ducks):
	

Solutions:
def duck_shoot(ammo, aim, ducks):
	return ducks.replace('2', 'X', int(ammo * aim))


duck_shoot = lambda am, aim, d: d.replace('2', 'X', int(am * aim))


def duck_shoot(ammo, aim, ducks):
	b = 0
	b += int(aim * ammo)
	return ducks.replace('2', 'X', b)


duck_shoot = lambda a, b, c: c.replace('2', 'X', int(a * b))


import math

def duck_shoot(ammo, aim, duck_pool):
	shots = math.floor(ammo * aim)
	return duck_pool.replace('2', 'X', shots)


from math import floor

def duck_shoot(ammo, aim, ducks):
	kills = [i for (i, c) in enumerate(ducks) if c == '2'][:min(floor(aim * ammo), ducks.count('2'))]
	return ''.join(['X' if i in kills else c for (i, c) in enumerate(ducks)])


def duck_shoot(ammo, aim, ducks):
	indices = [i for (i, c) in enumerate(ducks) if c == '2']
	hits = int(aim * ammo)
	result = list(ducks)
	for i in indices[:hits]:
		result[i] = 'X'
	return ''.join(result)


def duck_shoot(ammo, aim, ducks):
	hit = min(int(ammo * aim), ducks.count('2'))
	return ducks.replace('2', 'X', hit)


def duck_shoot(ammo, aim, ducks):
	output = ''
	kills = int(ammo * aim)
	for duck in ducks:
		if duck == '2':
			if kills > 0:
				output += 'X'
				kills -= 1
			else:
				output += duck
		else:
			output += duck
	return output


def duck_shoot(ammo, aim, ducks):
	bangs = int(ammo * aim)
	if bangs == 0:
		return ducks
	res = list(ducks)
	shot = 0
	for (i, c) in enumerate(res):
		if c == '2':
			res[i] = 'X'
			shot += 1
			if shot >= bangs:
				break
	return ''.join(res)