PEP 276 Simple Iterator for ints
James_Althoff at i2.com
James_Althoff at i2.com
Wed Nov 14 01:23:26 CET 2001
David Eppstein wrote:
>As a possibly-related side note to this debate:
>
>I have been moving towards using Python-like pseudocode in my algorithms
>classes in place of the C/C++/Java-like pseudocode I was previously using;
>that is, using colons and indentation to mark blocks of code instead of
>curly braces, etc. As well as conciseness, Python offers some other
>advantages e.g. in the ability to write code with list comprehensions.
>The single biggest barrier for me to write actual working Python code is
>the range/xrange syntax: I just don't feel comfortable writing code like
>
> for i in range(n-1,-1,-1):
> for j in range(i+1,n+1):
> ...do something...
>
>because I don't expect my students (who are not required to learn Python)
>to understand from that syntax that the outer loop runs backwards from n-1
>to 0 and that the inner loop runs from i+1 to n.
>
>Instead I have to make up some non-Python syntax which is less formal but
>immediately clear:
>
> for i in [n-1, n-2, ... 0]:
> for j in [i+1, i+2, ... n]:
> ...do something...
>
>(actual example from <http://www.ics.uci.edu/~eppstein/260/011023/>).
Thanks for your interesting example. I agree that your notation "[n-1,
n-2, ... 0]" is a very nice way of showing a sequence of integers. I'm not
sure how one would turn this into programming language syntax that would be
as clear. I think your notation works really well for your pseudo-code.
Nice!
Not so much to defend PEP 276 but more for fun :-),
...since your example is mathematics-oriented ("Dynamic Programming and
Optimal Triangulation") and is meant to be a descriptive notation of an
algorithm for your (presumably) *math-oriented* students, it might be
interesting/amusing to note that PEP 276 would allow the following:
for i in [n-1-a for a in n]:
for j in [i+1+a for a in n-i]:
# do something
:-)
Jim
More information about the Python-list
mailing list