//I have no knowledge about cache yet :(
Last active
July 27, 2019 09:08
-
-
Save fre2mansur/57314913e8274278032981d3337b37fa to your computer and use it in GitHub Desktop.
My answers [1-4]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /* | |
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 | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
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 and divide by fibonacci ratio. | |
*/ | |
$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); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Problem is var i = 0; is not a global scope. so i have changed the var to let. | |
function createArrayOfFunctions(y) { | |
var arr = []; | |
for (let i = 0; i < y; i++) { | |
arr[i] = function(x) { | |
return x + i; | |
} | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment