Sort by

recency

|

1253 Discussions

|

  • + 0 comments
    def catAndMouse(x, y, z):
        if abs(x-z)<abs(y-z):
            return 'Cat A' 
        elif abs(x-z)>abs(y-z):
            return 'Cat B' 
        elif abs(x-z)==abs(y-z):
            return 'Mouse C'
    
  • + 0 comments

    if abs(x-z)==abs(y-z): return "Mouse C" elif(abs(x-z)>abs(y-z)): return "Cat B"
    else: return "Cat A"

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/vbV5-DqJU74

    string catAndMouse(int x, int y, int z) {
        int a = abs(x-z), b = abs(y-z);
        if(a==b) return "Mouse C";
        if(a < b) return "Cat A";
        return "Cat B";
    }
    

    Short version

    string catAndMouse(int x, int y, int z) {
        int a = abs(x-z), b = abs(y-z);
        return (a > b) ? "Cat B" : (a < b) ? "Cat A" : "Mouse C";
    }
    
  • + 0 comments

    Python solution

    def catAndMouse(x, y, z):
        if abs(z - x) < abs(z - y):
            return "Cat A"
        elif abs(z - x) > abs(z - y):
            return "Cat B"
        else:
            return "Mouse C"
    
  • + 0 comments

    Here is my C++ solution :

    string catAndMouse(int x, int y, int z) { int diffA = abs(z - x); int diffB = abs(z - y);

    if(diffA < diffB)
    {
        return "Cat A";
    }
    else if(diffB < diffA) 
    {
        return "Cat B";
    }
    else {
        return "Mouse C";
    }
    

    }