Please Criticize My Code

Ray ray_usenet at yahoo.com
Sat Aug 20 09:18:26 CEST 2005


Hello,

I just wrote a short script that generates look and say sequence. What
do you Python guys think of it? Too "Java-ish"? I can't shake off the
feeling that somebody may have done this with 2-3 lines of Python
magic. Heh.

# generator for sequence
def lookAndSaySequence(firstTerm, n):
    term = str(firstTerm)
    for i in xrange(n):
        yield term
        term = lookAndSay(term)

# the method that looks, and says
def lookAndSay(number):
    look_at_this = str(number)
    say_this = ['0', look_at_this[0]]
    for digit in look_at_this:
        if say_this[-1] != digit:
            say_this.extend(('1', digit))
        else:
            say_this[-2] = str(int(say_this[-2]) + 1)
    return "".join(say_this)
    
# run it!
print [x for x in lookAndSaySequence(1, 30)]

Thanks,
Ray




More information about the Python-list mailing list