2016-02-22

Codility Answer 04 PART02


Here is my answers to Cod1l1ty training lesson 04 - PART02.
PassingCars


function solution($A) {
    $tot = 0;
    foreach($A as $val) {
        $tot += $val;
    }
    
    $pass = 0;
    $curr = 0;
    foreach($A as $val) {
        if($val == 0) {
            $pass += ($tot - $curr);
        }
        
        $curr += $val;
    }
    
    return ($pass > 1000000000 ? -1 : $pass);
}

GenomicRangeQuery

function solution($S, $P, $Q) { $n = strlen($S); $s = []; for($i = 0; $i < $n; $i++) { switch($S[$i]) { case 'A': $s[] = 1; break; case 'C': $s[] = 2; break; case 'G': $s[] = 3; break; default: $s[] = 4; break; } } $n = count($P); $ret = []; for($i = 0; $i < $n; $i++) { $min = 0; for($j = $P[$i]; $j <= $Q[$i]; $j++) { if($min == 1) { break; } if($min > $s[$j] || $min == 0) { $min = $s[$j]; } } $ret[] = $min; } return $ret; }
Note: not 100% yet

2015-12-02

Sorry for not posting anything for so long.
Currently busy helping church here building their website. It's almost done and can be checked at www.basileiausa.org just need the content ready.

2015-10-28

Codility Answers 04

Here is my answer to Cod1l1ty training lesson 04.
CountDiv
function solution($A) {
////  Brute-force way
//    $n = count($A);
//    $max = null;
//    for($p = 0; $p < $n - 2; $p++) {
//        for($q = $p + 1; $q < $n -1; $q++) {
//            for($r = $q + 1; $r < $n; $r++) {
//               $product = $A[$p] * $A[$q] * $A[$r];
//                if($max === null || $max < $product){
//                    $max = $product;
//                }
//            }
//        }
//    }
    
//  Better way
    $p = null;
    $q = null;
    $r = null;
    $p1 = null;
    $q1 = null;
    $r1 = null;
    
    foreach($A as $val){
        if($p === null || $val >= $p) {
            $r = $q;
            $q = $p;
            $p = $val;
        }elseif($q === null || $val >= $q) {
            $r = $q;
            $q = $val;
        }elseif($r === null || $val >= $r) {
            $r = $val;
        }
        if($p1 === null || $val <= $p1) {
            $r1 = $q1;
            $q1 = $p1;
            $p1 = $val;
        }elseif($q1 === null || $val <= $q1) {
            $r1 = $q1;
            $q1 = $val;
        }elseif($r1 === null || $val <= $r1) {
            $r1 = $val;
        }
    }
    
    if($p >= 0){
        if($r <= 0 || $p1 * $q1 > $q * $r) {
            $q = $p1;
            $r = $q1;
        }
    }
    
    return $p*$q*$r;
}

Should use merge sort for best performance, but because I didn't know how to do it yet, so I'll leave it like this for now.

2015-10-27

Codility Answers 03

Here is my answer to Cod1l1ty training lesson 03.
CountDiv
function solution($a, $b, $k) {
    $i = ($a % $k == 0 ? $a : $a + ($k - ($a % $k)));
    
    $ret = 0;
    if ($i < $b) {
        $ret = (int)(($b - $i) / $k) + 1;
    }elseif($i == $a){
        $ret = 1;
    }
    
    return $ret;
}

PassingCars
function solution($A) {
    $n = count($A);
    $sum = [];
    $m = 0;
    for($i = 0; $i < $n; $i++) {
        if($A[$i] == 0) {
            $sum[] = 0;
            $m++;
        }elseif($m > 0){
            $sum[$m - 1]++;
        }
    }
    
    $sumTot = 0;
    for($i = 0; $i < $m; $i++) {
        $sumTot += ($sum[$i] + ($i * $sum[$i]));
        if($sumTot > 1000000000) {
            return -1;
        }
    }
    
    return $sumTot;
}

MinAvgTwoSlice
function solution($A) {
    $n = count($A);
    
    $idx = 0;
    $min = null;
    $sums = [$A[0]];
    for($p = 0; $p < $n - 1; $p++) {
        for($q = $p + 1; $q < $n; $q++) {
            $sums[$q] = $sums[$q - 1] + $A[$q];
            
            $avg = ($sums[$q] - $sums[$p] + $A[$p]) / ($q - $p + 1);
            if($avg < $min || $min === null) {
                $min = $avg;
                $idx = $p;
            }
        }
    }
    return $idx;
}

GenomicRangeQuery
function solution($S, $P, $Q) {
    $ret = [];
    $m = count($P);
    for($i = 0; $i < $m; $i++) {
        $min = '';

        for($j = $P[$i]; $j <= $Q[$i]; $j++) {
            if($min == '' || $min > $S[$j]) {
                $min = $S[$j];
            }
        }

        switch($min) {
            case 'A':
                $ret[] = 1;
                break;
            case 'C':
                $ret[] = 2;
                break;
            case 'G':
                $ret[] = 3;
                break;
            default:
                $ret[] = 4;
        }
    }
    
    return $ret;
}

2015-10-26

Codility Answers 02

Here is my answer to Cod1l1ty training lesson 02.
FrogRiverOne
function solution($X, $A) {
    $c = count($A);
    $current = 0;
    $leaves = [];
    for($i = 0; $i < $c; $i++){
        if($current + 1 == $A[$i]){
            $current++;
        }
        while($current < $X){
            if(!isset($leaves[$current + 1])){
                break;
            }else{
                $current++;
            }
        }
        if($current >= $X){
            return $i;
        }
        
        $leaves[$A[$i]] = 1;
    }
    
    return -1;
}

PermCheck
function solution($a) {
    $tempArr = [];
    $n = count($a);
    
    for($i = 0; $i < $n; $i++){
        if($a[$i] > $n || isset($tempArr[$a[$i]])){
            return 0;
        }
        
        $tempArr[$a[$i]] = 1;
    }
        
    return 1;
}

MissingInteger
function solution($a) {
    $tempArr = [];
    $missing = 1;
    $n = count($a);
    $lookAtTemp = false;
    for($i = 0; $i < $n; $i++){
        if($a[$i] == $missing){
            $missing++;
            $lookAtTemp = true;
        }else{
            $tempArr[$a[$i]] = 1;
        }
        
        while($missing < $n + 1 && $lookAtTemp){
            if(isset($tempArr[$missing])){
                $missing++;
            }else{
                $lookAtTemp = false;
            }
        }
    }
    
    return $missing;
}

MaxCounters
class ct{
    private $arr;
    private $ct;
    
    public function __construct($n){
        $this->ct = $n;
        for($i = 0; $i < $n; $i++){
            $this->arr[$i] = 0;
        }
    }
    
    public function increase($x){
        $this->arr[$x-1] += 1;
    }
    
    public function maxCounter(){
        $max = 0;
        foreach($this->arr as $val){
            $max = ($max < $val ? $val : $max);
        }
        for($i = 0; $i <= $this->ct; $i++){
            $this->arr[$i] = $max;
        }
    }
    
    public function getArr(){
        return $this->arr;
    }
}

function solution($n, $a) {
    $counter = new ct($n);
    
    foreach($a as $val){
        if($val >= 1 && $val <= $n){
            $counter->increase($val);
        }elseif($val == $n + 1){
            $counter->maxCounter();
        }
    }
    
    return $counter->getArr();
}

2015-10-25

Cake Bank

Tutorial on how to use CakePHP to make the same banking that we do before. So far, it looks more simple that directly using PHP.

Here, I'll also show you how to setup Netbeans to use a remote server.



Sorry for the silence, I think without sound makes it faster :p

2015-10-24

Codility Answers 01

Here is my answer to Cod1l1ty training lesson 01.
TapeEquilibrium
function sumArr(array $a){
    $n = count($a);
    
    $sum = 0;
    for($i = 0; $i < $n; $i++){
        $sum += $a[$i];
    }
    
    return $sum;
}

function solution(array $a) {
    $n = count($a);
    
    if($n == 1){
        return $a[0];
    }
    
    $min = null;
    $p = 1;
    $sum1 = 0;
    $sum2 = sumArr($a);
    do{
        $sum1 += $a[$p - 1];
        $sum2 -= $a[$p - 1];
        $diff = abs($sum1 - $sum2);
        if($diff == 0){
            return 0;
        }elseif($min === null || $diff < $min){
            $min = $diff;
        }
        $p++;
    }while($p < $n);
    
    return $min;
}

FrogJmp
function solution($x, $y, $d) {
    $dist = $y - $x;
    if($dist % $d >= 1){
        return ((int)($dist / $d) + 1);
    }else{
        return (int)($dist / $d);
    }
}

PermMissingElem
function solution($a) {
    $tempArr = [];
    $missing = 1;
    $n = count($a);
    $lookAtTemp = false;
    for($i = 0; $i < $n; $i++){
        if($a[$i] == $missing){
            $missing++;
            $lookAtTemp = true;
        }else{
            $tempArr[$a[$i]] = 1;
        }
        
        while($missing < $n + 1 && $lookAtTemp){
            if(isset($tempArr[$missing])){
                $missing++;
            }else{
                $lookAtTemp = false;
            }
        }
    }
    
    return $missing;
}