Sort by

recency

|

3239 Discussions

|

  • + 0 comments

    Python 3

    def plusMinus(arr):
        n = 0
        p = 0
        z = 0
        l = len(arr)
        for x in arr:
            if x == 0:
                z+=1
            elif x > 0:
                p+=1
            else:
                n+=1
        
        [print(round(x,6)) for x in [float(p/l),float(n/l),float(z/l)]]
    
  • + 0 comments

    For Python3 Platform

    I wrote the code from scratch just to get more practice

    def plusMinus(arr):
        positive = len([i for i in arr if i > 0])
        negative = len([i for i in arr if i < 0])
        zero = len([i for i in arr if i == 0])
        
        print(f"{positive/len(arr):.6f}")
        print(f"{negative/len(arr):.6f}")
        print(f"{zero/len(arr):.6f}")
    
    n = int(input())
    arr = list(map(int, input().split()))
    
    plusMinus(arr)
    
  • + 0 comments

    def plusMinus(arr): n = len(arr) positive = neg = zero = 0

    for num in arr:
        if num > 0:
            positive += 1
        elif num < 0:
            neg += 1
        else:
            zero += 1
    
    # Print ratios with 6 decimal places
    print("{0:.6f}".format(positive / n))
    print("{0:.6f}".format(neg / n))
    print("{0:.6f}".format(zero / n))
    
  • + 0 comments
    using CategoryGroup = System.Collections.Generic.Dictionary<Category, int>;
    
    enum Category {
        Positive,
        Negative,
        Zero
    }
    
    class Result
    {
    
        /*
         * Complete the 'plusMinus' function below.
         *
         * The function accepts INTEGER_ARRAY arr as parameter.
         */
    
        public static void plusMinus(List<int> arr)
        {
            
            
            var total = arr.Count();
            CategoryGroup counts = new CategoryGroup
            {
                { Category.Positive, 0 },
                { Category.Negative, 0 },
                { Category.Zero, 0 },
            };
            
            
            arr.ForEach(n => {
               if(n > 0) counts[Category.Positive]++;
               else if(n<0) counts[Category.Negative]++;
               else counts[Category.Zero]++;
            });
            
            printCounts(counts, total);
        }
        
        private static void printCounts(CategoryGroup counts, int total)
            => counts.Keys.ToList().ForEach(key 
                => Console.WriteLine(
                        calcFraction(counts[key], total)
                    )
                );
        private static string calcFraction(int countOf, int total) => 
            ((double) countOf / total).ToString("F6");
    
    }
    
  • + 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);