Skip to content

Instantly share code, notes, and snippets.

@saintsGrad15
Last active October 28, 2021 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saintsGrad15/0deb9a6000fea3f2d3d34b3392731a23 to your computer and use it in GitHub Desktop.
Save saintsGrad15/0deb9a6000fea3f2d3d34b3392731a23 to your computer and use it in GitHub Desktop.
Return a list of tuples representing the inclusive lower and upper boundaries of each range of hexadecimal values from a namespace with 'number_of_bytes' bytes divided into 'number_of_sets' "equal" sets.
def find_hex_namespace_boundaries(number_of_bytes:int, number_of_sets:int = 2) -> Tuple[str]:
"""
Return a list of tuples representing the inclusive lower and upper boundaries
of each range of hexadecimal values from a namespace with 'number_of_bytes' bytes
divided into 'number_of_sets' "equal" sets.
:param number_of_bytes: The length of the hexadecimal namespace in bytes.
:param number_of_sets: The number of sets to divide the namespace into.
NOTE: The sets will be of as close to equal length as possible
while still covering the entirety of the namespace.
:return: A list of tuples representing the inclusive lower and upper boundaries
of each range of hexadecimal values from a namespace with 'number_of_bytes' bytes
divided into 'number_of_sets' "equal" sets.
"""
namespace_size = 16 ** number_of_bytes
set_size = fractions.Fraction(namespace_size, number_of_sets)
boundary_tuples = []
for i in range(number_of_sets):
lower_boundary = round(i * set_size)
upper_boundary = round((i + 1) * set_size - 1)
boundary_tuples.append((hex(lower_boundary), hex(upper_boundary)))
return boundary_tuples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment