Plus Minus

Sort by

recency

|

695 Discussions

|

  • + 0 comments

    C# Code static void plusMinus(List arr) { int total = arr.Count; int positive = 0, negative = 0, zero = 0; foreach (int num in arr) { if (num > 0) positive++; else if (num < 0) negative++; else zero++;

        Console.WriteLine($"{(double)(positive) % total:F6}");
        Console.WriteLine($"{(double)(negative) % total:F6}");
        Console.WriteLine($"{(double)(zero) % total:F6}");
    
    }
    

    }

  • + 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)