Skip to content

Commit 64510e3

Browse files
committed
Fix linting and pre-commit issues
1 parent 134c7ab commit 64510e3

File tree

1 file changed

+15
-48
lines changed

1 file changed

+15
-48
lines changed

graphs/array_to_graph.py

+15-48
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,33 @@
11
"""
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
233
"""
244

25-
# https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8
265

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]]:
307
"""
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]}
4712
"""
4813
length = len(arr)
49-
parts = len(arr)//base # Desired number of parts
14+
parts = len(arr) // base # Desired number of parts
5015
part_size = length // parts # Size of each part
51-
16+
5217
# 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+
5520
# Insert the result into a dictionary with keys from 0 to 3
5621
result_dict = {i: result[i] for i in range(parts)}
57-
22+
5823
return result_dict
5924

25+
6026
if __name__ == "__main__":
6127
# Example usage
6228
array = [1, 2, 3, 4, 5, 6, 7, 8]
6329
print(divide_array_to_graph(array, 2))
64-
30+
6531
import doctest
66-
doctest.testmod()
32+
33+
doctest.testmod()

0 commit comments

Comments
 (0)