1
1
"""
2
- MIT License
3
-
4
- Copyright (c) 2024 yuhari
5
-
6
- Permission is hereby granted, free of charge, to any person obtaining a copy
7
- of this software and associated documentation files (the "Software"), to deal
8
- in the Software without restriction, including without limitation the rights
9
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- copies of the Software, and to permit persons to whom the Software is
11
- furnished to do so, subject to the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included in all
14
- copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- SOFTWARE.
2
+ Reference: https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8
23
3
"""
24
4
25
- # https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8
26
5
27
- from typing import List , Dict
28
-
29
- def divide_array_to_graph (arr : List [int ], base : int ) -> Dict [int , List [int ]]:
6
+ def divide_array_to_graph (arr : list [int ], base : int ) -> dict [int , list [int ]]:
30
7
"""
31
- Splits an array into smaller parts and returns them in a dictionary, simulating a graph
32
- structure where each part of the array is a node connected to other parts.
33
-
34
- Args:
35
- arr (List[int]): The array to be divided.
36
- base (int): The divisor that determines the number of parts.
37
-
38
- Returns:
39
- Dict[int, List[int]]: A dictionary representing the graph structure,
40
- where each key is a node and the value is a list of connected nodes.
41
-
42
- Example:
43
- >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=2)
44
- {0: [1, 2], 1: [3, 4], 2: [5, 6], 3: [7, 8]}
45
- >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=3)
46
- {0: [1, 2, 3, 4], 1: [5, 6, 7, 8]}
8
+ >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=2)
9
+ {0: [1, 2], 1: [3, 4], 2: [5, 6], 3: [7, 8]}
10
+ >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=3)
11
+ {0: [1, 2, 3, 4], 1: [5, 6, 7, 8]}
47
12
"""
48
13
length = len (arr )
49
- parts = len (arr )// base # Desired number of parts
14
+ parts = len (arr ) // base # Desired number of parts
50
15
part_size = length // parts # Size of each part
51
-
16
+
52
17
# Divide the array into smaller parts
53
- result = [arr [i * part_size : (i + 1 ) * part_size ] for i in range (parts )]
54
-
18
+ result = [arr [i * part_size : (i + 1 ) * part_size ] for i in range (parts )]
19
+
55
20
# Insert the result into a dictionary with keys from 0 to 3
56
21
result_dict = {i : result [i ] for i in range (parts )}
57
-
22
+
58
23
return result_dict
59
24
25
+
60
26
if __name__ == "__main__" :
61
27
# Example usage
62
28
array = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
63
29
print (divide_array_to_graph (array , 2 ))
64
-
30
+
65
31
import doctest
66
- doctest .testmod ()
32
+
33
+ doctest .testmod ()
0 commit comments