Plus Minus

Sort by

recency

|

693 Discussions

|

  • + 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

  • + 1 comment

    public static void plusMinus(List arr) { var total = arr.Count(); var positives = arr.Where(p => p > 0).ToList(); Console.WriteLine(CalculateInputRatio(positives.Count, total));

        var negatives = arr.Where(p => p < 0).ToList();
        Console.WriteLine(CalculateInputRatio(negatives.Count, total));
    
        var xeros = arr.Where(p => p == 0).ToList();
        Console.WriteLine(CalculateInputRatio(xeros.Count, total));
    
    }
    
    private static string CalculateInputRatio(int numbersCount, int total)
    {
        decimal ratio = (decimal)numbersCount/total;
        return ratio.ToString("F6");
    }
    
  • + 0 comments
    def plusMinus(arr):
        
        print(f"{sum(1 for x in arr if x > 0)/n:.6f}")
        print(f"{sum(1 for x in arr if x < 0)/n:.6f}")
        print(f"{sum(1 for x in arr if x == 0)/n:.6f}")
    
    if __name__ == '__main__':
        n = int(input().strip())
    
        arr = list(map(int, input().rstrip().split()))
    
        plusMinus(arr)
    
  • + 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,
        );
    }