mardi 25 septembre 2018

Trying to enumerate/cycle through the alphabet, numbers, and etc with python 2

I am attempting to cycle through the alphabet so it will print '0, 1, 2, 3' and 'a, b, c' and '!' and so on. After all the characters have been cycled through, I want to then have it go 'aa' 'ab' and 'a0' and so on. This is the working code I have so far:

alph = {
    0: '0',
    1: '1',
    2: '2',
    3: '3',
    4: '4',
    5: '5',
    6: '6',
    7: '7',
    8: '8',
    9: '9',
    10: 'a',
    11: 'b',
    12: 'c',
    13: 'd',
    14: 'e',
    15: 'f',
    16: 'g',
    17: 'h',
    18: 'i',
    19: 'j',
    20: 'l',
    21: 'm',
    22: 'n',
    23: 'o',
    24: 'p',
    25: 'q',
    26: 'r',
    27: 's',
    28: 't',
    29: 'u',
    30: 'v',
    31: 'w',
    32: 'x',
    33: 'y',
    34: 'z',
    35: '!'
}


def one(sweet):
    print sweet

def yeah():
    i = 0
    while 1==1:
        if divmod(i,36)[0] == 0:
            a = alph[divmod(i, 36)[1]]
            sweet = a
            one(sweet)
            i += 1

        elif divmod(i,36)[0] < 36:
            b = alph[divmod(i, 36)[1]]
            a = alph[divmod(i, 36)[0]]
            sweet = a + b
            one(sweet)
            i += 1

    return false

yeah()

This part works great! it will print out 'a' through '!!'. The part I'm struggling to wrap my head around is the third part:

        elif divmod(i,36)[0] < 36**2:
            c = alph[divmod(i, 36)[1]]
            b = alph[divmod((i//36), 36)[0]]
            a = alph[divmod(i, 36)[0]]
            sweet = a + b + c
            one(sweet)
            i += 1

This should print 'aaa' 'aab' and so on. I am not sure how to go about this. After doing this. I also realized that I would have to create an infinite amount of 'elif' statements, one for 'aaaa' another for 'aaaaa' and etc. What would be the best way to go about creating a function that could potentially go to infinity?

Aucun commentaire:

Enregistrer un commentaire