# Cats and a Mouse

# Cats and a Mouse

amiya13 + 7 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.

vs97849 + 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.

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 + 1 comment 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.

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 + 0 comments 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"

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

abhiramsatpute + 1 comment python 3

for p in range(int(input())): x,y,z = map(int,input().split()) print("Cat A" if abs(z - x) < abs(z - y) else "Cat B" if abs(z - x) > abs(z - y) else "Mouse C")

beckwithdylan + 2 comments did the same except i always like to put the options together and the conditionals together for cleanliness

print(['Cat A','Cat B', 'Mouse C'][0 if abs(x-z) < abs(y-z) else 1 if abs(x-z) > abs(y-z) else 2])

abhiramsatpute + 0 comments thanks.. it definitely is cleaner

snkt_pat2 + 0 comments Thanks for sharing.

I did something like this.

def catAndMouse(x, y, z): return 'Cat A'*(abs(z-x)<abs(z-y))+'Cat B'*(abs(z-x)>abs(z-y))+'Mouse C'*(abs(z-x)==abs(z-y))

itisashutoshwor1 + 2 comments i couldnt understand why they failed my test case when i copied the case to custom test case it ran successfully, IDK what to do gave me 0 points.

Kanahaiya + 0 comments Hello Coding Lover,

Here is the video explaination of my solution in

**O(n) time**-and you can find most of the

**hackerrank solutions with video explaination**here-https://github.com/Java-aid/Hackerrank-Solutions

and many more needs to be addeed.

Any comments , feedback will be highly appreciated.

Thanks & Regards,

Kanahaiya Gupta

Git Hub URL | https://github.com/Java-aid/

LIKE US | https://www.facebook.com/javaaid/

SUBSCRIBE US | https://www.youtube.com/c/JavaAidTutorials

TELEGRAM LINK| http://t.me/javaaid

nish92rao + 0 comments The test cases seem to be corrupted.

kv1003 + 1 comment Easy JS code:

const catAndMouse = (x, y, z) => { const distanceCatA = Math.abs(x - z) const distanceCatB = Math.abs(y - z) if (distanceCatA === distanceCatB) { return 'Mouse C' } else if (distanceCatA < distanceCatB) { return 'Cat A' } else { return 'Cat B' } }

paulmcdowell18 + 0 comments Easier JS:

`function catAndMouse(x, y, z) { return (Math.abs(x-z) > Math.abs(y-z)) ? 'Cat B' : ((Math.abs(x-z) < Math.abs(y-z)) ? 'Cat A' : 'Mouse C') }`

iAmAStudent + 1 comment in JavaScript,

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

1988_domi + 1 comment If you define your Math.abs() at the beggining of the function should be quicker because for each if() it does Math.abs()

iAmAStudent + 0 comments Good point.

sanketsharma98 + 1 comment can anyone explain vector split_string(string); function

tpgreene + 0 comments The unique algorithim removes consecutive duplicates. The added in predicate function is saying "remove dups if 1 of them is a space and so is the other". This has the effect of moving each character forward in the string by however many were erased and returns a "new end" iterator (which points to the end of the new, shorter string). There are some "junk chars" at the end of the original string, so erase is called to clear these. The next while loop is removing any spaces from the end of the string (since they would only have been removed by unique if they were consecutive duplicates).

Then we set the delimiter to a space and use it to search for the individual words in the string. Using this delimiter, substrings are added to the "splits" vector of strings. The string::npos is the greatest value and means that it has reached the end of the string. So the min of this value or length of string (it will be length of string, because it is smaller) minus the most-recent starting point (i), will give you the index of the last char in string (which is also the last char of the last word). However, substr takes the length (including the starting char) of the substr as its second argument, so we add the +1 so that it will include the last char (since strings are zero indexed).

Sort 426 Discussions, By:

Please Login in order to post a comment