mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -48,22 +48,22 @@ class GraphAdjList:
|
||||
"""Add vertex"""
|
||||
if vet in self.adj_list:
|
||||
return
|
||||
# Add a new linked list to the adjacency list
|
||||
# Add a new linked list in the adjacency list
|
||||
self.adj_list[vet] = []
|
||||
|
||||
def remove_vertex(self, vet: Vertex):
|
||||
"""Remove vertex"""
|
||||
if vet not in self.adj_list:
|
||||
raise ValueError()
|
||||
# Remove the vertex vet's corresponding linked list from the adjacency list
|
||||
# Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
self.adj_list.pop(vet)
|
||||
# Traverse other vertices' linked lists, removing all edges containing vet
|
||||
# Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
for vertex in self.adj_list:
|
||||
if vet in self.adj_list[vertex]:
|
||||
self.adj_list[vertex].remove(vet)
|
||||
|
||||
def print(self):
|
||||
"""Print the adjacency list"""
|
||||
"""Print adjacency list"""
|
||||
print("Adjacency list =")
|
||||
for vertex in self.adj_list:
|
||||
tmp = [v.val for v in self.adj_list[vertex]]
|
||||
@@ -87,13 +87,13 @@ if __name__ == "__main__":
|
||||
graph.print()
|
||||
|
||||
# Add edge
|
||||
# Vertices 1, 2 i.e., v[0], v[2]
|
||||
# Vertices 1, 2 are v[0], v[2]
|
||||
graph.add_edge(v[0], v[2])
|
||||
print("\nAfter adding edge 1-2, the graph is")
|
||||
graph.print()
|
||||
|
||||
# Remove edge
|
||||
# Vertices 1, 3 i.e., v[0], v[1]
|
||||
# Vertices 1, 3 are v[0], v[1]
|
||||
graph.remove_edge(v[0], v[1])
|
||||
print("\nAfter removing edge 1-3, the graph is")
|
||||
graph.print()
|
||||
@@ -105,7 +105,7 @@ if __name__ == "__main__":
|
||||
graph.print()
|
||||
|
||||
# Remove vertex
|
||||
# Vertex 3 i.e., v[1]
|
||||
# Vertex 3 is v[1]
|
||||
graph.remove_vertex(v[1])
|
||||
print("\nAfter removing vertex 3, the graph is")
|
||||
graph.print()
|
||||
|
||||
@@ -16,15 +16,15 @@ class GraphAdjMat:
|
||||
|
||||
def __init__(self, vertices: list[int], edges: list[list[int]]):
|
||||
"""Constructor"""
|
||||
# Vertex list, elements represent "vertex value", index represents "vertex index"
|
||||
# Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
self.vertices: list[int] = []
|
||||
# Adjacency matrix, row and column indices correspond to "vertex index"
|
||||
# Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
self.adj_mat: list[list[int]] = []
|
||||
# Add vertex
|
||||
# Add vertices
|
||||
for val in vertices:
|
||||
self.add_vertex(val)
|
||||
# Add edge
|
||||
# Edges elements represent vertex indices
|
||||
# Add edges
|
||||
# Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
for e in edges:
|
||||
self.add_edge(e[0], e[1])
|
||||
|
||||
@@ -35,7 +35,7 @@ class GraphAdjMat:
|
||||
def add_vertex(self, val: int):
|
||||
"""Add vertex"""
|
||||
n = self.size()
|
||||
# Add new vertex value to the vertex list
|
||||
# Add the value of the new vertex to the vertex list
|
||||
self.vertices.append(val)
|
||||
# Add a row to the adjacency matrix
|
||||
new_row = [0] * n
|
||||
@@ -48,27 +48,27 @@ class GraphAdjMat:
|
||||
"""Remove vertex"""
|
||||
if index >= self.size():
|
||||
raise IndexError()
|
||||
# Remove vertex at `index` from the vertex list
|
||||
# Remove the vertex at index from the vertex list
|
||||
self.vertices.pop(index)
|
||||
# Remove the row at `index` from the adjacency matrix
|
||||
# Remove the row at index from the adjacency matrix
|
||||
self.adj_mat.pop(index)
|
||||
# Remove the column at `index` from the adjacency matrix
|
||||
# Remove the column at index from the adjacency matrix
|
||||
for row in self.adj_mat:
|
||||
row.pop(index)
|
||||
|
||||
def add_edge(self, i: int, j: int):
|
||||
"""Add edge"""
|
||||
# Parameters i, j correspond to vertices element indices
|
||||
# Parameters i, j correspond to the vertices element indices
|
||||
# Handle index out of bounds and equality
|
||||
if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:
|
||||
raise IndexError()
|
||||
# In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., satisfies (i, j) == (j, i)
|
||||
# In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
|
||||
self.adj_mat[i][j] = 1
|
||||
self.adj_mat[j][i] = 1
|
||||
|
||||
def remove_edge(self, i: int, j: int):
|
||||
"""Remove edge"""
|
||||
# Parameters i, j correspond to vertices element indices
|
||||
# Parameters i, j correspond to the vertices element indices
|
||||
# Handle index out of bounds and equality
|
||||
if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:
|
||||
raise IndexError()
|
||||
@@ -85,7 +85,7 @@ class GraphAdjMat:
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize undirected graph
|
||||
# Edges elements represent vertex indices
|
||||
# Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
vertices = [1, 3, 2, 5, 4]
|
||||
edges = [[0, 1], [0, 3], [1, 2], [2, 3], [2, 4], [3, 4]]
|
||||
graph = GraphAdjMat(vertices, edges)
|
||||
@@ -93,13 +93,13 @@ if __name__ == "__main__":
|
||||
graph.print()
|
||||
|
||||
# Add edge
|
||||
# Indices of vertices 1, 2 are 0, 2 respectively
|
||||
# Vertices 1, 2 have indices 0, 2 respectively
|
||||
graph.add_edge(0, 2)
|
||||
print("\nAfter adding edge 1-2, the graph is")
|
||||
graph.print()
|
||||
|
||||
# Remove edge
|
||||
# Indices of vertices 1, 3 are 0, 1 respectively
|
||||
# Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.remove_edge(0, 1)
|
||||
print("\nAfter removing edge 1-3, the graph is")
|
||||
graph.print()
|
||||
@@ -110,7 +110,7 @@ if __name__ == "__main__":
|
||||
graph.print()
|
||||
|
||||
# Remove vertex
|
||||
# Index of vertex 3 is 1
|
||||
# Vertex 3 has index 1
|
||||
graph.remove_vertex(1)
|
||||
print("\nAfter removing vertex 3, the graph is")
|
||||
graph.print()
|
||||
|
||||
@@ -15,24 +15,24 @@ from graph_adjacency_list import GraphAdjList
|
||||
|
||||
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
|
||||
"""Breadth-first traversal"""
|
||||
# Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
|
||||
# Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
# Vertex traversal sequence
|
||||
res = []
|
||||
# Hash set, used to record visited vertices
|
||||
# Hash set for recording vertices that have been visited
|
||||
visited = set[Vertex]([start_vet])
|
||||
# Queue used to implement BFS
|
||||
que = deque[Vertex]([start_vet])
|
||||
# Starting from vertex vet, loop until all vertices are visited
|
||||
while len(que) > 0:
|
||||
vet = que.popleft() # Dequeue the vertex at the head of the queue
|
||||
vet = que.popleft() # Dequeue the front vertex
|
||||
res.append(vet) # Record visited vertex
|
||||
# Traverse all adjacent vertices of that vertex
|
||||
# Traverse all adjacent vertices of this vertex
|
||||
for adj_vet in graph.adj_list[vet]:
|
||||
if adj_vet in visited:
|
||||
continue # Skip already visited vertices
|
||||
continue # Skip vertices that have been visited
|
||||
que.append(adj_vet) # Only enqueue unvisited vertices
|
||||
visited.add(adj_vet) # Mark the vertex as visited
|
||||
# Return the vertex traversal sequence
|
||||
visited.add(adj_vet) # Mark this vertex as visited
|
||||
# Return vertex traversal sequence
|
||||
return res
|
||||
|
||||
|
||||
|
||||
@@ -15,21 +15,21 @@ from graph_adjacency_list import GraphAdjList
|
||||
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):
|
||||
"""Depth-first traversal helper function"""
|
||||
res.append(vet) # Record visited vertex
|
||||
visited.add(vet) # Mark the vertex as visited
|
||||
# Traverse all adjacent vertices of that vertex
|
||||
visited.add(vet) # Mark this vertex as visited
|
||||
# Traverse all adjacent vertices of this vertex
|
||||
for adjVet in graph.adj_list[vet]:
|
||||
if adjVet in visited:
|
||||
continue # Skip already visited vertices
|
||||
continue # Skip vertices that have been visited
|
||||
# Recursively visit adjacent vertices
|
||||
dfs(graph, visited, res, adjVet)
|
||||
|
||||
|
||||
def graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
|
||||
"""Depth-first traversal"""
|
||||
# Use adjacency list to represent the graph, to obtain all adjacent vertices of a specified vertex
|
||||
# Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
# Vertex traversal sequence
|
||||
res = []
|
||||
# Hash set, used to record visited vertices
|
||||
# Hash set for recording vertices that have been visited
|
||||
visited = set[Vertex]()
|
||||
dfs(graph, visited, res, start_vet)
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user