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:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions
@@ -0,0 +1,121 @@
/**
* File: graph_adjacency_list.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_graph
import utils.Vertex
/* Undirected graph class based on adjacency list */
class GraphAdjList(edges: Array<Array<Vertex?>>) {
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
val adjList = HashMap<Vertex, MutableList<Vertex>>()
/* Constructor */
init {
// Add all vertices and edges
for (edge in edges) {
addVertex(edge[0]!!)
addVertex(edge[1]!!)
addEdge(edge[0]!!, edge[1]!!)
}
}
/* Get the number of vertices */
fun size(): Int {
return adjList.size
}
/* Add edge */
fun addEdge(vet1: Vertex, vet2: Vertex) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw IllegalArgumentException()
// Add edge vet1 - vet2
adjList[vet1]?.add(vet2)
adjList[vet2]?.add(vet1)
}
/* Remove edge */
fun removeEdge(vet1: Vertex, vet2: Vertex) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw IllegalArgumentException()
// Remove edge vet1 - vet2
adjList[vet1]?.remove(vet2)
adjList[vet2]?.remove(vet1)
}
/* Add vertex */
fun addVertex(vet: Vertex) {
if (adjList.containsKey(vet))
return
// Add a new linked list in the adjacency list
adjList[vet] = mutableListOf()
}
/* Remove vertex */
fun removeVertex(vet: Vertex) {
if (!adjList.containsKey(vet))
throw IllegalArgumentException()
// Remove the linked list corresponding to vertex vet in the adjacency list
adjList.remove(vet)
// Traverse the linked lists of other vertices and remove all edges containing vet
for (list in adjList.values) {
list.remove(vet)
}
}
/* Print adjacency list */
fun print() {
println("Adjacency list =")
for (pair in adjList.entries) {
val tmp = mutableListOf<Int>()
for (vertex in pair.value) {
tmp.add(vertex._val)
}
println("${pair.key._val}: $tmp,")
}
}
}
/* Driver Code */
fun main() {
/* Add edge */
val v = Vertex.valsToVets(intArrayOf(1, 3, 2, 5, 4))
val edges = arrayOf(
arrayOf(v[0], v[1]),
arrayOf(v[0], v[3]),
arrayOf(v[1], v[2]),
arrayOf(v[2], v[3]),
arrayOf(v[2], v[4]),
arrayOf(v[3], v[4])
)
val graph = GraphAdjList(edges)
println("\nAfter initialization, graph is")
graph.print()
/* Add edge */
// Vertices 1, 3 are v[0], v[1]
graph.addEdge(v[0]!!, v[2]!!)
println("\nAfter adding edge 1-2, graph is")
graph.print()
/* Remove edge */
// Vertex 3 is v[1]
graph.removeEdge(v[0]!!, v[1]!!)
println("\nAfter removing edge 1-3, graph is")
graph.print()
/* Add vertex */
val v5 = Vertex(6)
graph.addVertex(v5)
println("\nAfter adding vertex 6, graph is")
graph.print()
/* Remove vertex */
// Vertex 3 is v[1]
graph.removeVertex(v[1]!!)
println("\nAfter removing vertex 3, graph is")
graph.print()
}
@@ -0,0 +1,134 @@
/**
* File: graph_adjacency_matrix.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_graph
import utils.printMatrix
/* Undirected graph class based on adjacency matrix */
class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
val vertices = mutableListOf<Int>() // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
val adjMat = mutableListOf<MutableList<Int>>() // Adjacency matrix, where the row and column indices correspond to the "vertex index"
/* Constructor */
init {
// Add vertex
for (vertex in vertices) {
addVertex(vertex)
}
// Add edge
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
for (edge in edges) {
addEdge(edge[0], edge[1])
}
}
/* Get the number of vertices */
fun size(): Int {
return vertices.size
}
/* Add vertex */
fun addVertex(_val: Int) {
val n = size()
// Add the value of the new vertex to the vertex list
vertices.add(_val)
// Add a row to the adjacency matrix
val newRow = mutableListOf<Int>()
for (j in 0..<n) {
newRow.add(0)
}
adjMat.add(newRow)
// Add a column to the adjacency matrix
for (row in adjMat) {
row.add(0)
}
}
/* Remove vertex */
fun removeVertex(index: Int) {
if (index >= size())
throw IndexOutOfBoundsException()
// Remove the vertex at index from the vertex list
vertices.removeAt(index)
// Remove the row at index from the adjacency matrix
adjMat.removeAt(index)
// Remove the column at index from the adjacency matrix
for (row in adjMat) {
row.removeAt(index)
}
}
/* Add edge */
// Parameters i, j correspond to the vertices element indices
fun addEdge(i: Int, j: Int) {
// Handle index out of bounds and equality
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw IndexOutOfBoundsException()
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
adjMat[i][j] = 1
adjMat[j][i] = 1
}
/* Remove edge */
// Parameters i, j correspond to the vertices element indices
fun removeEdge(i: Int, j: Int) {
// Handle index out of bounds and equality
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
throw IndexOutOfBoundsException()
adjMat[i][j] = 0
adjMat[j][i] = 0
}
/* Print adjacency matrix */
fun print() {
print("Vertex list = ")
println(vertices)
println("Adjacency matrix =")
printMatrix(adjMat)
}
}
/* Driver Code */
fun main() {
/* Add edge */
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
val vertices = intArrayOf(1, 3, 2, 5, 4)
val edges = arrayOf(
intArrayOf(0, 1),
intArrayOf(0, 3),
intArrayOf(1, 2),
intArrayOf(2, 3),
intArrayOf(2, 4),
intArrayOf(3, 4)
)
val graph = GraphAdjMat(vertices, edges)
println("\nAfter initialization, graph is")
graph.print()
/* Add edge */
// Add vertex
graph.addEdge(0, 2)
println("\nAfter adding edge 1-2, graph is")
graph.print()
/* Remove edge */
// Vertices 1, 3 have indices 0, 1 respectively
graph.removeEdge(0, 1)
println("\nAfter removing edge 1-3, graph is")
graph.print()
/* Add vertex */
graph.addVertex(6)
println("\nAfter adding vertex 6, graph is")
graph.print()
/* Remove vertex */
// Vertex 3 has index 1
graph.removeVertex(1)
println("\nAfter removing vertex 3, graph is")
graph.print()
}
@@ -0,0 +1,65 @@
/**
* File: graph_bfs.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_graph
import utils.Vertex
import java.util.*
/* Breadth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
fun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {
// Vertex traversal sequence
val res = mutableListOf<Vertex?>()
// Hash set for recording vertices that have been visited
val visited = HashSet<Vertex>()
visited.add(startVet)
// Queue used to implement BFS
val que = LinkedList<Vertex>()
que.offer(startVet)
// Starting from vertex vet, loop until all vertices are visited
while (!que.isEmpty()) {
val vet = que.poll() // Dequeue the front vertex
res.add(vet) // Record visited vertex
// Traverse all adjacent vertices of this vertex
for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet))
continue // Skip vertices that have been visited
que.offer(adjVet) // Only enqueue unvisited vertices
visited.add(adjVet) // Mark this vertex as visited
}
}
// Return vertex traversal sequence
return res
}
/* Driver Code */
fun main() {
/* Add edge */
val v = Vertex.valsToVets(intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
val edges = arrayOf(
arrayOf(v[0], v[1]),
arrayOf(v[0], v[3]),
arrayOf(v[1], v[2]),
arrayOf(v[1], v[4]),
arrayOf(v[2], v[5]),
arrayOf(v[3], v[4]),
arrayOf(v[3], v[6]),
arrayOf(v[4], v[5]),
arrayOf(v[4], v[7]),
arrayOf(v[5], v[8]),
arrayOf(v[6], v[7]),
arrayOf(v[7], v[8])
)
val graph = GraphAdjList(edges)
println("\nAfter initialization, graph is")
graph.print()
/* Breadth-first traversal */
val res = graphBFS(graph, v[0]!!)
println("\nBreadth-first traversal (BFS) vertex sequence is")
println(Vertex.vetsToVals(res))
}
@@ -0,0 +1,60 @@
/**
* File: graph_dfs.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_graph
import utils.Vertex
/* Depth-first traversal helper function */
fun dfs(
graph: GraphAdjList,
visited: MutableSet<Vertex?>,
res: MutableList<Vertex?>,
vet: Vertex?
) {
res.add(vet) // Record visited vertex
visited.add(vet) // Mark this vertex as visited
// Traverse all adjacent vertices of this vertex
for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet))
continue // Skip vertices that have been visited
// Recursively visit adjacent vertices
dfs(graph, visited, res, adjVet)
}
}
/* Depth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
fun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {
// Vertex traversal sequence
val res = mutableListOf<Vertex?>()
// Hash set for recording vertices that have been visited
val visited = HashSet<Vertex?>()
dfs(graph, visited, res, startVet)
return res
}
/* Driver Code */
fun main() {
/* Add edge */
val v = Vertex.valsToVets(intArrayOf(0, 1, 2, 3, 4, 5, 6))
val edges = arrayOf(
arrayOf(v[0], v[1]),
arrayOf(v[0], v[3]),
arrayOf(v[1], v[2]),
arrayOf(v[2], v[5]),
arrayOf(v[4], v[5]),
arrayOf(v[5], v[6])
)
val graph = GraphAdjList(edges)
println("\nAfter initialization, graph is")
graph.print()
/* Depth-first traversal */
val res = graphDFS(graph, v[0])
println("\nDepth-first traversal (DFS) vertex sequence is")
println(Vertex.vetsToVals(res))
}