Plus Minus

Sort by

recency

|

697 Discussions

|

  • + 0 comments
    def plusMinus(arr):
        # Write your code here
        total = len(arr)
    
        positives = len([v for v in arr if v > 0]) / total
        negatives = len([v for v in arr if v < 0]) / total
        zeros = len([v for v in arr if v == 0]) / total
        
        def print_r(val: float):
            print(f"{round(val, 6)}")
        
        print_r(positives)
        print_r(negatives)
        print_r(zeros)
    
  • + 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