Sort by

recency

|

3162 Discussions

|

  • + 0 comments
    def plusMinus(arr):
        # Write your code here
        total_count = len(arr)
        print(f'{sum([1 for x in arr if x > 0]) / total_count:.6f}')
        print(f'{sum([1 for x in arr if x < 0]) / total_count:.6f}')
        print(f'{sum([1 for x in arr if x == 0]) / total_count:.6f}')
    
  • + 0 comments

    Here is mine. It has also null check.

    public static void plusMinus(List arr) { double positiveQuantity, negativeQuantity, zeroQuantity = 0;

        foreach(var item in arr){
            if(item > 0) positiveQuantity++;
            else if(item < 0) negativeQuantity++;
            else zeroQuantity++;
        }
    
        int length = arr.Count;
    
        Console.WriteLine(positiveQuantity>0?(positiveQuantity/length):0);
        Console.WriteLine(negativeQuantity>0?(negativeQuantity/length):0);
        Console.WriteLine(zeroQuantity>0?(zeroQuantity/length):0);
    }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/OqevjJbsN2Q

    void plusMinus(vector<int> arr) {
        double sp, sn, sz, pa = 1.0 / arr.size();
        sp = sn = sz = 0;
        for(int i = 0; i < arr.size(); i++){
            if(arr[i] > 0) sp+= pa;
            else if(arr[i]<0) sn+= pa;
            else sz+= pa;
        }
        cout << setprecision(6) << fixed;
        cout << sp <<endl;
        cout << sn <<endl;
        cout << sz <<endl;
    }
    
  • + 0 comments

    Here is the solution in C:

    void plusMinus(int arr_count, int* arr) {
    double positive=0,negative=0,zero=0;
    for(int i=0;i<arr_count;i++){
        if(arr[i]>0) positive++;
        else if(arr[i]<0) negative++;
        else zero++;
    }
    positive/=arr_count;
    negative/=arr_count;
    zero/=arr_count;
    printf("%.6f\n%.6f\n%.6f",positive,negative,zero);
    }
    
  • + 0 comments

    Could be better. But here it is in golang:

    func plusMinus(arr []int32) {
        var pos int32
        var negs int32
        var zeros int32
        for _, a := range arr{
            if a < 0 {
                negs++
            } else if a > 0{
                pos++
            } else{
                zeros++
            }
            
        }
        positives := float32(pos) / float32(len(arr))
        negatives := float32(negs) / float32(len(arr))
        zers := float32(zeros) / float32(len(arr))
        fmt.Printf("%.6f\n%.6f\n%.6f\n", positives, negatives, zers)
        
    
    }