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 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
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@
* [Lucas Lehmer Primality Test](maths/lucas_lehmer_primality_test.py)
* [Lucas Series](maths/lucas_series.py)
* [Maclaurin Series](maths/maclaurin_series.py)
* [Manhattan Distance](maths/manhattan_distance.py)
* [Matrix Exponentiation](maths/matrix_exponentiation.py)
* [Max Sum Sliding Window](maths/max_sum_sliding_window.py)
* [Median Of Two Arrays](maths/median_of_two_arrays.py)
Expand Down Expand Up @@ -616,6 +617,7 @@
* [Sum Of Digits](maths/sum_of_digits.py)
* [Sum Of Geometric Progression](maths/sum_of_geometric_progression.py)
* [Sum Of Harmonic Series](maths/sum_of_harmonic_series.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 @@ -683,13 +685,15 @@
* [Casimir Effect](physics/casimir_effect.py)
* [Centripetal Force](physics/centripetal_force.py)
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Ideal Gas Law](physics/ideal_gas_law.py)
* [Kinetic Energy](physics/kinetic_energy.py)
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
* [Malus Law](physics/malus_law.py)
* [N Body Simulation](physics/n_body_simulation.py)
* [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py)
* [Newtons Second Law Of Motion](physics/newtons_second_law_of_motion.py)
* [Potential Energy](physics/potential_energy.py)
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
* [Sheer Stress](physics/sheer_stress.py)

## Project Euler
Expand Down Expand Up @@ -978,6 +982,7 @@
* [Not Gate](quantum/not_gate.py)
* [Q Full Adder](quantum/q_full_adder.py)
* [Quantum Entanglement](quantum/quantum_entanglement.py)
* [Quantum Teleportation](quantum/quantum_teleportation.py)
* [Ripple Adder Classic](quantum/ripple_adder_classic.py)
* [Single Qubit Measure](quantum/single_qubit_measure.py)
* [Superdense Coding](quantum/superdense_coding.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 [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()