Categories
General

How to fix Laravel Nova Issue ? Failed to download Laravel/Nova …

Failed to download laravel/nova from dist: The 'https://nova.laravel.com/dist/laravel/nova/laravel-nova-0a8ec5b37dc3ca29f4b79c7318c9278e5a5d2c1a-zip-7e4351.zip' URL could not be accessed: HTTP/2 403

Two ways to fix this problem

  • Execute the following command with the email and password parameters
composer config http-basic.nova.laravel.com [email protected] NovaHasspassword
  • create an auth.json password and paste the following content
{"username":"[email protected]","password":"asdsadasdqweqwerewrew”}

Categories
General

“Unprocessable Entity” is not valid header name

As of March 20th 2022, https://github.com/GrahamCampbell updated the following library guzzlehttp/psr7

so in your unit test you will see errors as follow

InvalidArgumentException: "Unprocessable Entity" is not valid header name
InvalidArgumentException: "409 conflict with the current state of the target resource" is not valid header name
InvalidArgumentException: "Not Found" is not valid header name
InvalidArgumentException: "Not Acceptable" is not valid header name
InvalidArgumentException: "Not Found" is not valid header name
InvalidArgumentException: "Bad Request" is not valid header name
InvalidArgumentException: "409 conflict with the current state of the target resource" is not valid header name
InvalidArgumentException: "500 Internal Server Error" is not valid header name

The version 1.8.5 was breaking our set of unit test

Solution is to update composer.json and use this version below

"guzzlehttp/psr7": "1.8.3",

2.1.1 – 2022-03-20

Fixed

  • Validate header values properly

https://github.com/guzzle/psr7/blob/2.2.1/CHANGELOG.md

Categories
General

trunk-based development

https://www.devbridge.com/articles/branching-strategies-git-flow-vs-trunk-based-development/

https://trunkbaseddevelopment.com/

Categories
General

Palindrome 2



class Solution {
    
    public function palindrome2 (string $s) : bool {
        #aba - true
        #abca - 
        #abc - 
        
        $n = strlen($s); #4
        $p1 = 0;
        $p2 = $n - 1 ; #3
        $mid = floor(($n - 1) / 2); #1
        $charRemoved = false;
        
        while($p1 < $mid || $p2 > $mid) 
        {

            
            # p1:1 < mid:1 OR 
            # p2:2 > mid:1
            if ($s[$p1] == $s[$p2]) {
                $p1++; #1
                $p2--;  #1
                continue;
            }
            
            if($s[$p1+1] == $s[$p2] && $charRemoved ==false) {
                $p1 = $p1 + 1;
                $charRemoved = true;
                continue;
            } else if ($s[$p1+1] == $s[$p2] && $charRemoved == true) {
                return false;
            }

            if($s[$p2-1] == $s[$p1] && $charRemoved ==false) {
                $p2 = $p2 - 1;
                $charRemoved = true;
                continue;
            } else if ($s[$p2-1] == $s[$p1] && $charRemoved == true) {
                return false;
            }
            
            if(($s[$p1] != $s[$p2]) && ($p1 + 1 == $mid || $p2 - 1 == $mid) ) {
                return false;
            }
        }
        
        if($s[$p1] != $s[$p2] && (($n % 2) == 0)) {
            return true;
        }
        
        return true;
    }
    
}

$res = new Solution();
var_dump($res->palindrome2('abbca'));
Categories
General

How to delete a Git branch locally and remotely ?

Steps to delete qa branch

Step 1 – delete locally

git branch -d dev

Step 2 – delete remotely

git push origin origin/dev
git push origin --delete origin/dev
Categories
General

Screen Fitting – php

class Solution {

    
    public function screenFitting($rows, $cols, $sentence)
    {
        # intiialization
        $numberWords = count($sentence);
        $wordIndex = 0;
        $c = 0;
        $r = 0;

        # loop foreach row
        while($r < $rows && $c < $cols) {
            
                $wordLength = strlen($sentence[$wordIndex]); # 5 {hello} ; 5 {world}
                if(($c + $wordLength) < $cols) { # 5 < 7 ;  !! 11 < 7 ; 5 < 7; !! 11 < 7
                    $c += $wordLength;   # 5 ; 7
                    $hashmap[$sentence[$wordIndex]] += 1;
                    $wordIndex++; #1 

                    var_dump($hashmap);
                } else {
                    $r++;  #1 
                    $c = 0;
                    continue;
                }
                $c++; # 6

                if($wordIndex === $numberWords) {
                    $wordIndex = 0;
                }
            }
            
        return min($hashmap);
    }
}

$rows = 2;
$cols = 7;
$sentence = ['hello', 'world'];

$sentence = ["i","had","apple","pie"];
$rows = 4;
$cols = 15;
$solution = new Solution();
$res = $solution->screenFitting($rows, $cols, $sentence);

var_dump($res);

Simplified solution

$s = ["hello", "world"];
$cols = 7;
$rows = 6;

$col = 0;
$row = 0;
$i = 0;

$count = 0;;
while($row < $rows) 
{
    while($col < $cols) 
    {        
        $col = $col + strlen($s[$i]);
        if($col < $cols)  {
            continue;
        }
        $i++;
        if($i == 2) {
            $i = 0;
            $count++;
        }
    }
     $col = 0;
     $row++;
    
}
                            
print $count;
Categories
General

Max Connected Colors in a Grid


    
class Solution {

  public $gridNumberRows;
  public $gridNumberCols;
  public $grid;

  public function __construct() 
  {
  
  }

  public function maxConnectedColorValue(array $grid) : int
  {
     $this->grid = $grid;
    print $this->gridNumberRows = count($grid);
    $countCol = 0;
    foreach ($grid[0] as $col)
    {
      $countCol++;
    }
      
      print "\n\r";
   print  $this->gridNumberCols = $countCol;
    
      
      
    # visted cells to false
    for($r = 0; $r < $this->gridNumberRows ; $r++) {
      for($c = 0; $c < $this->gridNumberCols ; $c++)  {
        $visited[$r][$c] = false;
      }
    }

   
    $hashmapColor = [];
    for($row = 0; $row < $this->gridNumberRows ; $row++) {
      for($col = 0; $col < $this->gridNumberCols ; $col++)  {
        
        
        if($visited[$row][$col] === true) {
          continue;
        }

        
        if($grid[$row][$col] === ''){
          $visited[$row][$col] = true;
          continue;
        }

        $cellColor= $grid[$row][$col];
        $visited[$row][$col] = true;
        if(!array_key_exists($cellColor, $hashmapColor)) {
          $hashmapColor[$cellColor] = 1;
        }
        
        $this->dfs($row, $col, $visited, $hashmapColor);
        
        
        
      }
    }    
    var_dump($hashmapColor);
    return max($hashmapColor);
  }
  
    public function dfs($r, $c, &$visited, &$hashmapColor)
    {
      
      $cellColor = $this->grid[$r][$c];
        if($cellColor == '')
            return;
                
                
      # structure [R,C]
      $neighborInstruction = [[-1,0],[0,1],[1,0],[0,1]];
      
      foreach ($neighborInstruction as $neighbor) 
      {
        $neighborRow = $neighbor[0];
        $neighborCol = $neighbor[1];
        
    

          
          
          
        #if whithin boundaries
        if(((($neighborRow + $r) >= 0) && ($neighborRow + $r) < $this->gridNumberRows)
          && ($neighborCol + $r) >= 0 && ($neighborCol + $r) < $this->gridNumberCols)
        {
        
          # check same color
          if($this->grid[$neighborRow + $r][$neighborCol + $c] === $this->grid[$r][$c] 
            && $visited[$neighborRow + $r][$neighborCol + $c] === false) {
            
              if (array_key_exists($cellColor, $hashmapColor)) {
                $hashmapColor[$cellColor] += 1;
            } 

            $visited[$r][$c] = true;
            $this->dfs($neighborRow + $r, $neighborCol + $c, $visited, $hashmapColor);

          }
        }
        
      }
    }

  
}



$grid = [
['','r','b',''],
['','b','b','b'],
['','','b','']];

$solution = new Solution();
$res = $solution->maxConnectedColorValue($grid);

var_dump($res);
Categories
General

Binary tree – average sum Level – php – facebook


class Node {
  
  public $value;
  public $left;
  public $right;
  
  public function __construct(int $value) 
  {
    $this->value = $value;
   $this->left = null;
   $this->right = null;
  }
}



class Solution {

  public function bfs($node, &$hashmap, $level) 
  {
    
    if($node === null) {
      return;
    }

    
    $queue = new SplQueue();
  
    if($node->left)
    {
      $queue->enqueue($node->left);  
    }

    if($node->right)
    {
      $queue->enqueue($node->right);
    }


    
    while ($queue->count() > 0) {
    
      $childNode = $queue->pop();
      
      $hashmap[$level][] = $childNode->value;
      
        $this->bfs($childNode, $hashmap, $level + 1);
      
    }

    
  }
  
  public function AverageSumLevel($node) 
  {
    $hashmap = [];
    $level = 0;
    $hashmap[$level][] = $node->value;
    $this->bfs($node, $hashmap, $level + 1) ;
    
      
    foreach ($hashmap as $level => $values) 
    {
      $result[] = array_sum($values) / count($values);
    }
      
      
    return $result;
  }
}


$node5 = new Node(5);
$node5->left = new Node(4);
$node5->right = new Node(8);


$node5->left->left = new Node(2);
$node5->left->right = new Node(1);

$node5->right->right = new Node(9);
$node5->right->right->right = new Node(12);

$solution = new Solution();
$res = $solution->AverageSumLevel($node5);
var_dump($res);
Categories
General

Seating Arrangements Facebook – PHP

Seating ArrangementsThere are n guests attending a dinner party, numbered from 1 to n. The ith guest has a height of arr[i-1] inches.The guests will sit down at a circular table which has n seats, numbered from 1 to n in clockwise order around the table. As the host, you will choose how to arrange the guests, one per seat. Note that there are n! possible permutations of seat assignments.Once the guests have sat down, the awkwardness between a pair of guests sitting in adjacent seats is defined as the absolute difference between their two heights. Note that, because the table is circular, seats 1 and n are considered to be adjacent to one another, and that there are therefore n pairs of adjacent guests.The overall awkwardness of the seating arrangement is then defined as the maximum awkwardness of any pair of adjacent guests. Determine the minimum possible overall awkwardness of any seating arrangement.

Signature

int minOverallAwkwardness(int[] arr)

Input

n is in the range [3, 1000]. Each height arr[i] is in the range [1, 1000].

Output

Return the minimum achievable overall awkwardness of any seating arrangement.

Example

n = 4 arr = [5, 10, 6, 8] output = 4If the guests sit down in the permutation [3, 1, 4, 2] in clockwise order around the table (having heights [6, 5, 8, 10], in that order), then the four awkwardnesses between pairs of adjacent guests will be |6-5| = 1, |5-8| = 3, |8-10| = 2, and |10-6| = 4, yielding an overall awkwardness of 4. It’s impossible to achieve a smaller overall awkwardness.Report Bug

 
function minOverallAwkwardness($arr) {
  // Write your code here

    #big O NLogN
    sort($arr);
    $n = count($arr);
    
    $temp = $arr[$n-1];
    $arr[$n-1] = $arr[$n-2] ;
    $arr[$n-2] = $temp;
    

    return smallestAwkwardnes($arr);
}

function smallestAwkwardnes($arr){
    $heap = new SplMaxHeap();
    for ($i=0; $i < count($arr) - 1; $i++) {
        $akwardness = $arr[$i] - $arr[$i + 1];
        if($akwardness < 0){
            $akwardness *= -1;
        }
        $heap->insert($akwardness);
    }
    return $heap->extract();
}

Categories
General

Element Swapping Facebook

Element SwappingGiven a sequence of n integers arr, determine the lexicographically smallest sequence which may be obtained from it after performing at most k element swaps, each involving a pair of consecutive elements in the sequence.Note: A list x is lexicographically smaller than a different equal-length list y if and only if, for the earliest index at which the two lists differ, x’s element at that index is smaller than y’s element at that index.

Signature

int[] findMinArray(int[] arr, int k)

Input

n is in the range [1, 1000]. Each element of arr is in the range [1, 1,000,000]. k is in the range [1, 1000].

Output

Return an array of n integers output, the lexicographically smallest sequence achievable after at most k swaps.

Example 1

n = 3 k = 2 arr = [5, 3, 1] output = [1, 5, 3]We can swap the 2nd and 3rd elements, followed by the 1st and 2nd elements, to end up with the sequence [1, 5, 3]. This is the lexicographically smallest sequence achievable after at most 2 swaps.

Example 2

n = 5 k = 3 arr = [8, 9, 11, 2, 1] output = [2, 8, 9, 11, 1]We can swap [11, 2], followed by [9, 2], then [8, 2].

   function findMinArray(array $arr, int $k) : array
  {
    #arr = [5, 3, 1]
    # k:2, 1
    while($k >= 1) 
    {
    
      # swapping
      $temp = $arr[$k];        # temp 1      ; 1
      $arr[$k] = $arr[$k - 1]; # $arr[2] = 3; $arr[1] = 5
      $arr[$k - 1] = $temp;    # $arr[1] = 1; $arr[0] = 1

      #arr = [5, 1, 3]
      #arr = [1, 5, 3]
      $k--; #1 #0
    }
    
    return $arr;
    
  }