[Tutor] Sequences of letter

spir ☣ denis.spir at gmail.com
Mon Apr 12 10:10:16 CEST 2010


On Mon, 12 Apr 2010 00:12:59 -0500
Juan Jose Del Toro <jdeltoro1973 at gmail.com> wrote:

> Dear List;
> 
> I have embarked myself into learning Python, I have no programming
> background other than some Shell scripts and modifying some programs in
> Basic and PHP, but now I want to be able to program.
> 
> I have been reading Alan Gauld's Tutor which has been very useful and I've
> also been watching Bucky Roberts (thenewboston) videos on youtube (I get
> lost there quite often but have also been helpful).
> 
> So I started with an exercise to do sequences of letters, I wan to write a
> program that could print out the suquence of letters from "aaa" all the way
> to "zzz"  like this:
> aaa
> aab
> aac
> ...
> zzx
> zzy
> zzz
> 
> So far this is what I have:
> letras =
> ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z"]

cinqLetras = "abcde"
# No need to enumerate manually to get a list of letters:
print list(cinqLetras)
# ==> ['a', 'b', 'c', 'd', 'e']

> letra1 = 0
> letra2 = 0
> letra3 = 0
> for i in letras:
>     for j in letras:
>         for k in letras:
>             print letras[letra1]+letras[letra2]+letras[letra3]
>             letra3=letra3+1
>     letra2=letra2+1
> letra1=letra1+1

The items are *letters*, not indexes. Should be called l1,l2,l3 and "glued" directly.

> It goes all the way to aaz and then it gives me this error
> Traceback (most recent call last):
>  File "/home/administrador/programacion/python/letras2.py", line 8, in
> <module>
> print letras[letra1]+letras[letra2]+letras[letra3]
> IndexError: list index out of range
> Script terminated.
> 
> Am I even in the right path?
> I guess I should look over creating a function or something like that
> because when I run it I can't even use my computer no memory left


No need to use a list to traverse the string letter per letter:

def trioDeLetras(letras):
   for l1 in letras:
      for l2 in letras:
         for l3 in letras:
            # (the comma avoid one line per letter trio)
            print l1+l2+l3,

trioDeLetras(cinqLetras)
# ==>
'''\
aaa aab aac aad aae aba abb abc abd abe aca acb acc acd ace ada adb adc add ade aea aeb aec aed aee baa bab bac
bad bae bba bbb bbc bbd bbe bca bcb bcc bcd bce bda bdb bdc bdd bde bea beb bec bed bee caa cab cac cad cae cba
cbb cbc cbd cbe cca ccb ccc ccd cce cda cdb cdc cdd cde cea ceb cec ced cee daa dab dac dad dae dba dbb dbc dbd
dbe dca dcb dcc dcd dce dda ddb ddc ddd dde dea deb dec ded dee eaa eab eac ead eae eba ebb ebc ebd ebe eca ecb
ecc ecd ece eda edb edc edd ede eea eeb eec eed eee
''' 

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com


More information about the Tutor mailing list