Plus Minus

Sort by

recency

|

690 Discussions

|

  • + 0 comments

    let final ={p:0,n:0,z:0}; for(let i of arr){ if(i===0){ final['z']=(final['z']||0) +1; } if(i<0){ final['n']=(final['n']||0) +1; } if(i>0){ final['p']=(final['p']||0) +1; } } Object.values(final).map(item=>console.log(item/arr.length))

  • + 0 comments
    /*
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    fn plusMinus(arr: &[i32]) {
        let pos_count :f64 = arr 
            .into_iter()
            .filter(|&x| (*x > (0 as i32)))
            .count() as f64;
        
        let zero_count :f64 = arr
            .into_iter()
            .filter(|&x| (*x == (0 as i32)))
            .count() as f64;
            
        println!("{:.6}\n{:.6}\n{:.6}",
            pos_count / arr.len() as f64,
            ( arr.len() as f64 - (pos_count + zero_count) ) / arr.len() as f64,
            zero_count / arr.len() as f64,
        );
    }
    
  • + 0 comments

    Kotlin Solution:

    fun plusMinus(arr: Array<Int>): Unit {
        var zero: Double = 0.0
        var positive: Double = 0.0
        var negative: Double = 0.0
        
        for (n in arr){
            if (n > 0){
                positive++
            } else if (n <0){
                negative++
            } else {
                zero++
            }
        }
        println(String.format("%.6f", (positive / arr.size).toDouble()))
        println(String.format("%.6f", (negative / arr.size).toDouble()))
        println(String.format("%.6f", (zero / arr.size).toDouble()))
    }
    
  • + 0 comments
    def plusMinus(arr):
        # Write your code here
        pos = neg = zer = 0
        for i in arr:
            if i<0:
                neg +=1
            elif i>0:
                pos +=1
            else:
                zer +=1
        
        print("{:.6f}".format(pos/len(arr)))
        print("{:.6f}".format(neg/len(arr)))
        print("{:.6f}".format(zer/len(arr)))
            
    

    Python based implementation

    print("{:.6f}".format(pos/len(arr)))
    print("{:.6f}".format(neg/len(arr)))
    print("{:.6f}".format(zer/len(arr)))
    
  • + 0 comments

    In C# i solve simple way with cast

         int positiveCount = 0;
         int negativeCount = 0;
         int zeroCount = 0;
    
         foreach(int num in arr){
            if (num > 0)
                positiveCount++;
            else if (num < 0)
                negativeCount++;
            else 
                zeroCount++;
         }
    
         double positiveRatio = (double)positiveCount / arr.Count;
         double negativeRatio = (double)negativeCount / arr.Count;
         double zeroRatio = (double)zeroCount / arr.Count;
    
         Console.WriteLine(positiveRatio.ToString("F6"));
         Console.WriteLine(negativeRatio.ToString("F6"));
         Console.WriteLine(zeroRatio.ToString("F6"));