Itterating - "a" through "zzzz"

Reply

Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Itterating - "a" through "zzzz"

 
0
  #1
Sep 1st, 2009
Hi all. I'm attempting to output all of the possible combination from "a" to "zzzz" and I'm wondering if theres an easy way of doing that. As an example output would be ...

a
b
c
....
z
aa
ab
ac
....
az
ba
bb
...

and so on..

Is there a way to just have a binary or hex value, cast it as an ascii to get your first value, then increment it and cast it again.. and so on?

Any help would be much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Itterating - "a" through "zzzz"

 
1
  #2
Sep 2nd, 2009
This is how I would do it:
  1. $lower = true; //if you want lowercase letters
  2. $t = 4;
  3. $s = 1;
  4. while( $s <= $t ) {
  5. $i = ( $lower ? 97 : 65 );
  6. while( $i <= ( $lower ? 122 : 90 ) ) {
  7. echo str_repeat( chr( $i ),$s ) . '<br />';
  8. $i++;
  9. }
  10. $s++;
  11. }

That what you are talking about?
Last edited by kkeith29; Sep 2nd, 2009 at 2:00 am.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Itterating - "a" through "zzzz"

 
0
  #3
Sep 2nd, 2009
Originally Posted by kkeith29 View Post
This is how I would do it:
  1. $lower = true; //if you want lowercase letters
  2. $t = 4;
  3. $s = 1;
  4. while( $s <= $t ) {
  5. $i = ( $lower ? 97 : 65 );
  6. while( $i <= ( $lower ? 122 : 90 ) ) {
  7. echo str_repeat( chr( $i ),$s ) . '<br />';
  8. $i++;
  9. }
  10. $s++;
  11. }

That what you are talking about?

Well sorta.. It would basically generate all teh combinations of a set containing 'a' through 'z'. i.e. {a,b,c,...,z}. So it owuldnt just generate aa, bb, cc.. it would go through all the possible combinations with a as the first character then b as the second character and so on.. sorry if I was not specific enough in my initial post.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Itterating - "a" through "zzzz"

 
0
  #4
Sep 2nd, 2009
This code below will do the trick, but it's going to use a lot of memory to run this.
You are making 26 x 26 x 26 x 26 = 456976 combinations. Are you sure you want to generate all of these combinations? What are you trying to achieve this for?
  1. <?
  2. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  3. {
  4. for($b = 97; $b <= 122; $b++)
  5. {
  6. for($c = 97; $c <= 122; $c++)
  7. {
  8. for($d = 97; $d <= 122; $d++)
  9. {
  10. echo chr($a).chr($b).chr($c).chr($d)."<br/>";
  11. }
  12. }
  13. }
  14. }
  15. ?>
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Itterating - "a" through "zzzz"

 
0
  #5
Sep 2nd, 2009
In fact, I realise that the above code will not produce your single, double and triple character strings.
There are more elegant ways of doing it, but I am not able to put the time into devising one at the moment. One way would be just to repeat the code like so:
  1. <?
  2. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  3. {
  4. echo chr($a)."<br/>";
  5. }
  6.  
  7.  
  8.  
  9. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  10. {
  11. for($b = 97; $b <= 122; $b++)
  12. {
  13. echo chr($a).chr($b)."<br/>";
  14. }
  15. }
  16.  
  17.  
  18.  
  19. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  20. {
  21. for($b = 97; $b <= 122; $b++)
  22. {
  23. for($c = 97; $c <= 122; $c++)
  24. {
  25. echo chr($a).chr($b).chr($c)."<br/>";
  26. }
  27. }
  28. }
  29.  
  30.  
  31. for($a = 97; $a <= 122; $a++) /* 97 to 122 are the relevant ascii numbers */
  32. {
  33. for($b = 97; $b <= 122; $b++)
  34. {
  35. for($c = 97; $c <= 122; $c++)
  36. {
  37. for($d = 97; $d <= 122; $d++)
  38. {
  39. echo chr($a).chr($b).chr($c).chr($d)."<br/>";
  40. }
  41. }
  42. }
  43. }
  44. ?>
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Itterating - "a" through "zzzz"

 
0
  #6
Sep 16th, 2009
Originally Posted by SimonMayer View Post
This code below will do the trick, but it's going to use a lot of memory to run this.
You are making 26 x 26 x 26 x 26 = 456976 combinations. Are you sure you want to generate all of these combinations? What are you trying to achieve this for?
Thanks so much. Thats exactly what I was looking for. I am sorta unfamiliar with PHPs type casting syntax. I'm trying to stress test a script i'm preparing and just thought this would be a cool way to do it as well as a way to test myself and learn more.

Thanks again!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC