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

View File

@@ -0,0 +1,148 @@
/**
* File: array_deque.swift
* Created Time: 2023-02-22
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Double-ended queue based on circular array implementation */
class ArrayDeque {
private var nums: [Int] // Array for storing double-ended queue elements
private var front: Int // Front pointer, points to the front of the queue element
private var _size: Int // Double-ended queue length
/* Constructor */
init(capacity: Int) {
nums = Array(repeating: 0, count: capacity)
front = 0
_size = 0
}
/* Get the capacity of the double-ended queue */
func capacity() -> Int {
nums.count
}
/* Get the length of the double-ended queue */
func size() -> Int {
_size
}
/* Check if the double-ended queue is empty */
func isEmpty() -> Bool {
size() == 0
}
/* Calculate circular array index */
private func index(i: Int) -> Int {
// Use modulo operation to wrap the array head and tail together
// When i passes the tail of the array, return to the head
// When i passes the head of the array, return to the tail
(i + capacity()) % capacity()
}
/* Front of the queue enqueue */
func pushFirst(num: Int) {
if size() == capacity() {
print("Double-ended queue is full")
return
}
// Use modulo operation to wrap front around to the tail after passing the head of the array
// Add num to the front of the queue
front = index(i: front - 1)
// Add num to front of queue
nums[front] = num
_size += 1
}
/* Rear of the queue enqueue */
func pushLast(num: Int) {
if size() == capacity() {
print("Double-ended queue is full")
return
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
let rear = index(i: front + size())
// Front pointer moves one position backward
nums[rear] = num
_size += 1
}
/* Rear of the queue dequeue */
func popFirst() -> Int {
let num = peekFirst()
// Move front pointer backward by one position
front = index(i: front + 1)
_size -= 1
return num
}
/* Access rear of the queue element */
func popLast() -> Int {
let num = peekLast()
_size -= 1
return num
}
/* Return list for printing */
func peekFirst() -> Int {
if isEmpty() {
fatalError("Deque is empty")
}
return nums[front]
}
/* Driver Code */
func peekLast() -> Int {
if isEmpty() {
fatalError("Deque is empty")
}
// Initialize double-ended queue
let last = index(i: front + size() - 1)
return nums[last]
}
/* Return array for printing */
func toArray() -> [Int] {
// Elements enqueue
(front ..< front + size()).map { nums[index(i: $0)] }
}
}
@main
enum _ArrayDeque {
/* Driver Code */
static func main() {
/* Get the length of the double-ended queue */
let deque = ArrayDeque(capacity: 10)
deque.pushLast(num: 3)
deque.pushLast(num: 2)
deque.pushLast(num: 5)
print("Deque deque = \(deque.toArray())")
/* Update element */
let peekFirst = deque.peekFirst()
print("Front element peekFirst = \(peekFirst)")
let peekLast = deque.peekLast()
print("Rear element peekLast = \(peekLast)")
/* Elements enqueue */
deque.pushLast(num: 4)
print("After element 4 enqueues at rear, deque = \(deque.toArray())")
deque.pushFirst(num: 1)
print("After element 1 enqueues at front, deque = \(deque.toArray())")
/* Element dequeue */
let popLast = deque.popLast()
print("Dequeue rear element = \(popLast), after rear dequeue deque = \(deque.toArray())")
let popFirst = deque.popFirst()
print("Dequeue front element = \(popFirst), after front dequeue deque = \(deque.toArray())")
/* Get the length of the double-ended queue */
let size = deque.size()
print("Deque length size = \(size)")
/* Check if the double-ended queue is empty */
let isEmpty = deque.isEmpty()
print("Is deque empty = \(isEmpty)")
}
}

View File

@@ -0,0 +1,113 @@
/**
* File: array_queue.swift
* Created Time: 2023-01-11
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Queue based on circular array implementation */
class ArrayQueue {
private var nums: [Int] // Array for storing queue elements
private var front: Int // Front pointer, points to the front of the queue element
private var _size: Int // Queue length
init(capacity: Int) {
// Initialize array
nums = Array(repeating: 0, count: capacity)
front = 0
_size = 0
}
/* Get the capacity of the queue */
func capacity() -> Int {
nums.count
}
/* Get the length of the queue */
func size() -> Int {
_size
}
/* Check if the queue is empty */
func isEmpty() -> Bool {
size() == 0
}
/* Enqueue */
func push(num: Int) {
if size() == capacity() {
print("Queue is full")
return
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
// Add num to the rear of the queue
let rear = (front + size()) % capacity()
// Front pointer moves one position backward
nums[rear] = num
_size += 1
}
/* Dequeue */
@discardableResult
func pop() -> Int {
let num = peek()
// Move front pointer backward by one position, if it passes the tail, return to array head
front = (front + 1) % capacity()
_size -= 1
return num
}
/* Return list for printing */
func peek() -> Int {
if isEmpty() {
fatalError("Queue is empty")
}
return nums[front]
}
/* Return array */
func toArray() -> [Int] {
// Elements enqueue
(front ..< front + size()).map { nums[$0 % capacity()] }
}
}
@main
enum _ArrayQueue {
/* Driver Code */
static func main() {
/* Access front of the queue element */
let capacity = 10
let queue = ArrayQueue(capacity: capacity)
/* Elements enqueue */
queue.push(num: 1)
queue.push(num: 3)
queue.push(num: 2)
queue.push(num: 5)
queue.push(num: 4)
print("Queue queue = \(queue.toArray())")
/* Return list for printing */
let peek = queue.peek()
print("Front element peek = \(peek)")
/* Element dequeue */
let pop = queue.pop()
print("Dequeue element pop = \(pop), after dequeue queue = \(queue.toArray())")
/* Get the length of the queue */
let size = queue.size()
print("Queue length size = \(size)")
/* Check if the queue is empty */
let isEmpty = queue.isEmpty()
print("Is queue empty = \(isEmpty)")
/* Test circular array */
for i in 0 ..< 10 {
queue.push(num: i)
queue.pop()
print("After round \(i) enqueue + dequeue, queue = \(queue.toArray())")
}
}
}

View File

@@ -0,0 +1,85 @@
/**
* File: array_stack.swift
* Created Time: 2023-01-09
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Stack based on array implementation */
class ArrayStack {
private var stack: [Int]
init() {
// Initialize list (dynamic array)
stack = []
}
/* Get the length of the stack */
func size() -> Int {
stack.count
}
/* Check if the stack is empty */
func isEmpty() -> Bool {
stack.isEmpty
}
/* Push */
func push(num: Int) {
stack.append(num)
}
/* Pop */
@discardableResult
func pop() -> Int {
if isEmpty() {
fatalError("Stack is empty")
}
return stack.removeLast()
}
/* Return list for printing */
func peek() -> Int {
if isEmpty() {
fatalError("Stack is empty")
}
return stack.last!
}
/* Convert List to Array and return */
func toArray() -> [Int] {
stack
}
}
@main
enum _ArrayStack {
/* Driver Code */
static func main() {
/* Access top of the stack element */
let stack = ArrayStack()
/* Elements push onto stack */
stack.push(num: 1)
stack.push(num: 3)
stack.push(num: 2)
stack.push(num: 5)
stack.push(num: 4)
print("Stack stack = \(stack.toArray())")
/* Return list for printing */
let peek = stack.peek()
print("Top element peek = \(peek)")
/* Element pop from stack */
let pop = stack.pop()
print("Pop element pop = \(pop), after pop stack = \(stack.toArray())")
/* Get the length of the stack */
let size = stack.size()
print("Stack length size = \(size)")
/* Check if empty */
let isEmpty = stack.isEmpty()
print("Is stack empty = \(isEmpty)")
}
}

View File

@@ -0,0 +1,44 @@
/**
* File: deque.swift
* Created Time: 2023-01-14
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum Deque {
/* Driver Code */
static func main() {
/* Get the length of the double-ended queue */
// Swift has no built-in deque class, can use Array as deque
var deque: [Int] = []
/* Elements enqueue */
deque.append(2)
deque.append(5)
deque.append(4)
deque.insert(3, at: 0)
deque.insert(1, at: 0)
print("Deque deque = \(deque)")
/* Update element */
let peekFirst = deque.first!
print("Front element peekFirst = \(peekFirst)")
let peekLast = deque.last!
print("Rear element peekLast = \(peekLast)")
/* Element dequeue */
// When simulating with Array, popFirst complexity is O(n)
let popFirst = deque.removeFirst()
print("Dequeue front element popFirst = \(popFirst), after front dequeue deque = \(deque)")
let popLast = deque.removeLast()
print("Dequeue rear element popLast = \(popLast), after rear dequeue deque = \(deque)")
/* Get the length of the double-ended queue */
let size = deque.count
print("Deque length size = \(size)")
/* Check if the double-ended queue is empty */
let isEmpty = deque.isEmpty
print("Is deque empty = \(isEmpty)")
}
}

View File

@@ -0,0 +1,180 @@
/**
* File: linkedlist_deque.swift
* Created Time: 2023-02-22
* Author: nuomi1 (nuomi1@qq.com)
*/
/* Doubly linked list node */
class ListNode {
var val: Int // Node value
var next: ListNode? // Successor node reference
weak var prev: ListNode? // Predecessor node reference
init(val: Int) {
self.val = val
}
}
/* Double-ended queue based on doubly linked list implementation */
class LinkedListDeque {
private var front: ListNode? // Head node front
private var rear: ListNode? // Tail node rear
private var _size: Int // Length of the double-ended queue
init() {
_size = 0
}
/* Get the length of the double-ended queue */
func size() -> Int {
_size
}
/* Check if the double-ended queue is empty */
func isEmpty() -> Bool {
size() == 0
}
/* Enqueue operation */
private func push(num: Int, isFront: Bool) {
let node = ListNode(val: num)
// If the linked list is empty, make both front and rear point to node
if isEmpty() {
front = node
rear = node
}
// Front of the queue enqueue operation
else if isFront {
// Add node to the head of the linked list
front?.prev = node
node.next = front
front = node // Update head node
}
// Rear of the queue enqueue operation
else {
// Add node to the tail of the linked list
rear?.next = node
node.prev = rear
rear = node // Update tail node
}
_size += 1 // Update queue length
}
/* Front of the queue enqueue */
func pushFirst(num: Int) {
push(num: num, isFront: true)
}
/* Rear of the queue enqueue */
func pushLast(num: Int) {
push(num: num, isFront: false)
}
/* Dequeue operation */
private func pop(isFront: Bool) -> Int {
if isEmpty() {
fatalError("Deque is empty")
}
let val: Int
// Temporarily store head node value
if isFront {
val = front!.val // Delete head node
// Delete head node
let fNext = front?.next
if fNext != nil {
fNext?.prev = nil
front?.next = nil
}
front = fNext // Update head node
}
// Temporarily store tail node value
else {
val = rear!.val // Delete tail node
// Update tail node
let rPrev = rear?.prev
if rPrev != nil {
rPrev?.next = nil
rear?.prev = nil
}
rear = rPrev // Update tail node
}
_size -= 1 // Update queue length
return val
}
/* Rear of the queue dequeue */
func popFirst() -> Int {
pop(isFront: true)
}
/* Access rear of the queue element */
func popLast() -> Int {
pop(isFront: false)
}
/* Return list for printing */
func peekFirst() -> Int {
if isEmpty() {
fatalError("Deque is empty")
}
return front!.val
}
/* Driver Code */
func peekLast() -> Int {
if isEmpty() {
fatalError("Deque is empty")
}
return rear!.val
}
/* Return array for printing */
func toArray() -> [Int] {
var node = front
var res = Array(repeating: 0, count: size())
for i in res.indices {
res[i] = node!.val
node = node?.next
}
return res
}
}
@main
enum _LinkedListDeque {
/* Driver Code */
static func main() {
/* Get the length of the double-ended queue */
let deque = LinkedListDeque()
deque.pushLast(num: 3)
deque.pushLast(num: 2)
deque.pushLast(num: 5)
print("Deque deque = \(deque.toArray())")
/* Update element */
let peekFirst = deque.peekFirst()
print("Front element peekFirst = \(peekFirst)")
let peekLast = deque.peekLast()
print("Rear element peekLast = \(peekLast)")
/* Elements enqueue */
deque.pushLast(num: 4)
print("After element 4 enqueues at rear, deque = \(deque.toArray())")
deque.pushFirst(num: 1)
print("After element 1 enqueues at front, deque = \(deque.toArray())")
/* Element dequeue */
let popLast = deque.popLast()
print("Dequeue rear element = \(popLast), after rear dequeue deque = \(deque.toArray())")
let popFirst = deque.popFirst()
print("Dequeue front element = \(popFirst), after front dequeue deque = \(deque.toArray())")
/* Get the length of the double-ended queue */
let size = deque.size()
print("Deque length size = \(size)")
/* Check if the double-ended queue is empty */
let isEmpty = deque.isEmpty()
print("Is deque empty = \(isEmpty)")
}
}

View File

@@ -0,0 +1,107 @@
/**
* File: linkedlist_queue.swift
* Created Time: 2023-01-11
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Queue based on linked list implementation */
class LinkedListQueue {
private var front: ListNode? // Head node
private var rear: ListNode? // Tail node
private var _size: Int
init() {
_size = 0
}
/* Get the length of the queue */
func size() -> Int {
_size
}
/* Check if the queue is empty */
func isEmpty() -> Bool {
size() == 0
}
/* Enqueue */
func push(num: Int) {
// Add num after the tail node
let node = ListNode(x: num)
// If the queue is empty, make both front and rear point to the node
if front == nil {
front = node
rear = node
}
// If the queue is not empty, add the node after the tail node
else {
rear?.next = node
rear = node
}
_size += 1
}
/* Dequeue */
@discardableResult
func pop() -> Int {
let num = peek()
// Delete head node
front = front?.next
_size -= 1
return num
}
/* Return list for printing */
func peek() -> Int {
if isEmpty() {
fatalError("Queue is empty")
}
return front!.val
}
/* Convert linked list to Array and return */
func toArray() -> [Int] {
var node = front
var res = Array(repeating: 0, count: size())
for i in res.indices {
res[i] = node!.val
node = node?.next
}
return res
}
}
@main
enum _LinkedListQueue {
/* Driver Code */
static func main() {
/* Access front of the queue element */
let queue = LinkedListQueue()
/* Elements enqueue */
queue.push(num: 1)
queue.push(num: 3)
queue.push(num: 2)
queue.push(num: 5)
queue.push(num: 4)
print("Queue queue = \(queue.toArray())")
/* Return list for printing */
let peek = queue.peek()
print("Front element peek = \(peek)")
/* Element dequeue */
let pop = queue.pop()
print("Dequeue element pop = \(pop), after dequeue queue = \(queue.toArray())")
/* Get the length of the queue */
let size = queue.size()
print("Queue length size = \(size)")
/* Check if the queue is empty */
let isEmpty = queue.isEmpty()
print("Is queue empty = \(isEmpty)")
}
}

View File

@@ -0,0 +1,96 @@
/**
* File: linkedlist_stack.swift
* Created Time: 2023-01-09
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* Stack based on linked list implementation */
class LinkedListStack {
private var _peek: ListNode? // Use head node as stack top
private var _size: Int // Stack length
init() {
_size = 0
}
/* Get the length of the stack */
func size() -> Int {
_size
}
/* Check if the stack is empty */
func isEmpty() -> Bool {
size() == 0
}
/* Push */
func push(num: Int) {
let node = ListNode(x: num)
node.next = _peek
_peek = node
_size += 1
}
/* Pop */
@discardableResult
func pop() -> Int {
let num = peek()
_peek = _peek?.next
_size -= 1
return num
}
/* Return list for printing */
func peek() -> Int {
if isEmpty() {
fatalError("Stack is empty")
}
return _peek!.val
}
/* Convert List to Array and return */
func toArray() -> [Int] {
var node = _peek
var res = Array(repeating: 0, count: size())
for i in res.indices.reversed() {
res[i] = node!.val
node = node?.next
}
return res
}
}
@main
enum _LinkedListStack {
/* Driver Code */
static func main() {
/* Access top of the stack element */
let stack = LinkedListStack()
/* Elements push onto stack */
stack.push(num: 1)
stack.push(num: 3)
stack.push(num: 2)
stack.push(num: 5)
stack.push(num: 4)
print("Stack stack = \(stack.toArray())")
/* Return list for printing */
let peek = stack.peek()
print("Top element peek = \(peek)")
/* Element pop from stack */
let pop = stack.pop()
print("Pop element pop = \(pop), after pop stack = \(stack.toArray())")
/* Get the length of the stack */
let size = stack.size()
print("Stack length size = \(size)")
/* Check if empty */
let isEmpty = stack.isEmpty()
print("Is stack empty = \(isEmpty)")
}
}

View File

@@ -0,0 +1,40 @@
/**
* File: queue.swift
* Created Time: 2023-01-11
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum Queue {
/* Driver Code */
static func main() {
/* Access front of the queue element */
// Swift has no built-in queue class, can use Array as queue
var queue: [Int] = []
/* Elements enqueue */
queue.append(1)
queue.append(3)
queue.append(2)
queue.append(5)
queue.append(4)
print("Queue queue = \(queue)")
/* Return list for printing */
let peek = queue.first!
print("Front element peek = \(peek)")
/* Element dequeue */
// When simulating with Array, pop complexity is O(n)
let pool = queue.removeFirst()
print("Dequeue element pop = \(pool), after dequeue queue = \(queue)")
/* Get the length of the queue */
let size = queue.count
print("Queue length size = \(size)")
/* Check if the queue is empty */
let isEmpty = queue.isEmpty
print("Is queue empty = \(isEmpty)")
}
}

View File

@@ -0,0 +1,39 @@
/**
* File: stack.swift
* Created Time: 2023-01-09
* Author: nuomi1 (nuomi1@qq.com)
*/
@main
enum Stack {
/* Driver Code */
static func main() {
/* Access top of the stack element */
// Swift has no built-in stack class, can use Array as stack
var stack: [Int] = []
/* Elements push onto stack */
stack.append(1)
stack.append(3)
stack.append(2)
stack.append(5)
stack.append(4)
print("Stack stack = \(stack)")
/* Return list for printing */
let peek = stack.last!
print("Top element peek = \(peek)")
/* Element pop from stack */
let pop = stack.removeLast()
print("Pop element pop = \(pop), after pop stack = \(stack)")
/* Get the length of the stack */
let size = stack.count
print("Stack length size = \(size)")
/* Check if empty */
let isEmpty = stack.isEmpty
print("Is stack empty = \(isEmpty)")
}
}