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,100 @@
// File: graph_adjacency_list.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"strconv"
"strings"
. "github.com/krahets/hello-algo/pkg"
)
/* Undirected graph class based on adjacency list */
type graphAdjList struct {
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
adjList map[Vertex][]Vertex
}
/* Constructor */
func newGraphAdjList(edges [][]Vertex) *graphAdjList {
g := &graphAdjList{
adjList: make(map[Vertex][]Vertex),
}
// Add all vertices and edges
for _, edge := range edges {
g.addVertex(edge[0])
g.addVertex(edge[1])
g.addEdge(edge[0], edge[1])
}
return g
}
/* Get the number of vertices */
func (g *graphAdjList) size() int {
return len(g.adjList)
}
/* Add edge */
func (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {
_, ok1 := g.adjList[vet1]
_, ok2 := g.adjList[vet2]
if !ok1 || !ok2 || vet1 == vet2 {
panic("error")
}
// Add edge vet1 - vet2, add anonymous struct{},
g.adjList[vet1] = append(g.adjList[vet1], vet2)
g.adjList[vet2] = append(g.adjList[vet2], vet1)
}
/* Remove edge */
func (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {
_, ok1 := g.adjList[vet1]
_, ok2 := g.adjList[vet2]
if !ok1 || !ok2 || vet1 == vet2 {
panic("error")
}
// Remove edge vet1 - vet2
g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)
g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)
}
/* Add vertex */
func (g *graphAdjList) addVertex(vet Vertex) {
_, ok := g.adjList[vet]
if ok {
return
}
// Add a new linked list in the adjacency list
g.adjList[vet] = make([]Vertex, 0)
}
/* Remove vertex */
func (g *graphAdjList) removeVertex(vet Vertex) {
_, ok := g.adjList[vet]
if !ok {
panic("error")
}
// Remove the linked list corresponding to vertex vet in the adjacency list
delete(g.adjList, vet)
// Traverse the linked lists of other vertices and remove all edges containing vet
for v, list := range g.adjList {
g.adjList[v] = DeleteSliceElms(list, vet)
}
}
/* Print adjacency list */
func (g *graphAdjList) print() {
var builder strings.Builder
fmt.Printf("Adjacency list = \n")
for k, v := range g.adjList {
builder.WriteString("\t\t" + strconv.Itoa(k.Val) + ": ")
for _, vet := range v {
builder.WriteString(strconv.Itoa(vet.Val) + " ")
}
fmt.Println(builder.String())
builder.Reset()
}
}
@@ -0,0 +1,45 @@
// File: graph_adjacency_list_test.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestGraphAdjList(t *testing.T) {
/* Add edge */
v := ValsToVets([]int{1, 3, 2, 5, 4})
edges := [][]Vertex{{v[0], v[1]}, {v[0], v[3]}, {v[1], v[2]}, {v[2], v[3]}, {v[2], v[4]}, {v[3], v[4]}}
graph := newGraphAdjList(edges)
fmt.Println("After initialization, graph is:")
graph.print()
/* Add edge */
// Vertices 1, 3 are v[0], v[1]
graph.addEdge(v[0], v[2])
fmt.Println("\nAfter adding edge 1-2, graph is")
graph.print()
/* Remove edge */
// Vertex 3 is v[1]
graph.removeEdge(v[0], v[1])
fmt.Println("\nAfter removing edge 1-3, graph is")
graph.print()
/* Add vertex */
v5 := NewVertex(6)
graph.addVertex(v5)
fmt.Println("\nAfter adding vertex 6, graph is")
graph.print()
/* Remove vertex */
// Vertex 3 is v[1]
graph.removeVertex(v[1])
fmt.Println("\nAfter removing vertex 3, graph is")
graph.print()
}
@@ -0,0 +1,102 @@
// File: graph_adjacency_matrix.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import "fmt"
/* Undirected graph class based on adjacency matrix */
type graphAdjMat struct {
// Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
vertices []int
// Adjacency matrix, where the row and column indices correspond to the "vertex index"
adjMat [][]int
}
/* Constructor */
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
// Add vertex
n := len(vertices)
adjMat := make([][]int, n)
for i := range adjMat {
adjMat[i] = make([]int, n)
}
// Initialize graph
g := &graphAdjMat{
vertices: vertices,
adjMat: adjMat,
}
// Add edge
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
for i := range edges {
g.addEdge(edges[i][0], edges[i][1])
}
return g
}
/* Get the number of vertices */
func (g *graphAdjMat) size() int {
return len(g.vertices)
}
/* Add vertex */
func (g *graphAdjMat) addVertex(val int) {
n := g.size()
// Add the value of the new vertex to the vertex list
g.vertices = append(g.vertices, val)
// Add a row to the adjacency matrix
newRow := make([]int, n)
g.adjMat = append(g.adjMat, newRow)
// Add a column to the adjacency matrix
for i := range g.adjMat {
g.adjMat[i] = append(g.adjMat[i], 0)
}
}
/* Remove vertex */
func (g *graphAdjMat) removeVertex(index int) {
if index >= g.size() {
return
}
// Remove the vertex at index from the vertex list
g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)
// Remove the row at index from the adjacency matrix
g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)
// Remove the column at index from the adjacency matrix
for i := range g.adjMat {
g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)
}
}
/* Add edge */
// Parameters i, j correspond to the vertices element indices
func (g *graphAdjMat) addEdge(i, j int) {
// Handle index out of bounds and equality
if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {
fmt.Errorf("%s", "Index Out Of Bounds Exception")
}
// In an undirected graph, the adjacency matrix is symmetric about the main diagonal, i.e., (i, j) == (j, i)
g.adjMat[i][j] = 1
g.adjMat[j][i] = 1
}
/* Remove edge */
// Parameters i, j correspond to the vertices element indices
func (g *graphAdjMat) removeEdge(i, j int) {
// Handle index out of bounds and equality
if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {
fmt.Errorf("%s", "Index Out Of Bounds Exception")
}
g.adjMat[i][j] = 0
g.adjMat[j][i] = 0
}
/* Print adjacency matrix */
func (g *graphAdjMat) print() {
fmt.Printf("\tVertex list = %v\n", g.vertices)
fmt.Printf("\tAdjacency matrix = \n")
for i := range g.adjMat {
fmt.Printf("\t\t\t%v\n", g.adjMat[i])
}
}
@@ -0,0 +1,43 @@
// File: graph_adjacency_matrix_test.go
// Created Time: 2023-01-31
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
)
func TestGraphAdjMat(t *testing.T) {
/* Add edge */
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
vertices := []int{1, 3, 2, 5, 4}
edges := [][]int{{0, 1}, {1, 2}, {2, 3}, {0, 3}, {2, 4}, {3, 4}}
graph := newGraphAdjMat(vertices, edges)
fmt.Println("After initialization, graph is:")
graph.print()
/* Add edge */
// Add vertex
graph.addEdge(0, 2)
fmt.Println("After adding edge 1-2, graph is")
graph.print()
/* Remove edge */
// Vertices 1, 3 have indices 0, 1 respectively
graph.removeEdge(0, 1)
fmt.Println("After removing edge 1-3, graph is")
graph.print()
/* Add vertex */
graph.addVertex(6)
fmt.Println("After adding vertex 6, graph is")
graph.print()
/* Remove vertex */
// Vertex 3 has index 1
graph.removeVertex(1)
fmt.Println("After removing vertex 3, graph is")
graph.print()
}
+41
View File
@@ -0,0 +1,41 @@
// File: graph_bfs.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
. "github.com/krahets/hello-algo/pkg"
)
/* Breadth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
func graphBFS(g *graphAdjList, startVet Vertex) []Vertex {
// Vertex traversal sequence
res := make([]Vertex, 0)
// Hash set for recording vertices that have been visited
visited := make(map[Vertex]struct{})
visited[startVet] = struct{}{}
// Queue used to implement BFS, using slice to simulate queue
queue := make([]Vertex, 0)
queue = append(queue, startVet)
// Starting from vertex vet, loop until all vertices are visited
for len(queue) > 0 {
// Dequeue the front vertex
vet := queue[0]
queue = queue[1:]
// Record visited vertex
res = append(res, vet)
// Traverse all adjacent vertices of this vertex
for _, adjVet := range g.adjList[vet] {
_, isExist := visited[adjVet]
// Only enqueue unvisited vertices
if !isExist {
queue = append(queue, adjVet)
visited[adjVet] = struct{}{}
}
}
}
// Return vertex traversal sequence
return res
}
@@ -0,0 +1,29 @@
// File: graph_bfs_test.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestGraphBFS(t *testing.T) {
/* Add edge */
vets := ValsToVets([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
edges := [][]Vertex{
{vets[0], vets[1]}, {vets[0], vets[3]}, {vets[1], vets[2]}, {vets[1], vets[4]},
{vets[2], vets[5]}, {vets[3], vets[4]}, {vets[3], vets[6]}, {vets[4], vets[5]},
{vets[4], vets[7]}, {vets[5], vets[8]}, {vets[6], vets[7]}, {vets[7], vets[8]}}
graph := newGraphAdjList(edges)
fmt.Println("After initialization, graph is:")
graph.print()
/* Breadth-first traversal */
res := graphBFS(graph, vets[0])
fmt.Println("Breadth-first traversal (BFS) vertex sequence is:")
PrintSlice(VetsToVals(res))
}
+36
View File
@@ -0,0 +1,36 @@
// File: graph_dfs.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
. "github.com/krahets/hello-algo/pkg"
)
/* Depth-first traversal helper function */
func dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {
// append operation returns a new reference, must reassign original reference to new slice's reference
*res = append(*res, vet)
visited[vet] = struct{}{}
// Traverse all adjacent vertices of this vertex
for _, adjVet := range g.adjList[vet] {
_, isExist := visited[adjVet]
// Recursively visit adjacent vertices
if !isExist {
dfs(g, visited, res, adjVet)
}
}
}
/* Depth-first traversal */
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
// Vertex traversal sequence
res := make([]Vertex, 0)
// Hash set for recording vertices that have been visited
visited := make(map[Vertex]struct{})
dfs(g, visited, &res, startVet)
// Return vertex traversal sequence
return res
}
@@ -0,0 +1,28 @@
// File: graph_dfs_test.go
// Created Time: 2023-02-18
// Author: Reanon (793584285@qq.com)
package chapter_graph
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestGraphDFS(t *testing.T) {
/* Add edge */
vets := ValsToVets([]int{0, 1, 2, 3, 4, 5, 6})
edges := [][]Vertex{
{vets[0], vets[1]}, {vets[0], vets[3]}, {vets[1], vets[2]},
{vets[2], vets[5]}, {vets[4], vets[5]}, {vets[5], vets[6]}}
graph := newGraphAdjList(edges)
fmt.Println("After initialization, graph is:")
graph.print()
/* Depth-first traversal */
res := graphDFS(graph, vets[0])
fmt.Println("Depth-first traversal (DFS) vertex sequence is:")
PrintSlice(VetsToVals(res))
}