Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same results looks wrong in HackerRank

Tags:

php

I am trying to learn PHP by answering questions in hackerrank.com but I am stuck on this one.

<?php

function plus_minus($array) {
    $dimension=count($array);
    $negative =0;
    $positive = 0;
    $zeroes = 0;
    foreach ($array as $element) {
        $element>0 ? $positive++ : null;
        $element<0 ? $negative++ : null;
        $element==0 ? $zeroes++ : null;
    }
    echo number_format($positive/$dimension, 6);
    echo '<br>';
    echo number_format($negative/$dimension,6);
    echo '<br>';
    echo number_format($zeroes/$dimension, 6);
}


$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
$arr = array_map('intval', $arr);

plus_minus($arr);

?>

Here a screenshot: [1]

Is the question wrong or I did I make mistakes?

like image 825
Begum Avatar asked Mar 09 '23 10:03

Begum


1 Answers

echo '\n';

I fixed my problem by doing this

print_r(number_format($positive/$dimension, 6)); 
print_r("\n");
print_r(number_format($negative/$dimension,6));
print_r("\n");
print_r(number_format($zeroes/$dimension, 6));
like image 180
Begum Avatar answered Mar 19 '23 05:03

Begum