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:

  1. Môi trường PHP - Hiểu về thiết lập môi trường cơ bản bao gồm máy chủ web, cơ sở dữ liệu và cài đặt và thiết lập trình phân tích cú pháp PHP. & NBSP;- Understanding of basic environment setup including web server, database, and installation and setting up PHP Parser. 
  2. Biến - Hiểu biết cơ bản về bộ mảng được xác định trước có chứa các biến từ môi trường máy chủ web, bao gồm các biến toàn cầu và máy chủ. & NBSP;- Basic understanding of PHP set of predefined arrays containing variables from the web server environment, including global and server variables. 
  3. Ra quyết định và vòng lặp trong PHP - Hiểu biết cơ bản 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 PHP.- Basic understanding and ability to write code to perform conditional operations, decision making, and looping concepts in PHP.
  4. Mảng và chuỗi - Khả năng làm việc với các mảng số, kết hợp và đa chiều trong PHP và thực hiện các hoạt động chuỗi cơ bản như nối, thao tác, v.v. trong PHP.- Ability to work with numeric, associative, and multi-dimensional arrays in PHP, and performing basic string operations such as concatenation, manipulation, and so on in PHP.

hackerrank-php

Danh sách các thách thức hackerrank đã giải quyết bằng ngôn ngữ PHP

Thực hành> Thuật toán> khởi động

  • Giải quyết tôi trước

function solveMeFirst($a,$b){
	return $a + $b;
}

  • Tổng mảng đơn giản

function simpleArraySum($ar) {
    $sum = 0;
    foreach($ar as $value){
        $sum+=$value;
    }
    return $sum;
}

  • So sánh các bộ ba

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];
}

  • Một khoản tiền rất lớn

function aVeryBigSum($ar) {
    $sum = 0;
    foreach($ar as $value){
        $sum+=$value;
    }
    return $sum;
}

  • Sự khác biệt chéo

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);
}

  • Cộng với trừ đi

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";
    }
}

  • Cầu thang

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";
    }
}

  • Tổng Mini-Max

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);
}

  • Nến bánh sinh nhật

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];
}

  • Chuyển đổi thời gian

function timeConversion($s) {
    $strToTime = strtotime($s);
    return date('H:i:s', $strToTime);
}

  • Chấm điểm sinh viên

function simpleArraySum($ar) {
    $sum = 0;
    foreach($ar as $value){
        $sum+=$value;
    }
    return $sum;
}
0