Sort by

recency

|

3235 Discussions

|

  • + 0 comments

    float po = 0.0f; float neg = 0.0f; float ze = 0.0f; int n=arr.size(); for(int i=0;i0){ po++; } } float a =(float) (po/n); System.out.printf("%.6f%n",a); float b =(float) (neg/n); System.out.printf("%.6f%n",b); float c =(float) (ze/n); System.out.printf("%.6f%n",c);

  • + 0 comments

    This is my code in Python 3.

    def plusMinus(arr):
        n = len(arr)
        pos = [i for i in arr if i > 0]
        neg = [i for i in arr if i < 0]
        zero = [i for i in arr if i == 0]
        
        print(len(pos) / n)
        print(len(neg) / n)
        print(len(zero) / n)
    
    if __name__ == '__main__':
        n = int(input().strip())
        arr = list(map(int, input().rstrip().split()))
        plusMinus(arr)
    
  • + 0 comments
    void plusMinus(vector<int> arr) {
    	int p = 0, n = 0, z = 0;
    	for(int el : arr){
    		if(el > 0) 
    			p++;
    		else if(el < 0) 
    			n++;
    		else 
    			z++;
    	}
    	double total = arr.size();
    	double pr = p / total;
    	double nr = n / total;
    	double zr = z / total;
    
    	// Set the output to print 6 decimal places
    	cout << fixed << setprecision(6);
    	// Print each result on a new line
    	cout << fixed << setprecision(6);
    	cout << pr << "\n" << nr << "\n" << zr;
    }
    
  • + 0 comments

    Rust:

    fn plusMinus(arr: &[i32]) {
        let mut pos: f32 = 0.0;
        let mut neg: f32 = 0.0;
        let mut zer: f32 = 0.0;
        let all = arr.len() as f32;
        
        for &i in arr {
            match i {
                i if i > 0 => pos += 1 as f32,
                i if i < 0 => neg += 1 as f32,
                i => zer += 1 as f32,
            }
        }
        println!("{}", pos / all);
        println!("{}", neg / all);
        println!("{}", zer / all);
    }
    
  • + 0 comments

    JS:

    function plusMinus(arr: number[]): void {
        // Write your code here
        let pos = 0;
        let neg = 0;
        let zer = 0;
        let all = arr.length;
        
        for (const num of arr) {
            switch (true) {
                case num > 0: pos++; break;
                case num < 0: neg++; break;
                default: zer++;
            }
        }
        
        console.log(pos / all);
        console.log(neg / all);
        console.log(zer / all);
    }