Plus Minus

  • + 0 comments

    Scala

    def plusMinus(arr: Array[Int]) {
        // Write your code here
        val n = arr.length
        var positiveCount, negativeCount, zeroCount = 0
        
        arr.map(elem => {
            if(elem > 0) positiveCount = positiveCount + 1
            else if(elem < 0) negativeCount = negativeCount + 1
            else zeroCount = zeroCount + 1
        })
        val positiveRatio = positiveCount.toFloat/n
        val negativeRatio = negativeCount.toFloat/n
        val zeroRatio = zeroCount.toFloat/n
        
            println(f"$positiveRatio%.6f")
            println(f"$negativeRatio%.6f")
            println(f"$zeroRatio%.6f")
        }
    }