diff --git a/src/sage/combinat/set_partition.py b/src/sage/combinat/set_partition.py index a820cd9bd69..e9d78b853ea 100644 --- a/src/sage/combinat/set_partition.py +++ b/src/sage/combinat/set_partition.py @@ -2034,6 +2034,14 @@ class SetPartitions(UniqueRepresentation, Parent): sage: SetPartitions('aabcd').cardinality() # needs sage.libs.flint 15 + If the number of parts exceeds the length of the set, + an empty iterator is returned (:issue:`37643`):: + + sage: SetPartitions(range(3), 4).list() + [] + sage: SetPartitions('abcd', 6).list() + [] + REFERENCES: - :wikipedia:`Partition_of_a_set` @@ -2062,18 +2070,16 @@ def __classcall_private__(cls, s=None, part=None): pass s = frozenset(s) - if part is not None: + if part is None: + return SetPartitions_set(s) + else: if isinstance(part, (int, Integer)): - if len(s) < part: - raise ValueError("part must be <= len(set)") return SetPartitions_setn(s, part) else: part = sorted(part, reverse=True) if part not in Partitions(len(s)): raise ValueError("part must be an integer partition of %s" % len(s)) return SetPartitions_setparts(s, Partition(part)) - else: - return SetPartitions_set(s) def __contains__(self, x): """ diff --git a/src/sage/combinat/set_partition_iterator.pyx b/src/sage/combinat/set_partition_iterator.pyx index 66d2d829acc..93024e7666a 100644 --- a/src/sage/combinat/set_partition_iterator.pyx +++ b/src/sage/combinat/set_partition_iterator.pyx @@ -130,5 +130,7 @@ def set_partition_iterator_blocks(base_set, Py_ssize_t k): cdef Py_ssize_t n = len(base) cdef list a = list(range(n)) # TODO: implement _set_partition_block_gen as an iterative algorithm + if n < k: + return for P in _set_partition_block_gen(n, k, a): yield from_word( P, base)