mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-21 15:57:14 +00: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:
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* File: graph_adjacency_list.swift
|
||||
* Created Time: 2023-02-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Undirected graph class based on adjacency list */
|
||||
public class GraphAdjList {
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
public private(set) var adjList: [Vertex: [Vertex]]
|
||||
|
||||
/* Constructor */
|
||||
public init(edges: [[Vertex]]) {
|
||||
adjList = [:]
|
||||
// Add all vertices and edges
|
||||
for edge in edges {
|
||||
addVertex(vet: edge[0])
|
||||
addVertex(vet: edge[1])
|
||||
addEdge(vet1: edge[0], vet2: edge[1])
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
public func size() -> Int {
|
||||
adjList.count
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
public func addEdge(vet1: Vertex, vet2: Vertex) {
|
||||
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
|
||||
fatalError("Invalid parameter")
|
||||
}
|
||||
// Add edge vet1 - vet2
|
||||
adjList[vet1]?.append(vet2)
|
||||
adjList[vet2]?.append(vet1)
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
public func removeEdge(vet1: Vertex, vet2: Vertex) {
|
||||
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
|
||||
fatalError("Invalid parameter")
|
||||
}
|
||||
// Remove edge vet1 - vet2
|
||||
adjList[vet1]?.removeAll { $0 == vet2 }
|
||||
adjList[vet2]?.removeAll { $0 == vet1 }
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
public func addVertex(vet: Vertex) {
|
||||
if adjList[vet] != nil {
|
||||
return
|
||||
}
|
||||
// Add a new linked list in the adjacency list
|
||||
adjList[vet] = []
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
public func removeVertex(vet: Vertex) {
|
||||
if adjList[vet] == nil {
|
||||
fatalError("Invalid parameter")
|
||||
}
|
||||
// Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
adjList.removeValue(forKey: vet)
|
||||
// Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
for key in adjList.keys {
|
||||
adjList[key]?.removeAll { $0 == vet }
|
||||
}
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
public func print() {
|
||||
Swift.print("Adjacency list =")
|
||||
for (vertex, list) in adjList {
|
||||
let list = list.map { $0.val }
|
||||
Swift.print("\(vertex.val): \(list),")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if !TARGET
|
||||
|
||||
@main
|
||||
enum GraphAdjacencyList {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Add edge */
|
||||
let v = Vertex.valsToVets(vals: [1, 3, 2, 5, 4])
|
||||
let edges = [[v[0], v[1]], [v[0], v[3]], [v[1], v[2]], [v[2], v[3]], [v[2], v[4]], [v[3], v[4]]]
|
||||
let graph = GraphAdjList(edges: edges)
|
||||
print("\nAfter initialization, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 3 are v[0], v[1]
|
||||
graph.addEdge(vet1: v[0], vet2: v[2])
|
||||
print("\nAfter adding edge 1-2, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Remove edge */
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeEdge(vet1: v[0], vet2: v[1])
|
||||
print("\nAfter removing edge 1-3, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Add vertex */
|
||||
let v5 = Vertex(val: 6)
|
||||
graph.addVertex(vet: v5)
|
||||
print("\nAfter adding vertex 6, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeVertex(vet: v[1])
|
||||
print("\nAfter removing vertex 3, graph is")
|
||||
graph.print()
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* File: graph_adjacency_list.swift
|
||||
* Created Time: 2023-02-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Undirected graph class based on adjacency list */
|
||||
public class GraphAdjList {
|
||||
// Adjacency list, key: vertex, value: all adjacent vertices of that vertex
|
||||
public private(set) var adjList: [Vertex: [Vertex]]
|
||||
|
||||
/* Constructor */
|
||||
public init(edges: [[Vertex]]) {
|
||||
adjList = [:]
|
||||
// Add all vertices and edges
|
||||
for edge in edges {
|
||||
addVertex(vet: edge[0])
|
||||
addVertex(vet: edge[1])
|
||||
addEdge(vet1: edge[0], vet2: edge[1])
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
public func size() -> Int {
|
||||
adjList.count
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
public func addEdge(vet1: Vertex, vet2: Vertex) {
|
||||
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
|
||||
fatalError("Invalid parameter")
|
||||
}
|
||||
// Add edge vet1 - vet2
|
||||
adjList[vet1]?.append(vet2)
|
||||
adjList[vet2]?.append(vet1)
|
||||
}
|
||||
|
||||
/* Remove edge */
|
||||
public func removeEdge(vet1: Vertex, vet2: Vertex) {
|
||||
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
|
||||
fatalError("Invalid parameter")
|
||||
}
|
||||
// Remove edge vet1 - vet2
|
||||
adjList[vet1]?.removeAll { $0 == vet2 }
|
||||
adjList[vet2]?.removeAll { $0 == vet1 }
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
public func addVertex(vet: Vertex) {
|
||||
if adjList[vet] != nil {
|
||||
return
|
||||
}
|
||||
// Add a new linked list in the adjacency list
|
||||
adjList[vet] = []
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
public func removeVertex(vet: Vertex) {
|
||||
if adjList[vet] == nil {
|
||||
fatalError("Invalid parameter")
|
||||
}
|
||||
// Remove the linked list corresponding to vertex vet in the adjacency list
|
||||
adjList.removeValue(forKey: vet)
|
||||
// Traverse the linked lists of other vertices and remove all edges containing vet
|
||||
for key in adjList.keys {
|
||||
adjList[key]?.removeAll { $0 == vet }
|
||||
}
|
||||
}
|
||||
|
||||
/* Print adjacency list */
|
||||
public func print() {
|
||||
Swift.print("Adjacency list =")
|
||||
for (vertex, list) in adjList {
|
||||
let list = list.map { $0.val }
|
||||
Swift.print("\(vertex.val): \(list),")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if !TARGET
|
||||
|
||||
@main
|
||||
enum GraphAdjacencyList {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Add edge */
|
||||
let v = Vertex.valsToVets(vals: [1, 3, 2, 5, 4])
|
||||
let edges = [[v[0], v[1]], [v[0], v[3]], [v[1], v[2]], [v[2], v[3]], [v[2], v[4]], [v[3], v[4]]]
|
||||
let graph = GraphAdjList(edges: edges)
|
||||
print("\nAfter initialization, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Add edge */
|
||||
// Vertices 1, 3 are v[0], v[1]
|
||||
graph.addEdge(vet1: v[0], vet2: v[2])
|
||||
print("\nAfter adding edge 1-2, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Remove edge */
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeEdge(vet1: v[0], vet2: v[1])
|
||||
print("\nAfter removing edge 1-3, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Add vertex */
|
||||
let v5 = Vertex(val: 6)
|
||||
graph.addVertex(vet: v5)
|
||||
print("\nAfter adding vertex 6, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 is v[1]
|
||||
graph.removeVertex(vet: v[1])
|
||||
print("\nAfter removing vertex 3, graph is")
|
||||
graph.print()
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* File: graph_adjacency_matrix.swift
|
||||
* Created Time: 2023-02-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Undirected graph class based on adjacency matrix */
|
||||
class GraphAdjMat {
|
||||
private var vertices: [Int] // Vertex list, where the element represents the "vertex value" and the index represents the "vertex index"
|
||||
private var adjMat: [[Int]] // Adjacency matrix, where the row and column indices correspond to the "vertex index"
|
||||
|
||||
/* Constructor */
|
||||
init(vertices: [Int], edges: [[Int]]) {
|
||||
self.vertices = []
|
||||
adjMat = []
|
||||
// Add vertex
|
||||
for val in vertices {
|
||||
addVertex(val: val)
|
||||
}
|
||||
// Add edge
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
for e in edges {
|
||||
addEdge(i: e[0], j: e[1])
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the number of vertices */
|
||||
func size() -> Int {
|
||||
vertices.count
|
||||
}
|
||||
|
||||
/* Add vertex */
|
||||
func addVertex(val: Int) {
|
||||
let n = size()
|
||||
// Add the value of the new vertex to the vertex list
|
||||
vertices.append(val)
|
||||
// Add a row to the adjacency matrix
|
||||
let newRow = Array(repeating: 0, count: n)
|
||||
adjMat.append(newRow)
|
||||
// Add a column to the adjacency matrix
|
||||
for i in adjMat.indices {
|
||||
adjMat[i].append(0)
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove vertex */
|
||||
func removeVertex(index: Int) {
|
||||
if index >= size() {
|
||||
fatalError("Out of bounds")
|
||||
}
|
||||
// Remove the vertex at index from the vertex list
|
||||
vertices.remove(at: index)
|
||||
// Remove the row at index from the adjacency matrix
|
||||
adjMat.remove(at: index)
|
||||
// Remove the column at index from the adjacency matrix
|
||||
for i in adjMat.indices {
|
||||
adjMat[i].remove(at: index)
|
||||
}
|
||||
}
|
||||
|
||||
/* Add edge */
|
||||
// Parameters i, j correspond to the vertices element indices
|
||||
func addEdge(i: Int, j: Int) {
|
||||
// Handle index out of bounds and equality
|
||||
if i < 0 || j < 0 || i >= size() || j >= size() || i == j {
|
||||
fatalError("Out of bounds")
|
||||
}
|
||||
// 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
|
||||
func removeEdge(i: Int, j: Int) {
|
||||
// Handle index out of bounds and equality
|
||||
if i < 0 || j < 0 || i >= size() || j >= size() || i == j {
|
||||
fatalError("Out of bounds")
|
||||
}
|
||||
adjMat[i][j] = 0
|
||||
adjMat[j][i] = 0
|
||||
}
|
||||
|
||||
/* Print adjacency matrix */
|
||||
func print() {
|
||||
Swift.print("Vertex list = ", terminator: "")
|
||||
Swift.print(vertices)
|
||||
Swift.print("Adjacency matrix =")
|
||||
PrintUtil.printMatrix(matrix: adjMat)
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum GraphAdjacencyMatrix {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Add edge */
|
||||
// Note that the edges elements represent vertex indices, i.e., corresponding to the vertices element indices
|
||||
let vertices = [1, 3, 2, 5, 4]
|
||||
let edges = [[0, 1], [1, 2], [2, 3], [0, 3], [2, 4], [3, 4]]
|
||||
let graph = GraphAdjMat(vertices: vertices, edges: edges)
|
||||
print("\nAfter initialization, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Add edge */
|
||||
// Add vertex
|
||||
graph.addEdge(i: 0, j: 2)
|
||||
print("\nAfter adding edge 1-2, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Remove edge */
|
||||
// Vertices 1, 3 have indices 0, 1 respectively
|
||||
graph.removeEdge(i: 0, j: 1)
|
||||
print("\nAfter removing edge 1-3, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Add vertex */
|
||||
graph.addVertex(val: 6)
|
||||
print("\nAfter adding vertex 6, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Remove vertex */
|
||||
// Vertex 3 has index 1
|
||||
graph.removeVertex(index: 1)
|
||||
print("\nAfter removing vertex 3, graph is")
|
||||
graph.print()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* File: graph_bfs.swift
|
||||
* Created Time: 2023-02-21
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import graph_adjacency_list_target
|
||||
import utils
|
||||
|
||||
/* Breadth-first traversal */
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
func graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
|
||||
// Vertex traversal sequence
|
||||
var res: [Vertex] = []
|
||||
// Hash set for recording vertices that have been visited
|
||||
var visited: Set<Vertex> = [startVet]
|
||||
// Queue used to implement BFS
|
||||
var que: [Vertex] = [startVet]
|
||||
// Starting from vertex vet, loop until all vertices are visited
|
||||
while !que.isEmpty {
|
||||
let vet = que.removeFirst() // Dequeue the front vertex
|
||||
res.append(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.append(adjVet) // Only enqueue unvisited vertices
|
||||
visited.insert(adjVet) // Mark this vertex as visited
|
||||
}
|
||||
}
|
||||
// Return vertex traversal sequence
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum GraphBFS {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Add edge */
|
||||
let v = Vertex.valsToVets(vals: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
let edges = [
|
||||
[v[0], v[1]], [v[0], v[3]], [v[1], v[2]], [v[1], v[4]],
|
||||
[v[2], v[5]], [v[3], v[4]], [v[3], v[6]], [v[4], v[5]],
|
||||
[v[4], v[7]], [v[5], v[8]], [v[6], v[7]], [v[7], v[8]],
|
||||
]
|
||||
let graph = GraphAdjList(edges: edges)
|
||||
print("\nAfter initialization, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Breadth-first traversal */
|
||||
let res = graphBFS(graph: graph, startVet: v[0])
|
||||
print("\nBreadth-first traversal (BFS) vertex sequence is")
|
||||
print(Vertex.vetsToVals(vets: res))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* File: graph_dfs.swift
|
||||
* Created Time: 2023-02-21
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import graph_adjacency_list_target
|
||||
import utils
|
||||
|
||||
/* Depth-first traversal helper function */
|
||||
func dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {
|
||||
res.append(vet) // Record visited vertex
|
||||
visited.insert(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: graph, visited: &visited, res: &res, vet: adjVet)
|
||||
}
|
||||
}
|
||||
|
||||
/* Depth-first traversal */
|
||||
// Use adjacency list to represent the graph, in order to obtain all adjacent vertices of a specified vertex
|
||||
func graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
|
||||
// Vertex traversal sequence
|
||||
var res: [Vertex] = []
|
||||
// Hash set for recording vertices that have been visited
|
||||
var visited: Set<Vertex> = []
|
||||
dfs(graph: graph, visited: &visited, res: &res, vet: startVet)
|
||||
return res
|
||||
}
|
||||
|
||||
@main
|
||||
enum GraphDFS {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Add edge */
|
||||
let v = Vertex.valsToVets(vals: [0, 1, 2, 3, 4, 5, 6])
|
||||
let edges = [
|
||||
[v[0], v[1]], [v[0], v[3]], [v[1], v[2]],
|
||||
[v[2], v[5]], [v[4], v[5]], [v[5], v[6]],
|
||||
]
|
||||
let graph = GraphAdjList(edges: edges)
|
||||
print("\nAfter initialization, graph is")
|
||||
graph.print()
|
||||
|
||||
/* Depth-first traversal */
|
||||
let res = graphDFS(graph: graph, startVet: v[0])
|
||||
print("\nDepth-first traversal (DFS) vertex sequence is")
|
||||
print(Vertex.vetsToVals(vets: res))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user