Sort by

recency

|

1254 Discussions

|

  • + 0 comments

    java (8)

    static String catAndMouse(int x, int y, int z) {
    
        String result = "Mouse C";
    
        int catA = Math.abs(z - x);
        int catB = Math.abs(z - y);
    
        if (catA > catB) {
            result = "Cat B";
        }
    
        else if (catA < catB) {
    
            result = "Cat A";
    
        }
    
        return result;
    
    }
    
  • + 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
    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

    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"