Sort by

recency

|

1267 Discussions

|

  • + 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):
        #
        # Write your code here.
        #
        
        step1 = abs(z - x)
        step2 = abs(z - y)
        
        if step1 == step2:
            return 'Mouse C'
            
        elif step1 < step2:
            return 'Cat A'
        
        elif step2 < step1:
            return 'Cat B'
        
    
  • + 0 comments

    // Complete the catAndMouse function below.

    static string catAndMouse(int x, int y, int z) {
    
        int mouseToCatA = int.Abs(x - z);
        int mouseToCatB = int.Abs(y - z);
    
        if(mouseToCatA == mouseToCatB)
        {
            return "Mouse C";
        }
        else if(mouseToCatA < mouseToCatB)
        {
            return "Cat A";
        }
        else return "Cat B";
    
    }
    
  • + 0 comments

    Perl:

    sub catAndMouse {
        my ($x, $y, $z) = @_;
        
        return "Mouse C" if (abs($y - $z) == abs($x - $z));
        return (abs($y - $z) > abs($x - $z)) ? "Cat A" : "Cat B";
    }
    
  • + 0 comments
    def catAndMouse(x, y, z):
        cat_a, cat_b = abs(x-z), abs(y-z)
        return "Mouse C" if cat_a == cat_b else "Cat A" if cat_a < cat_b else "Cat B"