Plus Minus

Sort by

recency

|

663 Discussions

|

  • + 0 comments

    in c# public static void plusMinus(List arr) {

        int positiveNum =0  ,  negativeNum=0 ,  zeros =0;
        int size = arr.Count(); 
        foreach(int x in arr ){
            if(x >0 ){
                positiveNum++;
            }    
             else if (x<0){
                 negativeNum ++;
             }
             else{
                 zeros++;
             }    
        }
        Console.WriteLine((positiveNum/(double)size).ToString("F6"));
        Console.WriteLine((negativeNum/(double)size).ToString("F6"));
        Console.WriteLine((zeros/(double)size).ToString("F6"));
    }
    
  • + 0 comments

    enumerate is slightly faster than for

    def plusMinus(arr): positives = 0 zeros = 0 negatives = 0 size = len(arr)

    for number arr:
        if number > 0 :
            positives += 1
        elif number == 0 :
            zeros += 1
        else:
            negatives += 1
    
    print(f'{positives/size:.6f}')
    print(f'{negatives/size:.6f}')
    print(f'{zeros/size:.6f}')
    

    if name == 'main': n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))
    
    plusMinus(arr)
    
  • + 0 comments
    # 
    
    public static void main(String[] args) {
       double add= 0;
        double sub=0;
        double zero=0;
        Scanner sc =new Scanner(System.in);
        // System.out.println("Enter the no of elements");
        int n =sc.nextInt();    
        int [] arr= new int[n];
        // System.out.println("Enter the elements");
    
    
        for(int i=0;i<arr.length;i++) {
            arr[i]=sc.nextInt();
            if (arr[i]>=1 && arr[i]<=100) {
                add+=1;
            }
            if (arr[i]<=-1 && arr[i]>=-100) {
                sub+=1;
            }
            if (arr[i]==0) {
                zero+=1;
            }
    
          }
        float n1 = n;
    
       System.out.printf("%.6f%n",add/n1);
       System.out.printf("%.6f%n",sub/n1);
       System.out.printf("%.6f%n",zero/n1);
    
        sc.close();
    }
    

    }

  • + 0 comments

    const { positives, negatives, zeros } = arr.reduce((acc, x) => { if (x > 0) acc.positives += 1; else if (x < 0) acc.negatives += 1; else acc.zeros += 1; return acc; }, { positives: 0, negatives: 0, zeros: 0 });

    console.log((positives / length).toFixed(6));
    console.log((negatives / length).toFixed(6));
    console.log((zeros / length).toFixed(6));
    
  • + 1 comment

    Java streams

    public static void plusMinus(List<Integer> arr) {
    
        long negValues = arr.stream().filter(x -> x < 0).count();
        long posValues = arr.stream().filter(x -> x > 0).count();
        long zeroValues = arr.stream().filter(x -> x == 0).count();
        long length = negValues + posValues + zeroValues;
        
        
        System.out.println((double)posValues/length);
        System.out.println((double)negValues/length);
        System.out.println((double)zeroValues/length);
    
        }