def calculate(L, N, X):
n = min(L//X, N)
return n, L//n
编辑:
def spread(seq, N=None, X=1):
"""Yield successive subsequences of seq having at least X elements.
If N is specified, the number of subsequences yielded will not exceed N.
The first L % X subsequences yielded (where L = len(seq)) will be longer
by 1 than the remaining ones.
>>> list(spread('abcdefghij', 4, 3))
['abcd', 'efg', 'hij']
>>> list(spread('abcdefghijklmnopqrstuvwxyz', 4, 7))
['abcdefghi', 'jklmnopqr', 'stuvwxyz']
seq any object supporting len(...) and slice-indexing
N a positive integer (default: L)
X a positive integer not greater than L (default: 1)
"""
# All error-checking code omitted
L = len(seq) # length of seq
assert 0 < X <= L
if N is None: N = L
assert 0 < N
# A total of n subsequences will be yielded, the first r of which will
# have length x + 1, and the remaining ones will have length x.
# if we insist on using calculate()...
# n, x = calculate(L, N, X)
# r = L % n
# ...but this entails separate computations of L//n and L%n; may as well
# do both with a single divmod(L, n)
n = min(L//X, N)
x, r = divmod(L, n)
start = 0
stride = x + 1 # stride will revert to x when i == r
for i in range(n):
if i == r: stride = x
finish = start + stride
yield seq[start:finish]
start = finish
assert start == L