22
22
SOFTWARE.
23
23
"""
24
24
25
- # https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8
25
+ # https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8
26
26
27
27
from typing import List , Dict
28
28
29
+
29
30
def divide_array_to_graph (arr : List [int ], base : int ) -> Dict [int , List [int ]]:
30
31
"""
31
- Splits an array into smaller parts and returns them in a dictionary, simulating a graph
32
+ Splits an array into smaller parts and returns them in a dictionary, simulating a graph
32
33
structure where each part of the array is a node connected to other parts.
33
34
34
35
Args:
35
36
arr (List[int]): The array to be divided.
36
37
base (int): The divisor that determines the number of parts.
37
38
38
39
Returns:
39
- Dict[int, List[int]]: A dictionary representing the graph structure,
40
+ Dict[int, List[int]]: A dictionary representing the graph structure,
40
41
where each key is a node and the value is a list of connected nodes.
41
42
42
43
Example:
@@ -46,21 +47,23 @@ def divide_array_to_graph(arr: List[int], base: int) -> Dict[int, List[int]]:
46
47
{0: [1, 2, 3, 4], 1: [5, 6, 7, 8]}
47
48
"""
48
49
length = len (arr )
49
- parts = len (arr )// base # Desired number of parts
50
+ parts = len (arr ) // base # Desired number of parts
50
51
part_size = length // parts # Size of each part
51
-
52
+
52
53
# Divide the array into smaller parts
53
- result = [arr [i * part_size : (i + 1 ) * part_size ] for i in range (parts )]
54
-
54
+ result = [arr [i * part_size : (i + 1 ) * part_size ] for i in range (parts )]
55
+
55
56
# Insert the result into a dictionary with keys from 0 to 3
56
57
result_dict = {i : result [i ] for i in range (parts )}
57
-
58
+
58
59
return result_dict
59
60
61
+
60
62
if __name__ == "__main__" :
61
63
# Example usage
62
64
array = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
63
65
print (divide_array_to_graph (array , 2 ))
64
-
66
+
65
67
import doctest
66
- doctest .testmod ()
68
+
69
+ doctest .testmod ()
0 commit comments