Comparing human code with AI generated code

WINW > Recommmendations > Comparing human code with AI generated code

In this post we’re going to compare human manual coding with AI generated code for the same problem.

We’re going to use the platform Codility as the vendor of the problem.

Here’s the link of the problem that we’re going to solve: https://app.codility.com/programmers/lessons/1-iterations/


1. Human generated code

// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";

/*

*/
function solution($number) {

    $binary = (string) decbin($number);
    // print "$binary\n";

    $numOneCount = substr_count($binary, '1');
    $numZeroCount = substr_count($binary, '0');

    if ($numOneCount === 0 
        || $numOneCount === 1
        || $numZeroCount === 0
    ) {
        return 0;
    }

    $singleNumbers = str_split($binary);

    // print_r($singleNumbers);
    // print "\n";

    $binaryGapsCount = [];
    $binaryGapsIndex = 0;
    foreach($singleNumbers as $val) {
        if ($val == 1) {
            $binaryGapsIndex++;
        } else {
            $binaryGapsCount[$binaryGapsIndex]++;
        }
    }
    // print_r($binaryGapsCount);

    unset($binaryGapsCount[0]);
    // print "\n";

    return max($binaryGapsCount);
}

How it works:


2. AI generated code


Leave a Reply