Changeset 2094


Ignore:
Timestamp:
Jan 2, 2010 12:37:19 AM (10 years ago)
Author:
wehart
Message:

Adding a sequence() function, which is analogous to range()
for generating 1-indexed sequences.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/util.py

    r1610 r2094  
    7171def dot_product(*args, **kwds):
    7272    return summation(*args, **kwds)
     73
     74def sequence(*args):
     75    """
     76    sequence([start,] stop[, step]) -> list of integers
     77
     78    Return a list containing an arithmetic progression of integers.
     79    sequence(i, j) returns [i, i+1, i+2, ..., j]; start (!) defaults to 1.
     80    When step is given, it specifies the increment (or decrement).
     81    For example, sequence(4) returns [1, 2, 3, 4].
     82    """
     83    if len(args) == 0:
     84        raise TypeError, 'sequence expected at least 1 arguments, got 0'
     85    if len(args) > 3:
     86        raise TypeError, 'sequence expected at most 3 arguments, got %d' % len(args)
     87    if len(args) == 1:
     88        return range(1,args[0]+1)
     89    if len(args) == 2:
     90        return range(args[0],args[1]+1)
     91    return range(args[0],args[1]+1,args[2])
     92
     93
Note: See TracChangeset for help on using the changeset viewer.