DEV Community is a community of 891,187 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

dev.to staff

Posted on

Daily Challenge #166 - Cat and Mouse

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 its 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'

This challenge comes from A.Partridge on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Discussion (3)

Andrei McMillan • Edited on

javascript

``````function cat_mouse(str, max_jump) {
var has_animals = /(c|m|d)/i.test(str);
if (!has_animals) {
console.log('boring without all three');
} else {
var str_lower = str.toLowerCase();
var cat_position = str_lower.indexOf('c');
var dog_position = str_lower.indexOf('d');
var mouse_position = str_lower.indexOf('m');
var min = Math.min(cat_position, mouse_position);
var max = Math.max(cat_position, mouse_position);

// is the dog between the cat and mouse
if (min < dog_position && dog_position < max) {
console.log('Protected!');
} else {
var distance = Math.abs(cat_position - mouse_position);
if (distance > max_jump) {
console.log('Escaped!');
} else {
console.log('Caught!');
}
}

}
}

// ----- test

cat_mouse('..j.....h.', 5);
cat_mouse('..c.....m.', 5);
cat_mouse('..c....m.', 5);
cat_mouse('d..m.......c.', 10);
cat_mouse('d..m.......c.', 5);
cat_mouse('..m....d...c.', 5);
cat_mouse('...m.........C...D', 10);
``````

python

``````import re
import math

def cat_mouse(str_input, max_jump):
str_lower = str_input.lower()
has_animal = re.search("c|m|d", str_lower)

if has_animal is None:
print('boring without all three')
else:
cat_position = str_lower.find("c")
dog_position = str_lower.find("d")
mouse_position = str_lower.find("m")
min_pos = min(cat_position, mouse_position)
max_pos = max(cat_position, mouse_position)

# is the dog between the cat and mouse
if min_pos < dog_position < max_pos:
print('Protected!')
else:
distance = math.fabs(cat_position - mouse_position)
if distance > max_jump:
print('Escaped!')
else:
print('Caught!')

# // -- test
cat_mouse('..j.....h.', 5)
cat_mouse('..c.....m.', 5)
cat_mouse('..c....m.', 5)
cat_mouse('d..m.......c.', 10)
cat_mouse('d..m.......c.', 5)
cat_mouse('..m....d...c.', 5)
cat_mouse('...m.........C...D', 10)

``````
Michael Kohl • Edited on

The quick & dirty Ruby version:

``````def cat_and_mouse(s, distance)
c, m, d = %w[C m D].map { |c| s.index(c) }
return 'Boring without all three' unless c && m && d
return 'Escaped' if (c - m).abs > distance + 1
return 'Protected' if d && (d > c && d < m || d > m && d < c)
'Caught'
end
``````
Mathanagopal Sankarasubramanian • Edited on

JAVA

``````import java.util.Scanner;

public class CatAndMouse
{
public static void main (String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string x");
String x = sc.nextLine();
System.out.println("Enter the jump");
int j = sc.nextInt();
x = x.toLowerCase();
int posofc = x.indexOf('c') + 1, posofm = x.indexOf('m') + 1, posofdog = x.indexOf('d');
if(posofdog>0){//Check if dog is in between
int start = posofc > posofm ? posofm : posofc;
int end = posofc > posofm ? posofc : posofm;
if(start<posofdog && end>posofdog){
System.out.println("Protected");
System.exit(0);
}
}
int compare = posofc < posofm ? (posofm - posofc - 1) : (posofc - posofm - 1);
if (compare > j){
System.out.println ("Escape");
}
else{
System.out.println ("Caught");
}

}
}
``````