Sort by

recency

|

1270 Discussions

|

  • + 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]
    
  • + 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'