Skip to content

Commit 246b6d6

Browse files
committed
added examples in _unrank_helper method and corner cases test
1 parent 2f1e749 commit 246b6d6

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/sage/combinat/integer_vector.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,23 @@ def __contains__(self, x):
781781
return True
782782

783783
def _unrank_helper(self, x, rtn):
784+
"""
785+
return an element at rank ``x``.
786+
787+
EXAMPLES::
788+
789+
sage: i=IntegerVectors(k=5)
790+
sage: i._unrank_helper(10, [2,0,0,0,0])
791+
[1, 0, 0, 0, 1]
792+
793+
sage: i=IntegerVectors(n=7)
794+
sage: i._unrank_helper(100, [7,0,0,0])
795+
[2, 0, 0, 5]
796+
797+
sage: i=IntegerVectors(n=12, k=7)
798+
sage: i._unrank_helper(1000, [12,0,0,0,0,0,0])
799+
[5, 3, 1, 1, 1, 1, 0]
800+
"""
784801
ptr = 0
785802
while True:
786803
current_rank = self.rank(rtn)
@@ -1097,14 +1114,17 @@ def unrank(self, x):
10971114
[1, 0, 0, 0, 1]
10981115
sage: IntegerVectors(k=5).unrank(15)
10991116
[0, 0, 2, 0, 0]
1117+
sage: IntegerVectors(k=0).unrank(0)
1118+
[]
11001119
"""
11011120
if self.k == 0 and x != 0:
11021121
raise IndexError(f"Index {x} is out of range for the IntegerVector.")
1103-
n = 0
11041122
rtn = [0]*self.k
1123+
if self.k == 0 and x == 0:
1124+
return rtn
1125+
11051126
while self.rank(rtn) <= x:
1106-
n += 1
1107-
rtn[0] = n
1127+
rtn[0] += 1
11081128
rtn[0] -= 1
11091129

11101130
return IntegerVectors._unrank_helper(self, x, rtn)

0 commit comments

Comments
 (0)