Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Echoing different Arrangements
Old 03-27-2009, 11:55 AM Echoing different Arrangements
Skilled Talker

Posts: 68
Name: Avi Zolty
Location: Atlanta
Trades: 0
To past the time a friend and mine play a game in which we are choose a word (such as "ETHIOPIA") and using those letters, must construct as many smaller words as possible (example: "path", "Patio", "eat" and "ate").
We run into a problem though... Sometimes we want to check to see if there are any other words.
So I designed a script, which I do not know how to code... So I am asking, please, if someone could explain to me HOW to script it (is PHP even the right language-choice?) or just to direct me in to best path.
There are three main parts or "functions" I need to be done..

1) Take a value (from, for a text-input or such) and
a) calculate how many letters there are
b) give each letter a value ($letterone, $lettertwo, $letterthree...)
-- Although I'm not sure if "b" is possible.... or even needed, for the next step

2) Take a bunch of letter, and echo each one in every possible order. I was thinking about using something like "x=1; for(x=1,x<9,x++){echo x};" and then setting each number as a different letter... In the previous example would echo E, T, H, I, O, P, I, A... but then how would I be able to echo each one 8 times, and for each set of 8, one of each of the next letter (so like: EE, ET, EH, EI...etc)? I'm sure there is another way to do this

3) Take all the echoed words, or all the words on a page/document and compare them all to a dictionary, to see if they are real words (if they are, keep them... if they aren't delete them).

Any help at all would be much appreciated. Thank you!
__________________
"If you say something interesting, people will remember your name" ~ Anonymous


Please login or register to view this content. Registration is FREE
.asp <- Irony
Zoltar1992 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-27-2009, 02:13 PM Re: Echoing different Arrangements
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I don't know if there's a really smart way of doing this. Of course, you could try all possible words, but that would not be very effective. Quite the opposite in fact. For a 8 letter word, there would be a total of 8! combinations of 8-letter words, which is 40320 combinations (8! = 8*7*6*5*4*3*2*1). Then plus all the combinations when only using 7 letters, 6 letters and so on.

So the total amount of combinations would be
8 1-letter words, plus (8*7) 2-letter words, (8*7*6) 3-letter words etc.
Which equals 109 600 combinations in total

And just to make a point of how ineffective this is, and how fast the count of possible combinations increase.
8! = 40 320
10! = 3 628 800
15! = 1 307 674 368 000

Any way, here is a function which will try all possible combinations. You'll have to figure out how to validate a word against a dictionary your self :P
PHP Code:
function tryAll($current$letters) {
   for(
$i 0$i count($letters); $i++) {
      
$letter $letters[$i];
      
$str $current.$letter;

      
// Check if $str is a valid word, with a dictionary in a
      // database, file or what ever smart solution you may find

      // Make a copy of $letters, without the currently one selected
      // (since that letter is already used, you dont wanna use it again)
      
$copy $letters;
      unset(
$copy[$i]);
      
      
// run recursive call to same function
      
tryAll($str$copy);
   }
}

$word = array('e''t''h''i''o''p''i''a');
tryAll(''$word); 
I dont know if this will help understanding it, but what is does is basically add the first available letter and check if it's a valid word, add the next letter and check again etc. When all letters are used, it backs up to the last point where it had several letters left, and try the other one. Then it keeps doing that until it has backed all the way to the start and tried all the different letters.

It would go like this

e, et, eth, ethi, ethio, ethiop, ethiopi, ethiopia, ethiopa, ethiopai, ethioi, ethioip, ethioipa, ethioia, ethioiap, ethioa, ethioap, ethioapi, ethioai, ethioaip ...
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-27-2009, 04:16 PM Re: Echoing different Arrangements
Skilled Talker

Posts: 68
Name: Avi Zolty
Location: Atlanta
Trades: 0
Thank you very much.. Yes I understand how enormous the job would be... but what you've done has helped me a lot!!

Actualy... something seems to be going wrong.. I added "echo $str . "<br>";" so that it would echo it all out so that I could see... but it just echos
e
e
e
e
e
e
etc... why is that?

And why wouldn't your code
ethiopia
ethiopie
ethiopit
ethiopih
etc...
Sorry for all the theoretical questions, i'm just trying to better understand learn.
__________________
"If you say something interesting, people will remember your name" ~ Anonymous


Please login or register to view this content. Registration is FREE
.asp <- Irony

Last edited by Zoltar1992; 03-27-2009 at 04:58 PM..
Zoltar1992 is offline
Reply With Quote
View Public Profile
 
Old 03-27-2009, 08:37 PM Re: Echoing different Arrangements
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Sorry, my mistake. It's the unset() part that doesn't work. When you unset a array value, apparently it's not completely removed from the array, only the actual value. That is, after unsetting the 'e', the array still looks like this

PHP Code:
array(
   
=> ,
   
=> 't',
   
=> 'h',
   
// and so on ...

That's why it kept repeating the e. I solved it by making this change

PHP Code:
   // This
   
$copy $letters;
   unset(
$copy[$i]);

   
// Becomes this
   
$copy = array();
   foreach (
$letters as $key => $letter) {
      if (
$key != $i) {
         
$copy[] = $letter;
      }
   } 
I also tried to gererate all words and it took about 7-8 sec just to go through it and print the words. I imagine that checking each word in a dictionary would make it go a whole lot slower.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Echoing different Arrangements
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20214 seconds with 12 queries