You will be given a string (x) featuring a cat 'C', a dog 'D' and a mouse 'm'. The rest of the string will be made up of '.'. 

You need to find out if the cat can catch the mouse from it's current position. The cat can jump (j) characters. 

Also, the cat cannot jump over the dog.

So:

if j = 5:

```..C.....m.``` returns 'Caught!'  <-- not more than j characters between

```.....C............m......``` returns 'Escaped!'  <-- as there are more than j characters between the two, the cat can't jump far enough

if j = 10:

```...m.........C...D``` returns 'Caught!' <--Cat can jump far enough and jump is not over dog

```...m....D....C.......``` returns 'Protected!' <-- Cat can jump far enough, but dog is in the way, protecting the mouse

Finally, if all three animals are not present, return 'boring without all three'

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Data structures

Starter code: def cat_mouse(x,j):
	

Solutions:
def cat_mouse(x, j):
	(d, c, m) = (x.find('D'), x.find('C'), x.find('m'))
	if -1 in [d, c, m]:
		return 'boring without all three'
	if abs(c - m) <= j:
		return 'Protected!' if c < d < m or m < d < c else 'Caught!'
	return 'Escaped!'


def cat_mouse(x, j):
	try:
		(cat, dog, mouse) = [x.index(c) for c in 'CDm']
		if abs(mouse - cat) > j:
			return 'Escaped!'
		elif cat < dog < mouse or cat > dog > mouse:
			return 'Protected!'
		else:
			return 'Caught!'
	except ValueError:
		return 'boring without all three'


from re import sub

def cat_mouse(s, j):
	if set(s) < set('mCD.'):
		return 'boring without all three'
	between = sub('.*[Cm](.*?)[Cm].*', '\\1', s)
	if len(between) > j:
		return 'Escaped!'
	if 'D' in between:
		return 'Protected!'
	return 'Caught!'


def cat_mouse(x, j):
	(c, d, m) = (x.find(animal) for animal in 'CDm')
	return 'boring without all three' if c < 0 or d < 0 or m < 0 else 'Escaped!' if abs(c - m) > j else 'Protected!' if c < d < m or c > d > m else 'Caught!'


def cat_mouse(x, j):
	try:
		(d, c, m) = map(x.index, 'DCm')
	except ValueError:
		return 'boring without all three'
	if j < abs(c - m) - 1:
		return 'Escaped!'
	elif c > d > m or m > d > c:
		return 'Protected!'
	return 'Caught!'


def cat_mouse(stg, j):
	if set('mCD.') - set(stg):
		return 'boring without all three'
	(c, d, m) = (stg.index(a) for a in 'CDm')
	if abs(c - m) > j:
		return 'Escaped!'
	elif m < d < c or c < d < m:
		return 'Protected!'
	else:
		return 'Caught!'


def cat_mouse(x, j):
	if not all(('C' in x, 'D' in x, 'm' in x)):
		return 'boring without all three'
	(cat, dog, mouse) = (x.index('C'), x.index('D'), x.index('m'))
	zone = [x[cat:mouse], x[mouse + 1:cat]][mouse < cat]
	return {(1, 0): 'Caught!', (1, 1): 'Protected!'}.get((len(zone) <= j, 'D' in zone), 'Escaped!')


def cat_mouse(x, j):
	if len(set(x)) != 4:
		return 'boring without all three'
	dog = x.index('D')
	cat = x.index('C')
	mouse = x.index('m')
	if max(cat, mouse) - min(cat, mouse) <= j:
		if min(cat, mouse) < dog and dog < max(cat, mouse):
			return 'Protected!'
		return 'Caught!'
	return 'Escaped!'


def cat_mouse(x, j):
	if not all(['C' in x, 'm' in x, 'D' in x]):
		return 'boring without all three'
	(c, m, d) = list(map(x.index, ['C', 'm', 'D']))
	if abs(m - c) <= j + 1:
		return ['Caught!', 'Protected!'][x.replace('.', '')[1] == 'D']
	else:
		return 'Escaped!'