Skip to content

Commit c9e4268

Browse files
Fixed allocation_number (TheAlgorithms#2768)
* fixed allocation_number * fixed pre-commit * fixed line too long * fixed bug
1 parent 240f053 commit c9e4268

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

Diff for: maths/allocation_number.py

+23-32
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,45 @@
1+
"""
2+
In a multi-threaded download, this algorithm could be used to provide
3+
each worker thread with a block of non-overlapping bytes to download.
4+
For example:
5+
for i in allocation_list:
6+
requests.get(url,headers={'Range':f'bytes={i}'})
7+
"""
18
from __future__ import annotations
29

310

411
def allocation_num(number_of_bytes: int, partitions: int) -> list[str]:
512
"""
613
Divide a number of bytes into x partitions.
14+
:param number_of_bytes: the total of bytes.
15+
:param partitions: the number of partition need to be allocated.
16+
:return: list of bytes to be assigned to each worker thread
717
8-
In a multi-threaded download, this algorithm could be used to provide
9-
each worker thread with a block of non-overlapping bytes to download.
10-
For example:
11-
for i in allocation_list:
12-
requests.get(url,headers={'Range':f'bytes={i}'})
13-
14-
parameter
15-
------------
16-
: param number_of_bytes
17-
: param partitions
18-
19-
return
20-
------------
21-
: return: list of bytes to be assigned to each worker thread
22-
23-
Examples:
24-
------------
2518
>>> allocation_num(16647, 4)
26-
['0-4161', '4162-8322', '8323-12483', '12484-16647']
27-
>>> allocation_num(888, 888)
28-
Traceback (most recent call last):
29-
...
30-
ValueError: partitions can not >= number_of_bytes!
19+
['1-4161', '4162-8322', '8323-12483', '12484-16647']
20+
>>> allocation_num(50000, 5)
21+
['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000']
3122
>>> allocation_num(888, 999)
3223
Traceback (most recent call last):
3324
...
34-
ValueError: partitions can not >= number_of_bytes!
25+
ValueError: partitions can not > number_of_bytes!
3526
>>> allocation_num(888, -4)
3627
Traceback (most recent call last):
3728
...
3829
ValueError: partitions must be a positive number!
3930
"""
4031
if partitions <= 0:
4132
raise ValueError("partitions must be a positive number!")
42-
if partitions >= number_of_bytes:
43-
raise ValueError("partitions can not >= number_of_bytes!")
33+
if partitions > number_of_bytes:
34+
raise ValueError("partitions can not > number_of_bytes!")
4435
bytes_per_partition = number_of_bytes // partitions
45-
allocation_list = [f"0-{bytes_per_partition}"]
46-
for i in range(1, partitions - 1):
47-
length = f"{bytes_per_partition * i + 1}-{bytes_per_partition * (i + 1)}"
48-
allocation_list.append(length)
49-
allocation_list.append(
50-
f"{(bytes_per_partition * (partitions - 1)) + 1}-" f"{number_of_bytes}"
51-
)
36+
allocation_list = []
37+
for i in range(partitions):
38+
start_bytes = i * bytes_per_partition + 1
39+
end_bytes = (
40+
number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition
41+
)
42+
allocation_list.append(f"{start_bytes}-{end_bytes}")
5243
return allocation_list
5344

5445

0 commit comments

Comments
 (0)