diff --git a/codes/python/chapter_array_and_linkedlist/linked_list.py b/codes/python/chapter_array_and_linkedlist/linked_list.py index ae006db49..e51835d41 100644 --- a/codes/python/chapter_array_and_linkedlist/linked_list.py +++ b/codes/python/chapter_array_and_linkedlist/linked_list.py @@ -4,10 +4,11 @@ Created Time: 2022-11-25 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import ListNode, print_linked_list def insert(n0: ListNode, P: ListNode): diff --git a/codes/python/chapter_array_and_linkedlist/list.py b/codes/python/chapter_array_and_linkedlist/list.py index 468f2a08c..b9ad6d352 100644 --- a/codes/python/chapter_array_and_linkedlist/list.py +++ b/codes/python/chapter_array_and_linkedlist/list.py @@ -43,7 +43,7 @@ if __name__ == "__main__": for i in range(len(nums)): tmp.append(nums[i]) print(f"\n通过索引遍历列表得到 tmp = {tmp}") - + tmp.clear() for num in nums: tmp.append(num) diff --git a/codes/python/chapter_backtracking/permutations_i.py b/codes/python/chapter_backtracking/permutations_i.py index d5c29d6c2..6cca91010 100644 --- a/codes/python/chapter_backtracking/permutations_i.py +++ b/codes/python/chapter_backtracking/permutations_i.py @@ -4,11 +4,6 @@ Created Time: 2023-04-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp - -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * - def backtrack( state: list[int], choices: list[int], selected: list[bool], res: list[list[int]] diff --git a/codes/python/chapter_backtracking/permutations_ii.py b/codes/python/chapter_backtracking/permutations_ii.py index fac0b37e1..aaaffc68b 100644 --- a/codes/python/chapter_backtracking/permutations_ii.py +++ b/codes/python/chapter_backtracking/permutations_ii.py @@ -4,11 +4,6 @@ Created Time: 2023-04-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp - -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * - def backtrack( state: list[int], choices: list[int], selected: list[bool], res: list[list[int]] diff --git a/codes/python/chapter_backtracking/preorder_traversal_i_compact.py b/codes/python/chapter_backtracking/preorder_traversal_i_compact.py index f2d7177e9..48a2f160f 100644 --- a/codes/python/chapter_backtracking/preorder_traversal_i_compact.py +++ b/codes/python/chapter_backtracking/preorder_traversal_i_compact.py @@ -4,10 +4,11 @@ Created Time: 2023-04-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree, list_to_tree def pre_order(root: TreeNode): diff --git a/codes/python/chapter_backtracking/preorder_traversal_ii_compact.py b/codes/python/chapter_backtracking/preorder_traversal_ii_compact.py index 27bf00bab..9ef1ab6ff 100644 --- a/codes/python/chapter_backtracking/preorder_traversal_ii_compact.py +++ b/codes/python/chapter_backtracking/preorder_traversal_ii_compact.py @@ -4,10 +4,11 @@ Created Time: 2023-04-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree, list_to_tree def pre_order(root: TreeNode): diff --git a/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py b/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py index dac756c79..22c55a8a3 100644 --- a/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py +++ b/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py @@ -4,10 +4,11 @@ Created Time: 2023-04-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree, list_to_tree def pre_order(root: TreeNode): diff --git a/codes/python/chapter_backtracking/preorder_traversal_iii_template.py b/codes/python/chapter_backtracking/preorder_traversal_iii_template.py index ef5466a44..10a88c03a 100644 --- a/codes/python/chapter_backtracking/preorder_traversal_iii_template.py +++ b/codes/python/chapter_backtracking/preorder_traversal_iii_template.py @@ -4,10 +4,11 @@ Created Time: 2023-04-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree, list_to_tree def is_solution(state: list[TreeNode]) -> bool: diff --git a/codes/python/chapter_computational_complexity/space_complexity.py b/codes/python/chapter_computational_complexity/space_complexity.py index 6b4cb90b0..3080fd720 100644 --- a/codes/python/chapter_computational_complexity/space_complexity.py +++ b/codes/python/chapter_computational_complexity/space_complexity.py @@ -4,10 +4,11 @@ Created Time: 2022-11-25 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import ListNode, TreeNode, print_tree def function() -> int: diff --git a/codes/python/chapter_divide_and_conquer/build_tree.py b/codes/python/chapter_divide_and_conquer/build_tree.py index 970f1d4a4..56f43702c 100644 --- a/codes/python/chapter_divide_and_conquer/build_tree.py +++ b/codes/python/chapter_divide_and_conquer/build_tree.py @@ -4,10 +4,11 @@ Created Time: 2023-07-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree def dfs( diff --git a/codes/python/chapter_graph/graph_adjacency_list.py b/codes/python/chapter_graph/graph_adjacency_list.py index db8e534bc..3d44998d0 100644 --- a/codes/python/chapter_graph/graph_adjacency_list.py +++ b/codes/python/chapter_graph/graph_adjacency_list.py @@ -4,10 +4,11 @@ Created Time: 2023-02-23 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import Vertex, vals_to_vets class GraphAdjList: diff --git a/codes/python/chapter_graph/graph_adjacency_matrix.py b/codes/python/chapter_graph/graph_adjacency_matrix.py index a91ae21ff..7c6f44fc7 100644 --- a/codes/python/chapter_graph/graph_adjacency_matrix.py +++ b/codes/python/chapter_graph/graph_adjacency_matrix.py @@ -4,10 +4,11 @@ Created Time: 2023-02-23 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import Vertex, print_matrix class GraphAdjMat: diff --git a/codes/python/chapter_graph/graph_bfs.py b/codes/python/chapter_graph/graph_bfs.py index 24ad250cb..a44a849ba 100644 --- a/codes/python/chapter_graph/graph_bfs.py +++ b/codes/python/chapter_graph/graph_bfs.py @@ -4,10 +4,11 @@ Created Time: 2023-02-23 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import Vertex, vals_to_vets, vets_to_vals from collections import deque from graph_adjacency_list import GraphAdjList diff --git a/codes/python/chapter_graph/graph_dfs.py b/codes/python/chapter_graph/graph_dfs.py index 3acd74ac1..2d8de8c3b 100644 --- a/codes/python/chapter_graph/graph_dfs.py +++ b/codes/python/chapter_graph/graph_dfs.py @@ -4,10 +4,11 @@ Created Time: 2023-02-23 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import Vertex, vets_to_vals, vals_to_vets from graph_adjacency_list import GraphAdjList diff --git a/codes/python/chapter_hashing/built_in_hash.py b/codes/python/chapter_hashing/built_in_hash.py index bdb0b1e35..5ca5b4f8f 100644 --- a/codes/python/chapter_hashing/built_in_hash.py +++ b/codes/python/chapter_hashing/built_in_hash.py @@ -4,10 +4,11 @@ Created Time: 2023-06-15 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import ListNode """Driver Code""" if __name__ == "__main__": diff --git a/codes/python/chapter_hashing/hash_map.py b/codes/python/chapter_hashing/hash_map.py index b9f52e3d5..c1b1472c0 100644 --- a/codes/python/chapter_hashing/hash_map.py +++ b/codes/python/chapter_hashing/hash_map.py @@ -4,10 +4,11 @@ Created Time: 2022-12-14 Author: msk397 (machangxinq@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import print_dict """Driver Code""" if __name__ == "__main__": diff --git a/codes/python/chapter_hashing/hash_map_chaining.py b/codes/python/chapter_hashing/hash_map_chaining.py index 1358650bb..964de79e9 100644 --- a/codes/python/chapter_hashing/hash_map_chaining.py +++ b/codes/python/chapter_hashing/hash_map_chaining.py @@ -4,9 +4,10 @@ Created Time: 2023-06-13 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) +sys.path.append(str(Path(__file__).parent.parent)) from chapter_hashing.array_hash_map import Pair diff --git a/codes/python/chapter_hashing/hash_map_open_addressing.py b/codes/python/chapter_hashing/hash_map_open_addressing.py index 475859038..31d46e3d4 100644 --- a/codes/python/chapter_hashing/hash_map_open_addressing.py +++ b/codes/python/chapter_hashing/hash_map_open_addressing.py @@ -4,9 +4,10 @@ Created Time: 2023-06-13 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) +sys.path.append(str(Path(__file__).parent.parent)) from chapter_hashing.array_hash_map import Pair diff --git a/codes/python/chapter_heap/heap.py b/codes/python/chapter_heap/heap.py index 22a3362eb..cf179d8a6 100644 --- a/codes/python/chapter_heap/heap.py +++ b/codes/python/chapter_heap/heap.py @@ -4,10 +4,11 @@ Created Time: 2023-02-23 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import print_heap import heapq diff --git a/codes/python/chapter_heap/my_heap.py b/codes/python/chapter_heap/my_heap.py index 32f62b8de..2072a6cdd 100644 --- a/codes/python/chapter_heap/my_heap.py +++ b/codes/python/chapter_heap/my_heap.py @@ -4,10 +4,11 @@ Created Time: 2023-02-23 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import print_heap class MaxHeap: diff --git a/codes/python/chapter_heap/top_k.py b/codes/python/chapter_heap/top_k.py index 5c6cae716..fea1c8791 100644 --- a/codes/python/chapter_heap/top_k.py +++ b/codes/python/chapter_heap/top_k.py @@ -4,10 +4,11 @@ Created Time: 2023-06-10 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import print_heap import heapq diff --git a/codes/python/chapter_searching/binary_search_edge.py b/codes/python/chapter_searching/binary_search_edge.py index 55ae9b8aa..5e4589a42 100644 --- a/codes/python/chapter_searching/binary_search_edge.py +++ b/codes/python/chapter_searching/binary_search_edge.py @@ -4,9 +4,10 @@ Created Time: 2023-08-04 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) +sys.path.append(str(Path(__file__).parent.parent)) from binary_search_insertion import binary_search_insertion diff --git a/codes/python/chapter_searching/hashing_search.py b/codes/python/chapter_searching/hashing_search.py index cdcb99578..ae5c38b0a 100644 --- a/codes/python/chapter_searching/hashing_search.py +++ b/codes/python/chapter_searching/hashing_search.py @@ -4,10 +4,11 @@ Created Time: 2022-11-26 Author: timi (xisunyy@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import ListNode, list_to_linked_list def hashing_search_array(hmap: dict[int, int], target: int) -> int: diff --git a/codes/python/chapter_searching/linear_search.py b/codes/python/chapter_searching/linear_search.py index 2ddb75a45..fb449e7bc 100644 --- a/codes/python/chapter_searching/linear_search.py +++ b/codes/python/chapter_searching/linear_search.py @@ -4,10 +4,11 @@ Created Time: 2022-11-26 Author: timi (xisunyy@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import ListNode, list_to_linked_list def linear_search_array(nums: list[int], target: int) -> int: diff --git a/codes/python/chapter_stack_and_queue/linkedlist_queue.py b/codes/python/chapter_stack_and_queue/linkedlist_queue.py index 6f096133c..42f227c1e 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_queue.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_queue.py @@ -4,10 +4,11 @@ Created Time: 2022-12-01 Author: Peng Chen (pengchzn@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import ListNode class LinkedListQueue: diff --git a/codes/python/chapter_stack_and_queue/linkedlist_stack.py b/codes/python/chapter_stack_and_queue/linkedlist_stack.py index ceb12c804..51ef7868f 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -4,10 +4,11 @@ Created Time: 2022-11-29 Author: Peng Chen (pengchzn@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import ListNode class LinkedListStack: diff --git a/codes/python/chapter_tree/array_binary_tree.py b/codes/python/chapter_tree/array_binary_tree.py index 8b2a465e9..0073a51aa 100644 --- a/codes/python/chapter_tree/array_binary_tree.py +++ b/codes/python/chapter_tree/array_binary_tree.py @@ -4,10 +4,11 @@ Created Time: 2023-07-19 Author: Krahets (krahets@163.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, list_to_tree, print_tree class ArrayBinaryTree: diff --git a/codes/python/chapter_tree/avl_tree.py b/codes/python/chapter_tree/avl_tree.py index 51932c453..6f477c1ff 100644 --- a/codes/python/chapter_tree/avl_tree.py +++ b/codes/python/chapter_tree/avl_tree.py @@ -4,10 +4,11 @@ Created Time: 2022-12-20 Author: a16su (lpluls001@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree class AVLTree: diff --git a/codes/python/chapter_tree/binary_search_tree.py b/codes/python/chapter_tree/binary_search_tree.py index 20ac6a824..d339bf8f3 100644 --- a/codes/python/chapter_tree/binary_search_tree.py +++ b/codes/python/chapter_tree/binary_search_tree.py @@ -4,10 +4,11 @@ Created Time: 2022-12-20 Author: a16su (lpluls001@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree class BinarySearchTree: diff --git a/codes/python/chapter_tree/binary_tree.py b/codes/python/chapter_tree/binary_tree.py index 3539432de..a007e419b 100644 --- a/codes/python/chapter_tree/binary_tree.py +++ b/codes/python/chapter_tree/binary_tree.py @@ -4,10 +4,11 @@ Created Time: 2022-12-20 Author: a16su (lpluls001@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, print_tree """Driver Code""" diff --git a/codes/python/chapter_tree/binary_tree_bfs.py b/codes/python/chapter_tree/binary_tree_bfs.py index e6c78c94b..d75b1f684 100644 --- a/codes/python/chapter_tree/binary_tree_bfs.py +++ b/codes/python/chapter_tree/binary_tree_bfs.py @@ -4,10 +4,11 @@ Created Time: 2022-12-20 Author: a16su (lpluls001@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, list_to_tree, print_tree from collections import deque diff --git a/codes/python/chapter_tree/binary_tree_dfs.py b/codes/python/chapter_tree/binary_tree_dfs.py index 3bcef854b..9b3488ad0 100644 --- a/codes/python/chapter_tree/binary_tree_dfs.py +++ b/codes/python/chapter_tree/binary_tree_dfs.py @@ -4,10 +4,11 @@ Created Time: 2022-12-20 Author: a16su (lpluls001@gmail.com) """ -import sys, os.path as osp +import sys +from pathlib import Path -sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) -from modules import * +sys.path.append(str(Path(__file__).parent.parent)) +from modules import TreeNode, list_to_tree, print_tree def pre_order(root: TreeNode | None):