MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / allocation_num

Function allocation_num

maths/allocation_number.py:12–44  ·  view source on GitHub ↗

Divide a number of bytes into x partitions. :param number_of_bytes: the total of bytes. :param partitions: the number of partition need to be allocated. :return: list of bytes to be assigned to each worker thread >>> allocation_num(16647, 4) ['1-4161', '4162-8322', '8323-12

(number_of_bytes: int, partitions: int)

Source from the content-addressed store, hash-verified

10
11
12def allocation_num(number_of_bytes: int, partitions: int) -> list[str]:
13 """
14 Divide a number of bytes into x partitions.
15 :param number_of_bytes: the total of bytes.
16 :param partitions: the number of partition need to be allocated.
17 :return: list of bytes to be assigned to each worker thread
18
19 >>> allocation_num(16647, 4)
20 ['1-4161', '4162-8322', '8323-12483', '12484-16647']
21 >>> allocation_num(50000, 5)
22 ['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000']
23 >>> allocation_num(888, 999)
24 Traceback (most recent call last):
25 ...
26 ValueError: partitions can not > number_of_bytes!
27 >>> allocation_num(888, -4)
28 Traceback (most recent call last):
29 ...
30 ValueError: partitions must be a positive number!
31 """
32 if partitions <= 0:
33 raise ValueError("partitions must be a positive number!")
34 if partitions > number_of_bytes:
35 raise ValueError("partitions can not > number_of_bytes!")
36 bytes_per_partition = number_of_bytes // partitions
37 allocation_list = []
38 for i in range(partitions):
39 start_bytes = i * bytes_per_partition + 1
40 end_bytes = (
41 number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition
42 )
43 allocation_list.append(f"{start_bytes}-{end_bytes}")
44 return allocation_list
45
46
47if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected