Skip to content

Instantly share code, notes, and snippets.

@fre2mansur
Created July 27, 2019 08:40
Show Gist options
  • Save fre2mansur/243aab46d75b5f08ea6ba3dd2535f4f7 to your computer and use it in GitHub Desktop.
Save fre2mansur/243aab46d75b5f08ea6ba3dd2535f4f7 to your computer and use it in GitHub Desktop.
My answers
/*
Explanation
This is very simple i just compare the two arrays if the second array is in first array i make it to show true
*/
function isSubset($array1, $array2) {
echo (!array_diff($array2, $array1) ? "true" : "false");
}
//output
isSubset(array("A","B","C","D","E"), array("A","E","D")); // true
isSubset(array("A","B","C","D","E"), array("A","D","Z")); // false
isSubset(array("A","D","E"), array("A","A","D","E")); // true
/*
Explanation
Firts i need to determine the number of input array is the perfect root or not so i have create the isPerfectSquare($n) function.
n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both is a perferct square so the isFibonacci($n) function return 1, if its match.
Next, i created a function to determine the upcoming fibonacci number.
i loop reversly from given number to 0, and i brake the loop if any number is fibonacci, i stored that value to the array.
*/
$input = array(1,22,9,0);
var_dump(nextFibonacci($input)); //output [2,34,13,1]
function nextFibonacci($input) {
foreach($input as $i) {
while($i>=0) {
$store[] = $i;
if(isFibonacci($i))
break;
$i--;
}
$last = end($store);
if($last == 0) {
$output[] = 1;
} else {
$output[] = round($last/0.618);
}
}
return $output;
}
//Checking for square root
function isPerfectSquare($x)
{
$s = (int)(sqrt($x));
return ($s * $s == $x);
}
//Determining the fibonacci number
function isFibonacci($n)
{
return isPerfectSquare(5 * $n * $n + 4) || isPerfectSquare(5 * $n * $n - 4);
}
function createArrayOfFunctions(y) {
var arr = [];
for (let i = 0; i < y; i++) {
arr[i] = function(x) {
return x + i;
}
}
return arr;
}
//Problem is var i = 0; is not a global scope. so i have changed the var to let.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment