Message188686
| Author |
tim.peters |
| Recipients |
rhettinger, tim.peters |
| Date |
2013年05月07日.21:38:43 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1367962723.84.0.508821715816.issue17930@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
Each time thru, CWR searches for the rightmost position not containing the maximum index. But this is wholly determined by what happened the last time thru - search isn't really needed. Here's Python code:
def cwr2(iterable, r):
pool = tuple(iterable)
n = len(pool)
if not n and r:
return
indices = [0] * r
yield tuple(pool[i] for i in indices)
j = r-1 if n > 1 else -1
while j >= 0:
newval = indices[j] + 1
indices[j:] = [newval] * (r - j)
yield tuple(pool[i] for i in indices)
j = r-1 if newval < n-1 else j-1
There `j` is the rightmost position not containing the maximum index. A little thought suffices to see that the next j is either r-1 (if newval is not the maximum index) or j-1 (if newval is the maximum index: since the indices vector is non-decreasing, if indices[j] was r-2 then indices[j-1] is also at most r-2).
I don't much care if this goes in, but Raymond should find it amusing so assigning it to him ;-) |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2013年05月07日 21:38:43 | tim.peters | set | recipients:
+ tim.peters, rhettinger |
| 2013年05月07日 21:38:43 | tim.peters | set | messageid: <1367962723.84.0.508821715816.issue17930@psf.upfronthosting.co.za> |
| 2013年05月07日 21:38:43 | tim.peters | link | issue17930 messages |
| 2013年05月07日 21:38:43 | tim.peters | create |
|