Skip to content

added sumset.py Fixes: #{6563} #6742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
* [Sum Of Digits](maths/sum_of_digits.py)
* [Sum Of Geometric Progression](maths/sum_of_geometric_progression.py)
* [Sumset](maths/sumset.py)
* [Sylvester Sequence](maths/sylvester_sequence.py)
* [Test Prime Check](maths/test_prime_check.py)
* [Trapezoidal Rule](maths/trapezoidal_rule.py)
Expand Down Expand Up @@ -632,7 +633,7 @@

## Physics
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Lorenz Transformation Four Vector](physics/lorenz_transformation_four_vector.py)
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
* [N Body Simulation](physics/n_body_simulation.py)
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)

Expand Down
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
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
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
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
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 [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 [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()