Lesson 3 – Prefix Sums – GenomicRangeQuery 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/genomic_range_query
Solution in php:
function solution($S, $P, $Q) { $S = str_split($S); $len = count($S); $lep = count($P); $arr = array(); $result = array(); $clone = array_fill(0, 4, 0); for($i = 0; $i < $len; $i++){ $arr[$i] = $clone; switch($S[$i]){ case 'A': $arr[$i][0] = 1; break; case 'C': $arr[$i][1] = 1; break; case 'G': $arr[$i][2] = 1; break; default: $arr[$i][3] = 1; break; } } for($i = 1; $i < $len; $i++){ for($j = 0; $j < 4; $j++){ $arr[$i][$j] += $arr[$i - 1][$j]; } } for($i = 0; $i < $lep; $i++){ $x = $P[$i]; $y = $Q[$i]; for($a = 0; $a < 4; $a++){ $sub = 0; if($x - 1 >= 0){ $sub = $arr[$x - 1][$a]; } if($arr[$y][$a] - $sub > 0){ $result[$i] = $a + 1; break; } } } return $result; }
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