From 134c7ab3d03d4afbf01c3f25c02ca2c4356da31c Mon Sep 17 00:00:00 2001 From: harigro Date: Fri, 17 Jan 2025 06:14:50 +0700 Subject: [PATCH 1/5] Adjust array splitting to handle non-divisible arrays --- graphs/array_to_graph.py | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 graphs/array_to_graph.py diff --git a/graphs/array_to_graph.py b/graphs/array_to_graph.py new file mode 100644 index 000000000000..32ee8caf6b1c --- /dev/null +++ b/graphs/array_to_graph.py @@ -0,0 +1,66 @@ +""" +MIT License + +Copyright (c) 2024 yuhari + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" + +# https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8 + +from typing import List, Dict + +def divide_array_to_graph(arr: List[int], base: int) -> Dict[int, List[int]]: + """ + Splits an array into smaller parts and returns them in a dictionary, simulating a graph + structure where each part of the array is a node connected to other parts. + + Args: + arr (List[int]): The array to be divided. + base (int): The divisor that determines the number of parts. + + Returns: + Dict[int, List[int]]: A dictionary representing the graph structure, + where each key is a node and the value is a list of connected nodes. + + Example: + >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=2) + {0: [1, 2], 1: [3, 4], 2: [5, 6], 3: [7, 8]} + >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=3) + {0: [1, 2, 3, 4], 1: [5, 6, 7, 8]} + """ + length = len(arr) + parts = len(arr)//base # Desired number of parts + part_size = length // parts # Size of each part + + # Divide the array into smaller parts + result = [arr[i * part_size: (i + 1) * part_size] for i in range(parts)] + + # Insert the result into a dictionary with keys from 0 to 3 + result_dict = {i: result[i] for i in range(parts)} + + return result_dict + +if __name__ == "__main__": + # Example usage + array = [1, 2, 3, 4, 5, 6, 7, 8] + print(divide_array_to_graph(array, 2)) + + import doctest + doctest.testmod() \ No newline at end of file From 2e216ab558da1ccbd3a71e640630e35954be7660 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:26:59 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/array_to_graph.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/graphs/array_to_graph.py b/graphs/array_to_graph.py index 32ee8caf6b1c..079c8e61502a 100644 --- a/graphs/array_to_graph.py +++ b/graphs/array_to_graph.py @@ -22,13 +22,14 @@ SOFTWARE. """ -# https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8 +# https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8 from typing import List, Dict + def divide_array_to_graph(arr: List[int], base: int) -> Dict[int, List[int]]: """ - Splits an array into smaller parts and returns them in a dictionary, simulating a graph + Splits an array into smaller parts and returns them in a dictionary, simulating a graph structure where each part of the array is a node connected to other parts. Args: @@ -36,7 +37,7 @@ def divide_array_to_graph(arr: List[int], base: int) -> Dict[int, List[int]]: base (int): The divisor that determines the number of parts. Returns: - Dict[int, List[int]]: A dictionary representing the graph structure, + Dict[int, List[int]]: A dictionary representing the graph structure, where each key is a node and the value is a list of connected nodes. Example: @@ -46,21 +47,23 @@ def divide_array_to_graph(arr: List[int], base: int) -> Dict[int, List[int]]: {0: [1, 2, 3, 4], 1: [5, 6, 7, 8]} """ length = len(arr) - parts = len(arr)//base # Desired number of parts + parts = len(arr) // base # Desired number of parts part_size = length // parts # Size of each part - + # Divide the array into smaller parts - result = [arr[i * part_size: (i + 1) * part_size] for i in range(parts)] - + result = [arr[i * part_size : (i + 1) * part_size] for i in range(parts)] + # Insert the result into a dictionary with keys from 0 to 3 result_dict = {i: result[i] for i in range(parts)} - + return result_dict + if __name__ == "__main__": # Example usage array = [1, 2, 3, 4, 5, 6, 7, 8] print(divide_array_to_graph(array, 2)) - + import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 64510e36825918148a2fb4e95d5e51804ecdbf9e Mon Sep 17 00:00:00 2001 From: harigro Date: Fri, 17 Jan 2025 15:32:58 +0700 Subject: [PATCH 3/5] Fix linting and pre-commit issues --- graphs/array_to_graph.py | 63 ++++++++++------------------------------ 1 file changed, 15 insertions(+), 48 deletions(-) diff --git a/graphs/array_to_graph.py b/graphs/array_to_graph.py index 32ee8caf6b1c..c90a45617817 100644 --- a/graphs/array_to_graph.py +++ b/graphs/array_to_graph.py @@ -1,66 +1,33 @@ """ -MIT License - -Copyright (c) 2024 yuhari - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +Reference: https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8 """ -# https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8 -from typing import List, Dict - -def divide_array_to_graph(arr: List[int], base: int) -> Dict[int, List[int]]: +def divide_array_to_graph(arr: list[int], base: int) -> dict[int, list[int]]: """ - Splits an array into smaller parts and returns them in a dictionary, simulating a graph - structure where each part of the array is a node connected to other parts. - - Args: - arr (List[int]): The array to be divided. - base (int): The divisor that determines the number of parts. - - Returns: - Dict[int, List[int]]: A dictionary representing the graph structure, - where each key is a node and the value is a list of connected nodes. - - Example: - >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=2) - {0: [1, 2], 1: [3, 4], 2: [5, 6], 3: [7, 8]} - >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=3) - {0: [1, 2, 3, 4], 1: [5, 6, 7, 8]} + >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=2) + {0: [1, 2], 1: [3, 4], 2: [5, 6], 3: [7, 8]} + >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=3) + {0: [1, 2, 3, 4], 1: [5, 6, 7, 8]} """ length = len(arr) - parts = len(arr)//base # Desired number of parts + parts = len(arr) // base # Desired number of parts part_size = length // parts # Size of each part - + # Divide the array into smaller parts - result = [arr[i * part_size: (i + 1) * part_size] for i in range(parts)] - + result = [arr[i * part_size : (i + 1) * part_size] for i in range(parts)] + # Insert the result into a dictionary with keys from 0 to 3 result_dict = {i: result[i] for i in range(parts)} - + return result_dict + if __name__ == "__main__": # Example usage array = [1, 2, 3, 4, 5, 6, 7, 8] print(divide_array_to_graph(array, 2)) - + import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 6c4ad2b31803ba56b29ad4661d97ecb5581b8ae9 Mon Sep 17 00:00:00 2001 From: harigro Date: Fri, 17 Jan 2025 15:52:58 +0700 Subject: [PATCH 4/5] adding program code references --- graphs/array_to_graph.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphs/array_to_graph.py b/graphs/array_to_graph.py index e22b5dac12cf..fec28d4865a5 100644 --- a/graphs/array_to_graph.py +++ b/graphs/array_to_graph.py @@ -22,7 +22,9 @@ SOFTWARE. """ -# https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8 +""" +Reference: https://gist.github.com/harigro/28df9ec639f74f217473f85065acf9d8 +""" def divide_array_to_graph(arr: list[int], base: int) -> dict[int, list[int]]: From 45a1b19e7d254af0db6d35f90745c7cf120e01c0 Mon Sep 17 00:00:00 2001 From: harigro Date: Fri, 24 Jan 2025 15:31:30 +0700 Subject: [PATCH 5/5] remove a variable with named langht --- graphs/array_to_graph.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/graphs/array_to_graph.py b/graphs/array_to_graph.py index fec28d4865a5..5ae215e038e9 100644 --- a/graphs/array_to_graph.py +++ b/graphs/array_to_graph.py @@ -34,9 +34,8 @@ def divide_array_to_graph(arr: list[int], base: int) -> dict[int, list[int]]: >>> divide_array_to_graph(arr=[1, 2, 3, 4, 5, 6, 7, 8], base=3) {0: [1, 2, 3, 4], 1: [5, 6, 7, 8]} """ - length = len(arr) parts = len(arr) // base # Desired number of parts - part_size = length // parts # Size of each part + part_size = len(arr) // parts # Size of each part # Divide the array into smaller parts result = [arr[i * part_size : (i + 1) * part_size] for i in range(parts)] @@ -50,7 +49,7 @@ def divide_array_to_graph(arr: list[int], base: int) -> dict[int, list[int]]: if __name__ == "__main__": # Example usage array = [1, 2, 3, 4, 5, 6, 7, 8] - print(divide_array_to_graph(array, 2)) + print(divide_array_to_graph(array, 3)) import doctest