Web Projects Consulting

MinAvgTwoSlice Demo Task at Codility.com

codility netbeansLesson 3 – Prefix Sums – MinAvgTwoSlice demo task solution code written in php. Gives 100/100 score at the time of publishing. The description of the problem is copyrighted, so please see the following link for it: https://codility.com/demo/take-sample-test/min_avg_two_slice

Solution in php:

function solution($A) {

    $cnt    = count($A);
    $cntm   = $cnt - 2;
    $min_avg_value = ($A[0] + $A[1])/2.0 ;
    $min_avg_pos = 0 ;

    for($i=0;$i<$cntm;$i++){
        if( ($A[$i] + $A[$i+1])/2 < $min_avg_value){
            $min_avg_value = ($A[$i] + $A[$i+1])/2;
            $min_avg_pos = $i;
        }
        if( ($A[$i] + $A[$i+1] + $A[$i+2])/3 < $min_avg_value){
            $min_avg_value = ($A[$i] + $A[$i+1] + $A[$i+2])/3;
            $min_avg_pos = $i;
        }
    }

    if(($A[$cnt - 1] + $A[$cnt - 2]) /2 < $min_avg_value){
        $min_avg_value = ($A[$cnt - 1] + $A[$cnt - 2]) /2;
        $min_avg_pos = $cnt-2;
    }

    return $min_avg_pos;
}

Given “AS IS”, can be ported from other languages from solutions found on the internet, please use with care.

Please note: we think that codility.com does not give a correct assessment of your real-world programming skills. For instance, reading the below would bring more understanding why: http://codility-test-questions.blogspot.com/2013/01/my-experience-with-codility-test.html

Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.