Plus Minus

Sort by

recency

|

694 Discussions

|

  • + 0 comments

    **JAVA **

    int length = arr.size(); int countPositive = 0; int countNegative = 0; int countZeroes = 0;

    for(int i : arr){
        if(i > 0){
            countPositive++;
        } else if(i < 0){
            countNegative++;
        } else{
            countZeroes++;
        }
    }
    
        double positiveRatio = (double) countPositive / length;
        double negativeRatio = (double) countNegative / length;
        double zeroRatio = (double) countZeroes / length;
    
        System.out.println(String.format("%.6f", positiveRatio));
        System.out.println(String.format("%.6f", negativeRatio));
        System.out.println(String.format("%.6f", zeroRatio));
    

  • + 0 comments

    Depending on the size of the dataset, it can use CompletableFuture or even a ThreadPool (in C# TPL for async and parallel tasks as well). But this task is quite simple, so it would not be required

  • + 1 comment

    public static void plusMinus(List arr) { var total = arr.Count(); var positives = arr.Where(p => p > 0).ToList(); Console.WriteLine(CalculateInputRatio(positives.Count, total));

        var negatives = arr.Where(p => p < 0).ToList();
        Console.WriteLine(CalculateInputRatio(negatives.Count, total));
    
        var xeros = arr.Where(p => p == 0).ToList();
        Console.WriteLine(CalculateInputRatio(xeros.Count, total));
    
    }
    
    private static string CalculateInputRatio(int numbersCount, int total)
    {
        decimal ratio = (decimal)numbersCount/total;
        return ratio.ToString("F6");
    }
    
  • + 0 comments
    def plusMinus(arr):
        
        print(f"{sum(1 for x in arr if x > 0)/n:.6f}")
        print(f"{sum(1 for x in arr if x < 0)/n:.6f}")
        print(f"{sum(1 for x in arr if x == 0)/n:.6f}")
    
    if __name__ == '__main__':
        n = int(input().strip())
    
        arr = list(map(int, input().rstrip().split()))
    
        plusMinus(arr)
    
  • + 0 comments

    let final ={p:0,n:0,z:0}; for(let i of arr){ if(i===0){ final['z']=(final['z']||0) +1; } if(i<0){ final['n']=(final['n']||0) +1; } if(i>0){ final['p']=(final['p']||0) +1; } } Object.values(final).map(item=>console.log(item/arr.length))