Sort by

recency

|

3245 Discussions

|

  • + 0 comments

    Rust

    fn plusMinus(arr: &[i32]) {
        
        let mut a = 0;
        let mut b = 0;
        let mut c = 0;
        
        for i in 0..arr.len(){
                if arr[i] == 0 {
                    a += 1
                }
                
                if arr[i] > 0{
                    b += 1
                }
                
                if arr[i] < 0{
                    c += 1
                }
               
            };
        let len_float = arr.len() as f64;
        let calc_a = a as f64 / len_float;
        let calc_b = b as f64 / len_float;
        let calc_c = c as f64 / len_float;
        
        println!("{:.6}", calc_b);
        println!("{:.6}", calc_c);
        println!("{:.6}", calc_a);
    }
      
    
  • + 0 comments

    Great explanation of how to calculate the positive, negative, and zero ratios in an array. I like how clearly the logic was presented. It reminds me of how precision and ratio handling are important in simulation games like Car Parking Multiplayer too

  • + 0 comments

    Could not shorten this further

    def plusMinus(arr):
        arr = [i/abs(i) if i != 0 else 0 for i in arr]
        print(float(arr.count(1))/float(len(arr)), "\n", float(arr.count(-1))/float(len(arr)), "\n",float(arr.count(0))/float(len(arr)))
    
  • + 0 comments

    simple java code from scratch

    public static void plusMinus(List arr) { // Write your code here int plus=0,minus=0,zero=0,total=0; for(int x:arr) { if(x<0) minus++; else if(x==0) zero++; else if(x>0) plus++; total++;
    } double positive=(double)plus/total, negative=(double)minus/total, Zero=(double)zero/total;
    System.out.println(String.format("%.6f",positive)); System.out.println(String.format("%.6f",negative)); System.out.println(String.format("%.6f",Zero)); }

  • + 0 comments