Connecting Tech Pros Worldwide Forums | Help | Site Map

Help writing a loop

Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#1: May 21 '09
I'm trying to figure out how to write a loop using 2 sets of values 'a-z' and 'A-Z' so it would look like this:
aaaaaaaaaa
aaaaaaaaaA
aaaaaaaaAa
aaaaaaaAaa
and so on:
aaaaaaaaAA
aaaaaaaAAA
and then moving on to b:
bbbbbbbbbA

I can do it using numbers but don't know how to do it with letters:
Expand|Select|Wrap|Line Numbers
  1. for i in range(10):
  2.    for j in range(10):
  3.      for k in range(10):
  4.         print i,j,k
  5.  

dshimer's Avatar
Expert
 
Join Date: Dec 2006
Location: Central Ohio, USA
Posts: 135
#2: May 21 '09

re: Help writing a loop


This almost seems to simplistic but how about something like this.

Note that the range values and results are truncated and don't exactly match your requirements, just used the original example loop to show the general idea.

Expand|Select|Wrap|Line Numbers
  1. for i in range(65,70):
  2.     for j in range(65,70):
  3.         for k in range(65,70):
  4.             print chr(i),chr(j),chr(k)
  5. ...             
  6. A A A
  7. A A B
  8. A A C
  9. A A D
  10. A A E
  11. A B A
  12. A B B
  13. A B C
  14. A B D
  15.  
  16.  
Thekid's Avatar
Member
 
Join Date: Feb 2007
Posts: 110
#3: May 21 '09

re: Help writing a loop


Ah.... I see. So simply using the chr() function will do it. Thanks!

Edited:
When I use that but change the ascii numbers to (97,122) and use 5 'for' loops,
it will do:
aaaaa
aaaab
and so on but I would need the Caps (65,90) to run through it. This method will take a long time to run through all of the combos, is there another way to do this quicker? Thanks.
Reply