Sort by

recency

|

1271 Discussions

|

  • + 0 comments

    function catAndMouse( catAPosition , catBPosition , mousePostion ) {

    const catAStep = Math.abs(mousePostion - catAPosition); const catBStep = Math.abs( mousePostion - catBPosition );

    let difference = catAStep - catBStep;

    let result = (difference === 0 )? 'Mouse C':(difference > 0)?'Cat B':'Cat A'; return result }

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-cats-and-a-mouse-problem-solution.html

  • + 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):
        
        min_catA = (z-x)**2
        min_catB = (z-y)**2 
        
        if min_catA > min_catB : 
            return "Cat B"
        elif min_catB > min_catA:
            return "Cat A"
        else : 
            return "Mouse C"
    
  • + 0 comments

    I thought using a dictionary of exclusive boolean statements would be fun

    `def catAndMouse(x, y, z):

    catA_dist = abs(x-z)
    catB_dist = abs(y-z)
    
    d = {catA_dist < catB_dist:'Cat A',
         catA_dist > catB_dist:'Cat B',
         catA_dist == catB_dist:'Mouse C',
    }
    
    return d[True]