All Possible Combinations of Letters
Let's say I have the letters...
A,D, and G
And O (It's special)
How can PHP make all possible combinations with these words by using all or as little amount of letters as possible? (But special letters must be included in each scramble)
For example PHP would go through those letters and get...
A-O
O-A
O-D-G
D-O-G
G-D-O
D-G-O
A-D-G-O
And on and on and on...
What would be the fastest way to do this and if it is a function return those in an array?
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Show the code you have already.
I would prebuild the intial arrays containing OA, OD, OG, OAD, OAG, ODG and OADG. For each of these, call a recursive function to display the possible results (or write it so it keeps looping, to avoid memory issues).
pritaeas
Posting Expert
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
Anyone have a good idea on how this function will work? I'm having a hard time starting with this...
1. How can I make these combinations in the shortest amount of time?
2. Also would making all of the combinations be the best way to see if any of the combo's match with over 20,000 words in an array?
3. One more thing, it will actually have 7 seven letters (instead of four) (and each letter can only be used one per combo), but the number of letters that must be included will change...
Example:
7 Letters {R,A,R,H,S,L,L}
Special Letters {O,A,D}
* Note: Multiple special letters cannot come in the same combination, therefore combos will be 8 characters or shorter.
* Note: One letter will not be a combo Minimum: 2 Characters
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
is this homework?
Nope, I'm working on a cool (complex) app!
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
If you have 7 and 3, then you'll have a lot more than 20.000 words ;)
Before we start, why don't you describe in text (pseudo code) how this should work.
pritaeas
Posting Expert
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
Here is how it should work...
Information:
- 20,000+ words stored in php array & MySQL table
- $_POST['yourletters'] seven letters player has in scrabble
- $_POST['boardletters'] 1 or more letters that have are available for use on board
Goal:
- Create legit words that contain letters from the $_POST['yourletters'] variable. The word must contain 1 letter from $_POST['boardletters'] (unless no board letters are given)
This is basically a Scrabble (The Game) cheat service.
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Not what I mean.
Write down in text the steps you would take, if you had to do this by hand. For example:
Sort the letters
For each letter
Combine with the fixed letter to make a two letter word
pritaeas
Posting Expert
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
That is the problem. I'm having a hard time figuring out this algorithm...
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
It's a nasty one and an intensive one. I've been trying to figure this one out, but I found a partial js solution here:
http://scriptar.com/JavaScript/permute.html
The basis is a fixed length permutator (non-repetitive element) and is QUICK.
I've tried to port it to php, with limited success. Will have another look.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
It's a nasty one and an intensive one. I've been trying to figure this one out, but I found a partial js solution here:
http://scriptar.com/JavaScript/permute.html
The basis is a fixed length permutator (non-repetitive element) and is QUICK.
I've tried to port it to php, with limited success. Will have another look.
You rock dude! This is perfect!
Instead of doing the app in PHP I can echo the php array into a javascript array and go from there.
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
This may be helpful: http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm
bye :)
That's even nicer for php! Nice one cereal.+1
Question - this is really intensive for the server - if a number of people are just tap, tap, tapping away - you could have a monster on your hands.
Using js, will place the load on the client. Just a thought.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
That's even nicer for php! Nice one cereal.+1
Question - this is really intensive for the server - if a number of people are just tap, tap, tapping away - you could have a monster on your hands.
Using js, will place the load on the client. Just a thought.
That's what I was thinking too...
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Thanks everyone for your help, but I'm still having trouble implanting this into an algorithm.
I can't figure out how to do what I've said above.
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
I was looking at producing a set of combinations and then creating permutations on them. The possible number of combinations is huge in itself. Wondering too about creating a dummy letter (say X) for the mandatory letter and then using the js equivalent of array_map() - .map? replacing with O, A, D. Whether this would be quicker than creating each set of arrays?
Total number of permutations for 7 letters, 1 of three mandatories, decreasing in size from 8 to 2, *I think* will give: 328776 answers for all 3 mandatories.
Do you really need that much?
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Let me show you guys the overall task. I'm sort of building a Scrabble Cheating website.
The user provides two values
1. Their 7 Letters
2. 1 or more letters available on the board.
My job is to simply put that information together and use it to make real words they can place on the board.
What do I have?
1. An English MySQL dictionary (that can potentually be loaded into a javascript or php array)
My best guess was to start by making all of these permutations and be able to see if they matched with English words.
Any better ideas?
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
I suppose you could do this, but it seems very intensive to me. There again, I've never tried to do something like that before.
BTW - I'm still messing with some JS solution. I've got it to the point of producing combos and then running permutations on those. However, it won't deal with duplicate letters. Nowhere near finished. My pet project for the week! By then, I reckon somebody else will have a neat solution. Why not ask a mod to move this to the JS forum? That's where all the JS experts live.
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080