FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add itertools recipe for directly finding the n-th combination by rhettinger · Pull Request #5161 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Move range check to start of function. Expand tests to include negati…
…ve indicies.
  • Loading branch information
rhettinger committed Jan 13, 2018
commit da2746caf25d5f2571b8915cf86e1d5190e6ffd4
4 changes: 2 additions & 2 deletions Doc/library/itertools.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ which incur interpreter overhead.
'Equivalent to list(combinations(iterable, r))[index]'
pool = tuple(iterable)
n = len(pool)
if r < 0 or r > n:
raise ValueError
c = 1
k = min(r, n-r)
for i in range(1, k+1):
Expand All @@ -871,8 +873,6 @@ which incur interpreter overhead.
index += c
if index < 0 or index >= c:
raise IndexError
if r < 0 or r > n:
raise ValueError
result = []
while r:
c, n, r = c*r//n, n-1, r-1
Expand Down
13 changes: 8 additions & 5 deletions Lib/test/test_itertools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,8 @@ def test_permutations_sizeof(self):
... 'Equivalent to list(combinations(iterable, r))[index]'
... pool = tuple(iterable)
... n = len(pool)
... if r < 0 or r > n:
... raise ValueError
... c = 1
... k = min(r, n-r)
... for i in range(1, k+1):
Expand All @@ -2274,8 +2276,6 @@ def test_permutations_sizeof(self):
... index += c
... if index < 0 or index >= c:
... raise IndexError
... if r < 0 or r > n:
... raise ValueError
... result = []
... while r:
... c, n, r = c*r//n, n-1, r-1
Expand Down Expand Up @@ -2371,9 +2371,12 @@ def test_permutations_sizeof(self):

>>> population = 'ABCDEFGH'
>>> for r in range(len(population) + 1):
... for i, expected in enumerate(combinations(population, r)):
... actual = nth_combination(population, r, i)
... assert expected == actual
... seq = list(combinations(population, r))
... for i in range(len(seq)):
... assert nth_combination(population, r, i) == seq[i]
... for i in range(-len(seq), 0):
... assert nth_combination(population, r, i) == seq[i]


"""

Expand Down

Back | FazBrowse Home | New Git URL