Sort by

recency

|

3252 Discussions

|

  • + 0 comments

    I used list comprehension + f-string (Python 3)

    def plusMinus(arr):
         # Write your code here
        p = len([k for k in arr if k>0])
        n = len([i for i in arr if i<0])
        z = len([j for j in arr if j==0])
        
        print(f'{p/len(arr):.6f}')
        print(f'{n/len(arr):.6f}')
        print(f'{z/len(arr):.6f}')
    
  • + 0 comments
        # Write your code here
        pos = neg = zero = 0
        n = len(arr)
        
        for value in arr:
            if (value == 0):
                zero += 1
            else:
                if (value > 0):
                    pos += 1
                else:
                    neg += 1
                    
        print(round(pos/n, 6))
        print(round(neg/n, 6))
        print(round(zero/n, 6))
    
  • + 0 comments

    Here is my Java solution for the “Plus Minus” challenge.

    Sharing in case it helps someone:

    https://github.com/eduardocintra/hacker-rank-solutions/blob/main/src/main/java/br/com/eduardocintra/easy/plusminus/PlusMinus.java

  • + 0 comments
    function plusMinus(arr) {
        // Write your code here
        const len = arr.length;
        let pos = 0;
        let neg = 0;
        let zero = 0;
        for(let i in arr) {
            arr[i] > 0 ? pos++ : arr[i] == 0 ? zero++ : neg++
        }
        const firstRatio = (pos / len).toFixed(6)
        const secRatio = (neg / len).toFixed(6)
        const thirdRatio = (zero / len).toFixed(6)
        console.log(firstRatio)
        console.log(secRatio)
        console.log(thirdRatio)
    }
    
  • + 1 comment

    Here is HackerRank Plus Minus solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-plus-minus-problem-solution.html