Skip to content

Commit 134c7ab

Browse files
committed
Adjust array splitting to handle non-divisible arrays
1 parent bae33ac commit 134c7ab

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

graphs/array_to_graph.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.
23+
"""
24+
25+
# https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8
26+
27+
from typing import List, Dict
28+
29+
def divide_array_to_graph(arr: List[int], base: int) -> Dict[int, List[int]]:
30+
"""
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]}
47+
"""
48+
length = len(arr)
49+
parts = len(arr)//base # Desired number of parts
50+
part_size = length // parts # Size of each part
51+
52+
# Divide the array into smaller parts
53+
result = [arr[i * part_size: (i + 1) * part_size] for i in range(parts)]
54+
55+
# Insert the result into a dictionary with keys from 0 to 3
56+
result_dict = {i: result[i] for i in range(parts)}
57+
58+
return result_dict
59+
60+
if __name__ == "__main__":
61+
# Example usage
62+
array = [1, 2, 3, 4, 5, 6, 7, 8]
63+
print(divide_array_to_graph(array, 2))
64+
65+
import doctest
66+
doctest.testmod()

0 commit comments

Comments
 (0)