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: [
Is the question wrong or I did I make mistakes?
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With