brane Posted August 28, 2006 Share Posted August 28, 2006 Hi, I am a bit new to php and have only written a few programs with it. The current one I am working on will find all the permutations of a string and match them against a word list in sql , so I do not feel guilty cheating on the jumble games in the newspaper. I have been able to find the permutations of up to six letter strings by doing: foreach($array as $a ) foreach($array as $b ) foreach($array as $c ) foreach($array as $d ) foreach($array as $e ) foreach($array as $f ) $end_arr[]= $a. $b. $c. $d. $e. $f. ;My problem is that as soon as i add another line for the seventh character in the array to be handled as $g the program will not work. Is there a limitation as to how many nested foreach functions you can use? I am using PHP 5.0.4 on a 64 bit version of Suse 10. Also when the program goes to compile it does not show the error and line number if it fails. Is there a line I need to edit in the ini to turn on the debugging feature? This feature used to help me out a lot in PHP 4 but i have since upgraded. Also if anyone knows a better way to find the permutations of a string any help would be appreciated. Thanks in advance :) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 28, 2006 Share Posted August 28, 2006 What do you mean "the program will not work?" What is in the array "$array"?If I understand what you correctly, you are attempting to get all the combinations of the characters in the array. For a word of 7 characters, that over 800,000 "words" and you're probably running out of time or memory or both.Ken Quote Link to comment Share on other sites More sharing options...
brane Posted August 28, 2006 Author Share Posted August 28, 2006 I mean that the program will read the string, split the characters into the $array, find the size of the array, echo the size of the array, depending on the size of the array it will also echo which case it will execute as i am using a switch to diffferentiate between 5 6 and 7 letter strings, but after that for a seven letter string it will not do anything else. It works for 5 and six letters when it prints the ending array with a line break. I wouldn't think that the results would take up a gig of ram, but again I am pretty much a noob. Thanks for your input :)sorry i forgot to mention that each output will also be 7 characters long so I think that it should return 5040 results. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2006 Share Posted August 28, 2006 Your returns more the just the permutations of the 7 letter.EG with an array of a,b,c,d,e,f the first few results are [0] => aaaaaa [1] => aaaaab [2] => aaaaac [3] => aaaaad [4] => aaaaae [5] => aaaaaf Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 28, 2006 Share Posted August 28, 2006 [quote]so I think that it should return 5040 results.[/quote]Your math is a little off. 5040 is 7! (factorial 7 or 7 * 6 * 5 * 4 * 3 * 2 * 1). That will only happen if you use all seven characters for one position in the word, 6 for another, and so on until there is only one left. The way you are attempting it, you are using all seven characters in each position. so it's 7 ^ 7 or 823,543 combinations.I would rethink your algorithm.Ken Quote Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2006 Share Posted August 28, 2006 try[code]<?phpfunction perm($arr, $stub='') { global $perms; if (count($arr)==2) { $perms[] = $stub . $arr[0] . $arr[1]; $perms[] = $stub . $arr[1] . $arr[0]; } else { foreach ($arr as $c) { $b = $arr; unset ($b[array_search($c, $b)]); perm(array_values($b), $stub.$c); } }}$perms = array();$array = array('a','b','c','d','e','f','g');perm ($array);// view resultsecho '<pre>', print_r($perms, true), '</pre>';?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.