About strictly increasing sequence

Problem:
You are given an array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input.

Solution:

function arrayChange($inputArray) {
    $count = 0;
        
    foreach ( $inputArray as $key => $value ) {
        $is_less = isset( $key ) && isset( $inputArray[ $key-1 ] ) && $value <= $inputArray[ $key-1 ];
        if ( $is_less ) {        
            $inputArray[ $key ] = ( $inputArray[ $key - 1 ] ) + 1;
            $count += $inputArray[ $key-1 ]- $value + 1;
        }
    }
  
    return $count;
}

Calculator in PHP

To develop a simple calculator class in the context of PHP is more fun with __call() magic method.

class Calculator {
    public function __call($method, $args) {
        if (isset($this->$method)) {
            $func = $this->$method;
            return call_user_func_array($func, $args);
        }
    }
}

$calculator = new Calculator();
$calculator->add = function ( $a1, $a2 ) { echo $a1 + $a2; };
$calculator->subs = function( $a1, $a2 ) { echo $a1 - $a2; };
$calculator->multi = function( $a1, $a2 ) { echo $a1 * $a2; };
$calculator->div = function( $a1, $a2 ) { echo $a1 / $a2; };

Is two arrays are similar?

Problem:

Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.

Given two arrays a and b, check whether they are similar.

Example

  • For a = [1, 2, 3] and b = [1, 2, 3], the output should be
    areSimilar(a, b) = true.The arrays are equal, no need to swap any elements.
  • For a = [1, 2, 3] and b = [2, 1, 3], the output should be
    areSimilar(a, b) = true.We can obtain b from a by swapping 2 and 1 in b.
  • For a = [1, 2, 2] and b = [2, 1, 1], the output should be
    areSimilar(a, b) = false.Any swap of any two elements either in a or in b won’t make a and b equal.

Solution:

function areSimilar($a, $b) {
    $array_difference = array_diff_assoc($a, $b);
    if( empty( $array_difference )) return true;
    if( count($array_difference) === 2) {
        $b_a_array_difference = array_diff_assoc($b, $a);
        $is_equal = array_diff($array_difference, $b_a_array_difference);
        if(!empty($is_equal)) {
            return false;
        }
        return true;
    }
    return false;
}

Counting Vowels

In JavaScript:

let str =
  "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum";
const vowels = /[aeiouy]/g;
let vowelCount = str.match(vowels).length;
console.log(`Total Vowel ${vowelCount}`);

Output: Total Vowel 174

In PHP

$str = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
$vowels = "/[aeiouy]/";
echo $vowel_count = preg_match_all( $vowels, $str );

Output: 174

( … ) Variable-length argument lists

In programming, there are many ways to solve a problem. But, everyone likes to easy and short solution.

The reason of telling that, PHP has a syntax token that will make my day. This is called Argument unpacking token or Variable-length argument lists token. This is typed with three dots ( … ). This is safe to use for 5.6 or latest version. For example,

function sum( ...$numbers ) {
    $acc = 0;
    foreach ( $numbers as $n ) {
        $acc += $n;
    }
    return $acc;
}

echo sum( 1, 2, 3, 4 );

return output 10 as 1+2+3+4=10.