Last active
April 20, 2020 03:28
-
-
Save ThienTranDuy/ba118ba1e14d77e66378674eb88e95e6 to your computer and use it in GitHub Desktop.
Hackerrank challenge - plusMinus - https://www.hackerrank.com/challenges/plus-minus/problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function plusMinus($arr) { | |
$arrLen = count($arr); | |
$posi = count(array_filter($arr, "countPosi")); | |
$nega = count(array_filter($arr, "countNega")); | |
$zero = count(array_filter($arr, "countZero")); | |
$ratioPosi = $posi / $arrLen; | |
$ratioNega = $nega / $arrLen; | |
$ratioZero = $zero / $arrLen; | |
echo abs(number_format($ratioPosi, 6, '.', '' )) ."\n"; | |
echo abs(number_format($ratioNega, 6, '.', '' )) ."\n"; | |
echo abs(number_format($ratioZero, 6, '.', '' )) ."\n"; | |
} | |
function countPosi($val) {return $val > 0;} | |
function countNega($val) {return $val < 0;} | |
function countZero($val) {return $val === 0;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment