We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Cats and a Mouse
  5. Discussions

Cats and a Mouse

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 902 Discussions, By:

votes

Please Login in order to post a comment

  • amiya13
    5 years ago+ 11 comments

    this problem should be of 10 points, not 15

    218|
    Permalink
    View more Comments..
  • Soumyadeep_B
    4 years ago+ 5 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"
    
    36|
    Permalink
    View more Comments..
  • epiccaterpi11er
    4 years ago+ 1 comment

    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

    24|
    Permalink
  • francois_bracha1
    4 years ago+ 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.

    12|
    Permalink
    View more Comments..
  • danielbiocchi
    4 years ago+ 5 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";
            }
    
        }
    
    10|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature