On Wed, 30 Nov 2011 22:15:27 -0500, Roy Smith wrote: > I need to try a bunch of names in sequence until I find one that works > (definition of "works" is unimportant). The algorithm is: > > 1) Given a base name, "foo", first see if just plain "foo" works. > > 2) If not, try "foo-1", "foo-2", and so on > > 3) If you reach "foo-20", give up. > > What would you say if you saw this: > > for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: > > It generates the right sequence of strings. But, if you came upon that > code, would it make sense to you, or would you spend half the afternoon > trying to figure out what it did and the other half of the afternoon > ranting about the deranged lunatic who wrote it?
Nah, it's fine. Not exactly the clearest piece of code in the world, but hardly worth a rant. I'd be more likely to write that as: for suffix in [''] + ["-%d" % i for i in range(1, 21)]: or if I needed to do it more than once, as a generator: def suffixes(max=20): yield "" for i in range(1, max+1): yield "-%d" % i -- Steven -- http://mail.python.org/mailman/listinfo/python-list