Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac 18411: a bounded_number_of_tuples function
Browse files Browse the repository at this point in the history
  • Loading branch information
videlec committed Sep 12, 2015
1 parent 559f73b commit 635279f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/sage/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,26 @@ def random_sublist(X, s):
return [a for a in X if random.random() <= s]


def bounded_number_of_tuples(elements, repeat, bound):
r"""
Return at most ``bound`` number of ``repeat``-tuples of ``elements``.
TESTS::
sage: from sage.misc.misc import bounded_number_of_tuples
sage: l = bounded_number_of_tuples([0,1,2,3], 2, 3)
sage: l # random
[(0,3), (1,2), (3,4)]
sage: len(l)
3
"""
from itertools import product
tuples = product(elements, repeat=repeat)
if len(elements) ** repeat < bound:
return tuples
else:
from sage.misc.prandom import sample
return sample(list(tuples), bound)

def powerset(X):
r"""
Expand Down

0 comments on commit 635279f

Please sign in to comment.