Categories
General

How to window sliding ? How to find the greatest sum of 4 elements in array ?

<?php 
function maxSum( $arr, $n, $k)
{
    // n must be greater
    if ($n < $k)
    {
        echo "Invalid";
        return -1;
    }
 
    // Compute sum of first
    // window of size k
    $max_sum = 0;
    for($i = 0; $i < $k; $i++)
    $max_sum += $arr[$i];
 
    // Compute sums of remaining windows by
    // removing first element of previous
    // window and adding last element of
    // current window.
    $window_sum = $max_sum;
    for ($i = $k; $i < $n; $i++)
    {
        echo "(window_sum) $window_sum \n\r";
        $window_sum += $arr[$i] - $arr[$i - $k];
        echo "(arr[$i])". $arr[$i]." - (arr[$i - $k])".$arr[$i - $k]."\n\r";
        echo "$i (window_sum) $window_sum \n\r";
        
        $max_sum = max($max_sum, $window_sum);
    }
 
    return $max_sum;
}
 
    // Driver code
    $arr = array(1, 4, 2, 10, 2, 3, 1, 0, 20);
    $k = 4;
    $n = count($arr);
    echo maxSum($arr, $n, $k);
 
// This code is contributed by anuj_67
Categories
General

How to resolve 3 sums alternative to O(n^3) in PHP

Complexity Analysis: 

  • Time Complexity : O(n2). 
    Only two nested loops are required, so the time complexity is O(n2).
  • Auxiliary Space : O(1), no extra space is required, so the time complexity is constant.
<?php
// PHP program to find
// triplets in a given
// array whose sum is zero
 
// function to print
// triplets with 0 sum
function findTriplets($arr, $n)
{
    $found = false;
 
    // sort array elements
    sort($arr);
 
    for ($i = 0; $i < $n - 1; $i++)
    {
        // initialize left
        // and right
        $l = $i + 1;
        $r = $n - 1;
        $x = $arr[$i];
        while ($l < $r)
        {
            if ($x + $arr[$l] +
                     $arr[$r] == 0)
            {
                // print elements if
                // it's sum is zero
                echo $x," ", $arr[$l],
                        " ", $arr[$r], "\n";
                $l++;
                $r--;
                $found = true;
            }
 
            // If sum of three elements
            // is less than zero then
            // increment in left
            else if ($x + $arr[$l] +
                          $arr[$r] < 0)
                $l++;
 
            // if sum is greater than
            // zero than decrement
            // in right side
            else
                $r--;
        }
    }
 
    if ($found == false)
        echo " No Triplet Found" ,"\n";
}
 
// Driver Code
$arr = array (0, -1, 2, -3, 1);
$n = sizeof($arr);
findTriplets($arr, $n);
 
// This code is contributed by ajit
?>

Complexity Analysis: 

  • Time Complexity: O(n2). 
    Since two nested loops are required, so the time complexity is O(n2).
  • Auxiliary Space: O(n). 
    Since a hashmap is required, so the space complexity is linear.

<?php
 
// function to print triplets with 0 sum
function findTriplets($arr, $n)
{
    $found = false;
 
    for ( $i=0; $i<$n-1; $i++)
    {
        // Find all pairs with sum equals to
        // "-arr[i]"
       $s = array();
        for ( $j=$i+1; $j<$n; $j++)
        {
            $x = -($arr[$i] + $arr[$j]);
            if (in_array($x, $s))
            {
                echo "FOUND => $x, $arr[$i], $arr[$j] \n\r";
                $found = true;
            }
            else
                $s[] = $arr[$j];
        }
    }
 
    if ($found == false)
        echo " No Triplet Found" ;
}
 
// Driver code
function main()
{
    $arr = array(0, -1, 2, -3, 1);

    $n = count($arr);
    findTriplets($arr, $n);
    return 0;
}

main();
Categories
Interview Interview questions

How to fibonacci with PHP ? Recursively

This function runs in O(2 exponent N)

function fibonacci(int $n) {
    if ($n == 0) {
        return 0; 
        
    }
    if ($n == 1) {
        return 1;
    }
    
    
    return fibonacci($n-1) + fibonacci($n-2);
}

echo 'FINAL ' . fibonacci(9);
Categories
Interview Interview questions

How to Fibonacci Bottom-Up Memoization PHP ? Dynamic Programming

This function runs in O(n) time

<?php
function fibonacci(int $n) {
    if ($n == 0) {
        return 0; 
        
    }    else if ($n == 1) {
        return 1;
    }
    
    $memo = array_fill(0, $n, 0);
    $memo[0] = 0;
    $memo[1] = 1;
    
    for ($i = 2; $i < $n; $i++) {
        
        $memo[$i] = $memo[$i - 1] + $memo[$i - 2] ;   
        echo    "memo[$i] ($memo[$i]) = memo[$i - 1] + memo[$i - 2] \n\r";   
        
    }
    return $memo[$n - 1] + $memo[$n - 2];
}

OR

function fibonacci(int $n) {
    if ($n == 0) {
        return 0; 
        
    }    else if ($n == 1) {
        return 1;
    }
    
    $memo = array_fill(0, $n, 0);
    $a = 0;
    $b = 1;
    
    for ($i = 2; $i < $n; $i++) {
        
       $c = $a + $b ;   
        $a = $b;
        $b = $c;
        echo    "c ($c) = a ($a) + b ($b)  \n\r";   
        
    }
    return $a + $b;
Categories
Interview Interview questions

How to Fibonacci Top-Down Memoization PHP ?

Top-Down Memoization PHP Fibonacci Problem

function fibonacci1(int $n) {
    
    $result = array_fill(0, $n + 1, 0);
<?php
function fibonacci1(int $n) {
    
    $result = array_fill(0, $n + 1, 0);
    return fibonacci($n,$result );
}

function fibonacci(int $i, $memo) { 
    
    if ($i == 0 || $i == 1) return $i;
    
    if ($memo[$i] == 0) {
        $memo[$i] = fibonacci($i - 1, $memo) + fibonacci($i - 2, $memo);
    }
    
    echo " memo[$i] ".$memo[$i]."\n\r";
    
    return $memo[$i];
}

echo 'FINAL ' . fibonacci1(9);

Categories
General

How to get Git deploy key

Step 1

Step 2

(from MAC)

$ pbcopy < ~/.ssh/id_rsa.pub

Step 3

Categories
General

How to fix Laravel Trailing Data Exception ?

Setting timestamps to false means you are going to lose both created_at and updated_at whereas you could set both of the keys in your model.

Case 1:

You have created_at column but not update_at you could simply set updated_at to false in your model

class ABC extends Model {

const UPDATED_AT = null;

Case 2:

You have both created_at and updated_at columns but with different column names

You could simply do:

class ABC extends Model {

const CREATED_AT = 'name_of_created_at_column';
const UPDATED_AT = 'name_of_updated_at_column';

Finally ignoring timestamps completely:

class ABC extends Model {

public $timestamps = false;

Trailing data is a Carbon error, it’s because you probably use Postgres and your date returns milliseconds.

“created_at” => “2018-04-19 07:01:19.929554”

You can add the following method to your (base) model.

public function getDateFormat()
{
     return 'Y-m-d H:i:s.u';
}

source : https://stackoverflow.com/questions/50210435/laravel-trailing-data-exception-when-model-save-or-update

Categories
General

Happy Friday Everyone!

Categories
Git

GIT : How to discard all local changes within a branch ?

RESET BRANCH/FILE

-- force discard —
git reset --hard origin/<davidBranch_name>

example with the master branch


git reset --hard origin/master
Categories
PHPUnit

What do you verify before deploying your code ?

vendor/bin/phpcs --standard=PSR2 --colors -p src/ && vendor/bin/phpunit --stop-on-failure  

PSR-2 Code Styling

This command returns all PSR-2 (code styling errors)

vendor/bin/phpcs --standard=PSR2 ~/directory_to_scan

vendor/bin/phpcs –standard=PSR2 –colors -pv src/ApiBundle

with more information

This command fix all fixable PSR-2 (code styling errors) issues

vendor/bin/phpcbf --standard=PSR2 --report=json ~/directory_to_scan

Run the Functional and Unit Tests

vendor/bin/phpunit –stop-on-failure