# Cats and a Mouse

# Cats and a Mouse

amiya13 + 8 comments this problem should be of 10 points, not 15

ScienceD + 1 comment IKR, its the most ez problem on this site xD But I had fun solving it anyway!

nawnaw011222 + 0 comments Not true lol Some problems are just impossible.

NeuroHacker + 0 comments hmm

jai00271 + 0 comments I agree. I was thinking there will be minimum 10-15 test cases for this.

dva_1_0 + 1 comment 5 would probably suffice

irrfankhann29 + 0 comments couldn't agree more

MadMatter + 1 comment [deleted]MadMatter + 0 comments To solve for N dimensions Pythagorean Theorem works for all of them x^2 + y^2 + z^2 .... + N^2 = H^2. I solved it in python idle. To switch between int and array I used a Try Exception block with Invalid type for when going through the dimension array.

But that is way more complicated than this. I solved for N dimensions for fun.

samarththeengin1 + 0 comments really this is very easy

sourav_raychaud1 + 0 comments Should have been 1 Point.

yalcinberkay + 0 comments I think this problem should'nt be exists.

danielbiocchi + 3 comments java 8:

static String catAndMouse(int x, int y, int z) { if(Math.abs(x - z) < Math.abs(y-z)){ return "Cat A"; } else if (Math.abs(x - z) > Math.abs(y-z)){ return "Cat B"; } else{ return "Mouse C"; } }

abdulbasithCOC + 2 comments Your Code Is undoutedly Good.... But the Function call of the function "abs( )" Can be reduced from 4 to 3 by storing in some returned value in variables a,b and c....

trapeznikov_alex + 2 comments static String catAndMouse(int x, int y, int z) { switch(Integer.compare(Math.abs(z-x), Math.abs(z-y))){ case -1: return "Cat A"; case 1: return "Cat B"; } return "Mouse C"; }

syakubov + 0 comments [deleted]syakubov + 2 comments Slight tweak:

static String catAndMouse(int x, int y, int z) { switch(Integer.compare(Math.abs(z-x), Math.abs(z-y))){ case -1: return "Cat A"; case 1: return "Cat B"; default: return "Mouse C"; } }

trapeznikov_alex + 0 comments Yes that's better, thx )

nvinson234 + 1 comment You should probably avoid the switch here unless Integer.compare() is guaranteed to return -1, 0, or 1 as output or you divide the result of Integer.compare() by

`Math.abs(x-y)`

.typically, you only get the guarantee of <0, 0, >0 (and not -1, 0, 1) because compare functions like this are usually implemented as:

`return a - b;`

.trapeznikov_alex + 1 comment Can you point to this imlementation in any JRE?

But yes, in jadadoc we can see:

Returns: the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y

You absolutely right.

nvinson234 + 0 comments Probably not. I assume most Java implementations are based on openJDK, and I looked through the openJDK sources. They use:

public int compareTo(Integer anotherInteger) { int thisVal = this.value; int anotherVal = anotherInteger.value; return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1)); }

which came from whatever reference sources they started with (I'm guessing Sun's 1.2 implementation).

I can, however, show you glibc's strcmp() implementation which does implement compare using subtraction: https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strcmp.c;h=3485aa64c10072ed7ad52340c6690d0ca7602506;hb=1d714fd95da16f0d97c8c670a2c899f99c01eb45#l45

I can also point to example code in https://www.gnu.org/software/libc/manual/html_node/Comparison-Functions.html which also uses the subtraction method (albeit in a somewhat questional way in my opinion). That said, this function guarantees that -1, 0, or 1 will be returned since it's finding the difference between 2 relational expressions.

shashank_kapile + 0 comments **Optimized**Needs only two abs() function calls.static String catAndMouse(int x, int y, int z) { int xDist=Math.abs(x-z); int yDist=Math.abs(y-z); return xDist<yDist ? "Cat A": xDist>yDist ? "Cat B":"Mouse C"; }

mrojas6996 + 0 comments Your solution helps me to correct my logical on PHP (I've just helped with yours)

function catAndMouse(y, $z) {

`if(abs(`$y-$`z) > abs(`$x-$`z)){ return "Cat A"; }else if (abs(`$x-$`z) > abs(`$y-$`z)){ return "Cat B"; } else { return "Mouse C"; }`

}

RulesofGame + 1 comment Can anyone tell me how can we solve the problem if constrain was this: -100<=x,y,z<=100

scan2 + 0 comments Same extremely easy. it seems a bit complicated with all that 0 in the middle and possible to have, for example -5 6 -4. And abs will give that B is closer which is wrong. Solution...drums...drums...still drums...add to all input numbers +100 and then run as it would be 0<=x,y,z<=200 :D

francois_bracha1 + 4 comments The default C++ code is nonsense.

for(int a0 = 0; a0 < q; a0++){ int x; int y; int z; cin >> x >> y >> z; vector <string> result = catAndMouse(x, y, z); for (ssize_t i = 0; i < result.size(); i++) { cout << result[i] << (i != result.size() - 1 ? " " : ""); } cout << endl; }

There's no point in expecting the user to return a sentence by filling a vector with single words in order to print them separated with spaces. I think it is counter-intuitive and needlessly confusing, and it should be refactored like so:

for (int a0 = 0; a0 < q; a0++) { int x, y, z; cin >> x >> y >> z; string result = catAndMouse(x, y, z); cout << result << endl; }

with the return type of catAndMouse changed to string.

jafkm7 + 2 comments Python 3 has the same problem. A simple print(result) would have done it, but they put some nonsense with .join that added spaces between each of the characters and guaranteed that the code would fail both tests. I don't even know how you'd fix it without altering their code.

gg_germain + 1 comment Maybe that was part of the exercise... Takes only one minute to fix it anyway

jafkm7 + 2 comments I sincerely doubt that it was part of the exercise as it's not in line with any other exercise that I've seen on this site. And while it is easy to fix, I assume that at least some of the people who come here are coming to learn to code. That being the case, the provided code samples should at least work, and ideally, they should be examples of good coding.

It's also worth noting that if someone trusted the provided code, didn't really understand that it was wrong, much less why, and wanted to solve the exercise without "cheating" by looking up the answer, then it would make thier lives much, much harder. At that point, they're no longer solving the exercise. They're debugging someone else's badly written code. A useful exercise to be sure, but not the one this hypothetical person signed up for, and not one that they're aware they're engaged in.

gg_germain + 0 comments Indeed indeed. I suggest to simply print the function input. I quite often do it. It usually helps better visualizing the data we're dealing with. Sometimes, like in this case, it reveals an unexpected input format, which was a bit frustrating though easy to fix.

pritesh_inexture + 0 comments I did it in python 3 you just need to return 'Cat A' or 'Cat b' or 'Mouse C' main is calling function for the times of query.

sathyasathu182 + 0 comments Facing the same issue.

gragest + 0 comments it is the same in c#. it is asking for a string array

adammoses + 0 comments Yea, was a bit weird, still all the function needed was some abs() conditionals.

kairat_arzybaev + 0 comments its expected to use 'return' instead of 'print'

Soumyadeep_B + 1 comment Python 3:

def catAndMouse(x, y, z): a = abs(x-z) b = abs(y-z) return "Cat A" if a<b else "Cat B" if b<a else "Mouse C"

danialamin489 + 0 comments Oh thanks, I totally forgot about abs() method

epiccaterpi11er + 0 comments one line c++:

string catAndMouse(int x, int y, int z) { return (abs(x - z) < abs(y - z)) ? "Cat A" : (abs(x - z) > abs(y - z)) ? "Cat B" : "Mouse C"; }

how is this worth 15 points lol

Sort 492 Discussions, By:

Please Login in order to post a comment