Plus Minus

  • + 0 comments

    My PHP Solution:

    <?php
    
    /*
     * Complete the 'plusMinus' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    function plusMinus($arr) {
        $positivi = 0;
        $negativi = 0;
        $base0 = 0;
        $dimensione = count($arr);
        
        foreach($arr as $item){
            switch(true){
                case $item > 0:
                $positivi++;
                break;
                case $item === 0:
                $base0++;
                break;
                case $item < 0:
                $negativi++;
                break;
            }
        }
    
        echo sprintf("%.6f", $positivi / $dimensione) . "\n";
        echo sprintf("%.6f", $negativi / $dimensione) . "\n";
        echo sprintf("%.6f", $base0 / $dimensione) . "\n";
    
    }
    
    $n = intval(trim(fgets(STDIN)));
    
    $arr_temp = rtrim(fgets(STDIN));
    
    $arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
    
    plusMinus($arr);