Created
November 6, 2018 00:40
-
-
Save dabiddo/8ec47653c4a5e8c66ad27826253878a7 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Created by PhpStorm. | |
* User: dabiddo | |
* Date: 11/5/2018 | |
* Time: 11:53 AM | |
* Create posible combinations of a secuence of numbers | |
*/ | |
function solution($N) { | |
$length=strlen($N)-1; | |
$arrNumbers = str_split($N); | |
$totalCombinations =0; | |
for($x=0;$x<=$length;$x++) | |
{ | |
$leading = $arrNumbers[$x]; | |
$newArray = $arrNumbers; | |
unset($newArray[$x]); | |
$newArray=array_values($newArray); | |
$totalCombinations += combine($leading,$newArray); | |
//var_dump($totalCombinations); | |
//array_merge($totalCombinations,combine($leading,$newArray)); | |
} | |
return $totalCombinations; | |
} | |
function combine($leading,$arrleft){ | |
$newLeng=count($arrleft); | |
$combiArray = []; | |
$rounds = 0; | |
while ($rounds!=$newLeng){ | |
$leadingNo=$leading; | |
foreach ($arrleft as $number){ | |
$leadingNo.=$number; | |
} | |
array_push($combiArray,$leadingNo); | |
if($rounds<$newLeng-1){ | |
$index = $arrleft[$rounds]; | |
$sindex = $arrleft[$rounds+1]; | |
$arrleft[$rounds]=$sindex; | |
$arrleft[$rounds+1]=$index; | |
} | |
$rounds++; | |
} | |
//var_dump($combiArray); | |
return count($combiArray); | |
} | |
echo(solution(1213)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment