added sumset.py Fixes: #{6563} by happiestbee · Pull Request #6742 · TheAlgorithms/Python · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions DIRECTORY.md
37 changes: 37 additions & 0 deletions maths/sumset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Calculates the SumSet of two sets of numbers (A and B)

Source:
https://en.wikipedia.org/wiki/Sumset

Comment on lines +5 to +7

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Source:
https://en.wikipedia.org/wiki/Sumset
https://en.wikipedia.org/wiki/Sumset

"""


def sumset(set_a: set, set_b: set) -> set:
"""
:param first set: a set of numbers
:param second set: a set of numbers
:return: the nth number in Sylvester's sequence

>>> sumset({1, 2, 3}, {4, 5, 6})
{5, 6, 7, 8, 9}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

>>> sumset({1, 2, 3}, {4, 5, 6, 7})
{5, 6, 7, 8, 9, 10}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

>>> 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()