Plus Minus

Sort by

recency

|

696 Discussions

|

  • + 0 comments
        public static void plusMinus(List<Integer> arr) {
            // Write your code here
            
            int positiveRatio = 0;
            int negativeRatio = 0;
            int zerosRatio = 0;
            
            int length = arr.size();
            
            for(int num: arr){
                if(num == 0){
                    zerosRatio += 1;
                }else if(num > 0){
                    positiveRatio += 1;
                }else{
                    negativeRatio += 1; 
                }
            }
        
            System.out.printf("%.6f%n", (double) positiveRatio / length);
            System.out.printf("%.6f%n", (double) negativeRatio / length);
            System.out.printf("%.6f%n", (double) zerosRatio / length);
        }
    
  • + 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");
    }