Files
Python/maths/sumset.py
Satyam Kumarandkumarsatyam9378-art 25a312ded5 Fix docstring copy-paste error in maths/sumset.py (#15052)
The docstring incorrectly referenced Sylvester's sequence (a copy-paste
error from maths/sylvester_sequence.py) and used param names that did
not match the actual function signature.

Fixes #15013

Co-authored-by: kumarsatyam9378-art <kumarsatyam9378@gmail.com>
2026-08-25 16:09:01 +02:00

38 lines
878 B
Python

"""
Calculates the SumSet of two sets of numbers (A and B)
Source:
https://en.wikipedia.org/wiki/Sumset
"""
def sumset(set_a: set, set_b: set) -> set:
"""
:param set_a: a set of numbers
:param set_b: a set of numbers
:return: the sumset of set_a and set_b (all pairwise sums a + b)
>>> sumset({1, 2, 3}, {4, 5, 6})
{5, 6, 7, 8, 9}
>>> sumset({1, 2, 3}, {4, 5, 6, 7})
{5, 6, 7, 8, 9, 10}
>>> sumset({1, 2, 3, 4}, 3)
Traceback (most recent call last):
...
AssertionError: The input value of [set_b=3] is not a set
"""
assert isinstance(set_a, set), f"The input value of [set_a={set_a}] is not a set"
assert isinstance(set_b, set), f"The input value of [set_b={set_b}] is not a set"
return {a + b for a in set_a for b in set_b}
if __name__ == "__main__":
from doctest import testmod
testmod()