Hướng dẫn hackerrank php practice - thực hành php hackerrank
PHP là một ngôn ngữ kịch bản đa năng nguồn mở được sử dụng rộng rãi, đặc biệt phù hợp để phát triển web và có thể được nhúng vào HTML. PHP là viết tắt của bộ tiền xử lý siêu văn bản và được tích hợp với một số cơ sở dữ liệu phổ biến, bao gồm MySQL, PostgreSQL, Oracle, Sybase, Informix và Microsoft SQL Server. & NBSP; Năng lực quan trọng này bao gồm sự hiểu biết cơ bản về môi trường PHP, các mảng PHP được xác định trước có chứa các biến, sự hiểu biết và khả năng viết mã để thực hiện các hoạt động có điều kiện, ra quyết định và lặp lại các khái niệm, trong số những người khác. & NBSP; Năng lực chính:
Danh sách các thách thức hackerrank đã giải quyết bằng ngôn ngữ PHP
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 simpleArraySum($ar) { $sum = 0; foreach($ar as $value){ $sum+=$value; } return $sum; }0 |