functioncat_mouse(str,max_jump){varhas_animals=/(c|m|d)/i.test(str);if(!has_animals){console.log('boring without all three');}else{varstr_lower=str.toLowerCase();varcat_position=str_lower.indexOf('c');vardog_position=str_lower.indexOf('d');varmouse_position=str_lower.indexOf('m');varmin=Math.min(cat_position,mouse_position);varmax=Math.max(cat_position,mouse_position);// is the dog between the cat and mouseif(min<dog_position&&dog_position<max){console.log('Protected!');}else{vardistance=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
importreimportmathdefcat_mouse(str_input,max_jump):str_lower=str_input.lower()has_animal=re.search("c|m|d",str_lower)ifhas_animalisNone: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
ifmin_pos<dog_position<max_pos:print('Protected!')else:distance=math.fabs(cat_position-mouse_position)ifdistance>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)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
javascript
python