Re: Bragging about Python
Steve Howell wrote: > --- Szabolcs Nagy <[EMAIL PROTECTED]> wrote: >> >> actually i don't like when a tutorial uses over >> complicated cute names >> if the context is obvious (fibonacci) then we don't >> need to add >> 'parent_rabbits' and such identifiers > > I still prefer the use of "rabbits," but I don't mind > if people change that, as I understand the brevity argument. The rabbit naming scheme makes the code far more accessible to people who aren't as familiar with fibonacci. Really, this is about making Python accessible to more people, not the same people it's currently accessible to :) Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python (8 queens)
--- Paul McGuire <[EMAIL PROTECTED]> wrote: > > > > 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? > > > > 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.) Actually, there was a minor example with exceptions, but agreed re:recursion, and this is just such a classic problem, it belongs on the page. > 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 > ""). Agreed. > > Worthy of your page? > Definitely worthy. Go ahead and add it. FWIW I have been labelling programs by the number of lines of code. So this is program #24, which now creates a gap between #16 and #23. Also, I humbly suggest test_queens instead of testQueens. I don't want to reopen the PEP 8 debate, just a recommendation. I think PEP 8 would also suggest some white space around '+' and '=='. > > 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 ) > Very Pythonic solution IMHO. Nicely done. Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase. http://farechase.yahoo.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
On Jun 7, 6:15 pm, Steve Howell <[EMAIL PROTECTED]> 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 ) -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
--- Szabolcs Nagy <[EMAIL PROTECTED]> wrote: > > the title of a program shouldn't be part of the code > (or it should at > least start with #) > sorry i'm too lazy now to fix it, but imho it needs > nicer layout to be > used as 'bragging about python' site > Agreed. I just spruced up the layout a tiny bit, but it could still use improvement. http://wiki.python.org/moin/SimplePrograms > btw nice idea > a good example worth a 100 pages of documentation > Thanks, I agree. :) Shape Yahoo! in your own image. Join our Network Research Panel today! http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
Steve Howell wrote: > --- Georg Brandl <[EMAIL PROTECTED]> wrote: > > >> > > >> 15 small programs here: > > >> > > >> http://wiki.python.org/moin/SimplePrograms > > >> > > > > > > IMHO a few python goodies are missing there. > > > > "It's a Wiki." ;) > > > > Yes indeed. Please feel free to add to the page, or > make your own fork. See link above. the title of a program shouldn't be part of the code (or it should at least start with #) sorry i'm too lazy now to fix it, but imho it needs nicer layout to be used as 'bragging about python' site btw nice idea a good example worth a 100 pages of documentation -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
--- Georg Brandl <[EMAIL PROTECTED]> wrote: > >> > >> 15 small programs here: > >> > >> http://wiki.python.org/moin/SimplePrograms > >> > > > > IMHO a few python goodies are missing there. > > "It's a Wiki." ;) > Yes indeed. Please feel free to add to the page, or make your own fork. See link above. Bored stiff? Loosen up... Download and play hundreds of games for free on Yahoo! Games. http://games.yahoo.com/games/front -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
--- Szabolcs Nagy <[EMAIL PROTECTED]> wrote: > > actually i don't like when a tutorial uses over > complicated cute names > if the context is obvious (fibonacci) then we don't > need to add > 'parent_rabbits' and such identifiers I still prefer the use of "rabbits," but I don't mind if people change that, as I understand the brevity argument. Pinpoint customers who are looking for what you sell. http://searchmarketing.yahoo.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
Cameron Laird wrote: > In article <[EMAIL PROTECTED]>, > Mathias Panzenboeck <[EMAIL PROTECTED]> wrote: >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 > > as more appealing to non-Pythoneers. I'm still suspicious about > how they're going to react to itertools.islice(). Now, though, > I've begun to question my own sense of style ... actually i don't like when a tutorial uses over complicated cute names if the context is obvious (fibonacci) then we don't need to add 'parent_rabbits' and such identifiers eg i find more readable and clear the following: def fib(): a, b = 0, 1 while True: yield a a, b = b, a + b for (n, fn) in enumerate(fib()): if n > 100: break print "F[%d] = %d" % (n, fn) ymmv -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
Mathias Panzenboeck schrieb: > Steve Howell schrieb: >> --- "[EMAIL PROTECTED]" >> <[EMAIL PROTECTED]> wrote: >> >>> Is there a resource somewhere on the net that can be >>> used to quickly >>> and effectively show Python's strengths to >>> non-Python programmers? >>> Small examples that will make them go "Wow, that >>> _is_ neat"? >>> >> >> 15 small programs here: >> >> http://wiki.python.org/moin/SimplePrograms >> > > IMHO a few python goodies are missing there. "It's a Wiki." ;) Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out. -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
In article <[EMAIL PROTECTED]>, Mathias Panzenboeck <[EMAIL PROTECTED]> wrote: >Steve Howell schrieb: >> --- "[EMAIL PROTECTED]" >> <[EMAIL PROTECTED]> wrote: >> >>> Is there a resource somewhere on the net that can be >>> used to quickly >>> and effectively show Python's strengths to >>> non-Python programmers? >>> Small examples that will make them go "Wow, that >>> _is_ neat"? >>> >> >> 15 small programs here: >> >> http://wiki.python.org/moin/SimplePrograms >> > >IMHO a few python goodies are missing there. >e.g. generator functions: > ># generic fibonacci without upper bound: >def fib(): >parent_rabbits, baby_rabbits = 1, 1 >while True: >yield baby_rabbits >parent_rabbits, baby_rabbits = baby_rabbits, parent_rabbits + >baby_rabbits > > ># only calculate and print the first 100 fibonacci numbers: >from itertools import islice > >for baby_rabbits in islice(fib(),100): >print 'This generation has %d rabbits' % baby_rabbits Good point. This example intrigues me. When I saw the first couple of lines, I expected 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 as more appealing to non-Pythoneers. I'm still suspicious about how they're going to react to itertools.islice(). Now, though, I've begun to question my own sense of style ... -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
Steve Howell schrieb: > --- "[EMAIL PROTECTED]" > <[EMAIL PROTECTED]> wrote: > >> Is there a resource somewhere on the net that can be >> used to quickly >> and effectively show Python's strengths to >> non-Python programmers? >> Small examples that will make them go "Wow, that >> _is_ neat"? >> > > 15 small programs here: > > http://wiki.python.org/moin/SimplePrograms > IMHO a few python goodies are missing there. e.g. generator functions: # generic fibonacci without upper bound: def fib(): parent_rabbits, baby_rabbits = 1, 1 while True: yield baby_rabbits parent_rabbits, baby_rabbits = baby_rabbits, parent_rabbits + baby_rabbits # only calculate and print the first 100 fibonacci numbers: from itertools import islice for baby_rabbits in islice(fib(),100): print 'This generation has %d rabbits' % baby_rabbits -- http://mail.python.org/mailman/listinfo/python-list
Re: Bragging about Python
--- "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Is there a resource somewhere on the net that can be > used to quickly > and effectively show Python's strengths to > non-Python programmers? > Small examples that will make them go "Wow, that > _is_ neat"? > 15 small programs here: http://wiki.python.org/moin/SimplePrograms Need a vacation? Get great deals to amazing places on Yahoo! Travel. http://travel.yahoo.com/ -- http://mail.python.org/mailman/listinfo/python-list