List of solved Hackerrank challenges in PHP language
function solveMeFirst($a,$b){ return $a + $b; }
function simpleArraySum($ar) { $sum = 0; foreach($ar as $value){ $sum+=$value; } return $sum; }
function compareTriplets($a, $b) { $length = count($a); $aliceScore = 0; $bobScore = 0; for($i=0; $i<$length; $i++){ if($a[$i] == $b[$i]){ continue; } if($a[$i] > $b[$i]){ $aliceScore++; continue; } $bobScore++; } return [$aliceScore, $bobScore]; }
function aVeryBigSum($ar) { $sum = 0; foreach($ar as $value){ $sum+=$value; } return $sum; }
function diagonalDifference($arr) { $length = count($arr); $primaryDiagonal = 0; $secondaryDiagonal = 0; $lastIndex = $length - 1; for($i = 0; $i<$length; $i++){ $primaryDiagonal+=$arr[$i][$i]; $secondaryDiagonal+=$arr[$i][$lastIndex--]; } return abs($primaryDiagonal - $secondaryDiagonal); }
function plusMinus($arr) { $POSITIVES = 0; $NEGATIVES = 1; $ZEROES = 2; $length = count($arr); // declare the temp numbers to index positions $numbers = [0, 0, 0]; // determine the plusMinus of the numbers foreach($arr as $val){ if($val === 0){ $numbers[$ZEROES] = $numbers[$ZEROES] + 1; continue; } if($val > 0){ $numbers[$POSITIVES] = $numbers[$POSITIVES] + 1; continue; } $numbers[$NEGATIVES] = $numbers[$NEGATIVES] + 1; } $plusMinusArr = array_map(function ($number) use ($length) { return number_format($number/$length, 6); }, $numbers); foreach($plusMinusArr as $plusMinus){ echo $plusMinus; echo "\n"; } }
function staircase($n) { $totalSteps = $n; $staircase = []; // start from the right steps for($n; $n>0; $n--){ $stairs = []; // proceed from left stair until the reach the final stair steps // then gradually increase the starting stair for($i=1; $i<=$n; $i++){ // if it reached the final step print the stairs "#" else the its spaces if($i == $n){ // calculate the total stairs to print $totalPrintOfStairs = ($totalSteps - $n) + 1; for($totalPrintOfStairs; $totalPrintOfStairs>0; $totalPrintOfStairs--){ array_push($stairs, "#"); } array_push($staircase, $stairs); break; } array_push($stairs, ""); } } // print the staircases foreach($staircase as $stairs){ foreach($stairs as $stair){ echo $stair; } echo "\n"; } }
function miniMaxSum($arr) { $maxSumTempArr = $arr; $minSumTempArr = $arr; // sort into ascending order and get the first four values sort($minSumTempArr); $minSumArr = array_splice($minSumTempArr, 0 ,4); // sort into descending order and get the first four values rsort($maxSumTempArr); $maxSumArr = array_splice($maxSumTempArr, 0, 4); echo array_sum($minSumArr) . '' . array_sum($maxSumArr); }
function birthdayCakeCandles($ar) { // count the recurring of the candle $candles = array_count_values($ar); // sort the recurring candle by descending & get the most recurring candle krsort($candles); $candlesBlowsValue = array_values($candles); return $candlesBlowsValue[0]; }
function timeConversion($s) { $strToTime = strtotime($s); return date('H:i:s', $strToTime); }
function gradingStudents($grades) { $MODULO = 5; $finalGrades = []; $finalGrades = array_map(function($grade) use ($MODULO){ // get the next multiplier by 5 $nextMultiplier = $grade + ($MODULO - $grade % $MODULO); $difference = $nextMultiplier - $grade; // do not round up if grade less than 40 // the difference is 3 and greater than 3 if($nextMultiplier < 40 || $difference === 3 || $difference > 3){ return $grade; } // round up if the difference is less than 3 return $nextMultiplier; }, $grades); return $finalGrades; }