Bragging about Python

Paul McGuire ptmcg at austin.rr.com
Fri Jun 8 09:09:15 EDT 2007


On Jun 7, 6:15 pm, Steve Howell <showel... at yahoo.com> wrote:
> Programs like this were posted on this thread:
>
>
>
>
>
> >    def fib():
> >        generation, parent_rabbits, baby_rabbits = 1,
> > 1, 1
> >        while True:
> >            yield generation, baby_rabbits
> >            generation += 1
> >            parent_rabbits, baby_rabbits = \
> >                   baby_rabbits, parent_rabbits +
> > baby_rabbits
>
> >     for pair in fib():
> >         if pair[0] > 100:
> >             break
> >         print "Generation %d has %d (baby) rabbits."
> > % pair
>
> One goal behind the SimplePrograms page is to give
> people that are new to Python a *gentle* immersion
> into Python code.  I prefer simple:
>
>     parent_rabbits, baby_rabbits = (1, 1)
>     while baby_rabbits < 100:
>         print 'This generation has %d rabbits' %
> baby_rabbits
>         parent_rabbits, baby_rabbits = (baby_rabbits,
> parent_rabbits + baby_rabbits)
>
> Somebody commented in another reply that they'd prefer
> the variable names "a" and "b," but other than that, I
> think it's hard to simplify this.
>
> The problem of counting rabbits is not sufficiently
> rich to motivate a solution with generator functions,
> and "yield" statements are just gonna scare people
> away from the Python, unless they've had a chance to
> see simpler idioms first.
>
> I do think there's a place on the page for a good
> generators example, but it needs to solve a
> sufficiently complex problem that the use of
> generators actually simplifies the solution.
>
> So I'm throwing down the gauntlet--can somebody write
> a short program (maybe 10 to 20 lines) where you solve
> a problem more simply than a similar
> non-generator-using solution would solve it?  Maybe
> something like Eight Queens?
>
> -- Steve
>
> P.S.  FWIW the page does already include examples of
> generator expressions and the itertools module, but it
> does not yet show any code that actually implements a
> generator.  I would greatly welcome the addition of a
> good example.
>
> ___________________________________________________________________________­_________
> Yahoo! oneSearch: Finally, mobile search
> that gives answers, not web links.http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC- Hide quoted text -
>
> - Show quoted text -

Well, I misread your "gauntlet throwing", and implemented a brief 8-
queens using recursion and exceptions.  Perhaps this could serve as
the "before" to an "after" using generators. (I note that neither
recursion nor exceptions are covered in your examples.)  Also, in
googling about for other 8-queens solutions in Python, most are quite
complicated - this one uses only 24 lines, and works for boards of any
size.  I also think this line for printing out the board:

print "\n".join( "."*q+"Q"+"."*(BOARD_SIZE-q-1) for q in queens )

illustrates a couple of interesting idioms of Python (joining a list,
generator expression, "."*repetition to give "....").

Worthy of your page?

-- Paul


BOARD_SIZE = 8
def validate(queens):
    left = right = col = queens[-1]
    for r in reversed(queens[:-1]):
        left,right = left-1,right+1
        if r in (left, col, right):
            raise Exception

def add_queen(queens):
    for i in range(BOARD_SIZE):
        testQueens = queens+[i]
        try:
            validate(testQueens)
            if len(testQueens)==BOARD_SIZE:
                return testQueens
            else:
                return add_queen(testQueens)
        except:
            pass
    raise Exception

queens = add_queen([])
print queens
print "\n".join( "."*q+"Q"+"."*(BOARD_SIZE-q-1) for q in queens )





More information about the Python-list mailing list