Languages :: PHP :: Knowledge Base : How to get all phone number combinations of letters ? |
|||
| By: VGR |
Date: 18/03/2005 11:51:33 |
Points: 0 | Status: Answered Quality : Good |
|
Dated 07/05/2003 <?php // ----- Functions function Recursion($par,$prev,&$resu,&$nb) { // algorithm : // if length>0 treat first char in a loop, call for the rest // else memorize result and get out recursively if (strlen($par)>0) { switch ($par[0]) { case 0 : $chars=array('0'); break; case 1 : $chars=array('1'); break; case 2 : $chars=array('A','B','C'); break; case 3 : $chars=array('D','E','F'); break; case 4 : $chars=array('G','H','I'); break; // 'I' should be out (confusion with 1) case 5 : $chars=array('J','K','L'); break; case 6 : $chars=array('M','N','O'); break; // 'O' should be out (confusion with 0) case 7 : $chars=array('P','Q','R','S'); break; case 8 : $chars=array('T','U','V'); break; case 9 : $chars=array('W','X','Y','Z'); break; } // switch/case of foreach($chars as $char) { $newst=$prev.$char; Recursion(substr($par,1),$newst,$resu,$nb); } } else { // result found $resu[$nb]=$prev; // substr($resu[$nb-1],0,strlen($resu[$nb-1])-1); $nb++; } } // Recursion Procedure function DoIt($tel) { $resu=array(); $nb=0; Recursion($tel,"",$resu,$nb); return $resu; } // DoIt Array Function // ----- MAIN $numtel='4143'; $result=DoIt($numtel); // $result initialized in DoIt() $nbres=count($result); echo "number $numtel "; echo "$nbres combinations : "; for ($i=0;$i<$nbres;$i++) echo $result[$i].' '; ?> |
|||
| By: VGR | Date: 18/03/2005 11:52:09 | Type : Comment |
|
| The results : number 4143 27 combinations : G1GD G1GE G1GF G1HD G1HE G1HF G1ID G1IE G1IF H1GD H1GE H1GF H1HD H1HE H1HF H1ID H1IE H1IF I1GD I1GE I1GF I1HD I1HE I1HF I1ID I1IE I1IF |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








