Codility Lesson Solutions Using PHP

by | December 3, 2020 | Website Development

This page may contain affiliate links to recommended products or services. If you choose to purchase after clicking a link, I may receive a commission at no additional cost to you.

Last updated on: February 7, 2023

For website developers, software engineers, programmers, hackers, and coders, Codility is a site aimed at helping you practice and refine your algorithmic programming skills.

From the coder’s side of Codility’s services, there are free coding lessons and challenge puzzles. Coding lessons and challenges test your coding skills and math acumen. They provide you with a problem and task you with solving it in your most efficient way. Your coding solution gets submitted, pumped through their automated unit testing, and returned with a score of how your code performed (it’s all about performance and correctness) – you’re graded on a scale of 1-100%.

My Experience with Codility

I was introduced to Codility through my professional network.

The coding lessons have been a fun way to practice my PHP and sharpen problem solving abilities. Admittedly, I did not solve them all within the allotted timeframes or without seeking help from outside resources. However, these are practice lessons and learning how to solve math puzzles in efficient ways provides a great deal of personal growth and sound mental exercise – it’s all good stuff. Learning for pleasure makes learning more pleasurable.

IMHO, the hardest part is getting a feel for interpreting the objectives and making assumptions about how solutions are interpreted by the automated testing processes. Figuring out what the questions are asking and expecting for a solution is half the battle. Putting together pieces to provide a solution is relatively straight forward when you know how to proceed. 

GitHub PHP Solution Code Repository

To see my PHP code solutions check out my Codility PHP Lesson Solutions on Github

Sample – Finding Missing Integer (PHP)

Here’s a sample problem involving finding a missing number within a random dataset.

The Puzzle

Write a function: function solution($A); that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.

For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [1, 3], the function should return 1.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [1,000,000..1,000,000].

My 100/100 Codility PHP Solution

Here is my 100/100 Codility PHP solution to the missing integer puzzle.

function solution($A) {
    if ( !is_array($A) ) {

        return 1;

    }

    $max = max($A);
    $max_range = 1000000;

    if ( $max < 1 ) {

        return 1;

    }

    if ( $max > $max_range ) {

        return 1;

    }

    $min = min($A);
    $min_range = -1000000;

    if ( $min < $min_range ) {

        return 1;

    }

    $working_array = [];

    for ( $i=0, $limit = count($A); $i < $limit; $i++ ) {

        if ( $A[$i] > 0 &amp;&amp; !isset($working_array[$A[$i]]) ) {

            $working_array[$A[$i]] = $A[$i];

        }

    }

    $first = min($working_array);

    if ( $first != 1 ) {

        return 1;

    }

    $count = count($working_array);

    if ( $count == 1 && $working_array[$first] == 1 ) {

        return 2;

    }

    if ( $count == 1 && $working_array[$first] != 1 ) {

        return 1;

    }

    for ( $i=$first, $limit=($first + $count); $i < $limit; $i++ ) {

        if ( array_key_exists($i, $working_array) === false ) {

            return $i;

        }

    }

    return ( $max + 1 > $max_range ) ? 1 : ($max + 1);
}
Floyd Hartford is a website developer from southern Maine. He's focused on creating and building WordPress websites and loves spending time digging into code like HTML, CSS, scss, jQuery, PHP, and MySQL.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

9 + 4 =

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Looking for WordPress Help?

I’m a freelance web developer specializing in small business and corporate marketing websites.

Pin It on Pinterest