Plus Minus

Sort by

recency

|

278 Discussions

|

  • + 0 comments

    Scala

    def plusMinus(arr: Array[Int]) {
        // Write your code here
        val n = arr.length
        var positiveCount, negativeCount, zeroCount = 0
        
        arr.map(elem => {
            if(elem > 0) positiveCount = positiveCount + 1
            else if(elem < 0) negativeCount = negativeCount + 1
            else zeroCount = zeroCount + 1
        })
        val positiveRatio = positiveCount.toFloat/n
        val negativeRatio = negativeCount.toFloat/n
        val zeroRatio = zeroCount.toFloat/n
        
            println(f"$positiveRatio%.6f")
            println(f"$negativeRatio%.6f")
            println(f"$zeroRatio%.6f")
        }
    }
    
  • + 0 comments
    function plusMinus(arr: number[]): void {
        const d: number = arr.length;
        let p: number = 0;
        let z: number = 0;
        let n: number = 0;
        
        arr.forEach((num): number => num > 0 ?  p++ : num < 0 ? n++ : z++);
       
        [p, n, z].forEach((c): void => console.log((c/d).toFixed(6)));
    }
    
  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'plusMinus' function below.

    #

    The function accepts INTEGER_ARRAY arr as parameter.

    #

    def plusMinus(arr): pos_num = 0 neg_num = 0 zero_num = 0

    iterate over array

    for num in arr:
        if num > 0:
            pos_num += 1
        elif num < 0:
            neg_num += 1
        else:
            zero_num += 1
    

    Calculate totals

    total = len(arr)
    pos_ratio = pos_num/total
    neg_ratio = neg_num/total
    zero_ratio = zero_num/total
    

    Print totals

    print("{:.6f}".format(pos_ratio))   
    print("{:.6f}".format(neg_ratio))
    print("{:.6f}".format(zero_ratio))   
    

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

    arr = list(map(int, input().rstrip().split()))
    
    plusMinus(arr)
    
  • + 0 comments

    This is the Java code

    public static void plusMinus(List arr) { // Write your code here int len=arr.size(); float posCount=0; float negCount=0; float zeroCount=0; for(int i = 0; i < len ; i++) { int elements = arr.get(i); if(elements>0){ posCount++; } else if(elements<0){ negCount++; } else{ zeroCount++; } } System.out.printf("%.6f%n",posCount/len); System.out.printf("%.6f%n",negCount/len); System.out.printf("%.6f%n",zeroCount/len);

    }
    

    }

  • + 0 comments

    foreach(int number in arr) { if (number>0) {
    positives+=1; } else if (number<0) { negatives+=1; } else { zeros+=1; }
    } System.Console.WriteLine("{(negatives/arr.Count):F6}"); System.Console.WriteLine($"{(zeros/arr.Count):F6}");