mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-24 12:06:07 +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,107 @@
|
||||
/**
|
||||
* File: array.swift
|
||||
* Created Time: 2023-01-05
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Random access to element */
|
||||
func randomAccess(nums: [Int]) -> Int {
|
||||
// Randomly select a number in interval [0, nums.count)
|
||||
let randomIndex = nums.indices.randomElement()!
|
||||
// Retrieve and return the random element
|
||||
let randomNum = nums[randomIndex]
|
||||
return randomNum
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
func extend(nums: [Int], enlarge: Int) -> [Int] {
|
||||
// Initialize an array with extended length
|
||||
var res = Array(repeating: 0, count: nums.count + enlarge)
|
||||
// Copy all elements from the original array to the new array
|
||||
for i in nums.indices {
|
||||
res[i] = nums[i]
|
||||
}
|
||||
// Return the extended new array
|
||||
return res
|
||||
}
|
||||
|
||||
/* Insert element num at index index in the array */
|
||||
func insert(nums: inout [Int], num: Int, index: Int) {
|
||||
// Move all elements at and after index index backward by one position
|
||||
for i in nums.indices.dropFirst(index).reversed() {
|
||||
nums[i] = nums[i - 1]
|
||||
}
|
||||
// Assign num to the element at index index
|
||||
nums[index] = num
|
||||
}
|
||||
|
||||
/* Remove the element at index index */
|
||||
func remove(nums: inout [Int], index: Int) {
|
||||
// Move all elements after index index forward by one position
|
||||
for i in nums.indices.dropFirst(index).dropLast() {
|
||||
nums[i] = nums[i + 1]
|
||||
}
|
||||
}
|
||||
|
||||
/* Traverse array */
|
||||
func traverse(nums: [Int]) {
|
||||
var count = 0
|
||||
// Traverse array by index
|
||||
for i in nums.indices {
|
||||
count += nums[i]
|
||||
}
|
||||
// Direct traversal of array elements
|
||||
for num in nums {
|
||||
count += num
|
||||
}
|
||||
// Traverse simultaneously data index and elements
|
||||
for (i, num) in nums.enumerated() {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the specified element in the array */
|
||||
func find(nums: [Int], target: Int) -> Int {
|
||||
for i in nums.indices {
|
||||
if nums[i] == target {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@main
|
||||
enum _Array {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize array */
|
||||
let arr = Array(repeating: 0, count: 5)
|
||||
print("Array arr = \(arr)")
|
||||
var nums = [1, 3, 2, 5, 4]
|
||||
print("Array nums = \(nums)")
|
||||
|
||||
/* Insert element */
|
||||
let randomNum = randomAccess(nums: nums)
|
||||
print("Get random element \(randomNum) from nums")
|
||||
|
||||
/* Traverse array */
|
||||
nums = extend(nums: nums, enlarge: 3)
|
||||
print("Extend array length to 8, get nums = \(nums)")
|
||||
|
||||
/* Insert element */
|
||||
insert(nums: &nums, num: 6, index: 3)
|
||||
print("Insert number 6 at index 3, get nums = \(nums)")
|
||||
|
||||
/* Remove element */
|
||||
remove(nums: &nums, index: 2)
|
||||
print("Delete element at index 2, get nums = \(nums)")
|
||||
|
||||
/* Traverse array */
|
||||
traverse(nums: nums)
|
||||
|
||||
/* Find element */
|
||||
let index = find(nums: nums, target: 3)
|
||||
print("Find element 3 in nums, index = \(index)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* File: linked_list.swift
|
||||
* Created Time: 2023-01-08
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Insert node P after node n0 in the linked list */
|
||||
func insert(n0: ListNode, P: ListNode) {
|
||||
let n1 = n0.next
|
||||
P.next = n1
|
||||
n0.next = P
|
||||
}
|
||||
|
||||
/* Remove the first node after node n0 in the linked list */
|
||||
func remove(n0: ListNode) {
|
||||
if n0.next == nil {
|
||||
return
|
||||
}
|
||||
// n0 -> P -> n1
|
||||
let P = n0.next
|
||||
let n1 = P?.next
|
||||
n0.next = n1
|
||||
}
|
||||
|
||||
/* Access the node at index index in the linked list */
|
||||
func access(head: ListNode, index: Int) -> ListNode? {
|
||||
var head: ListNode? = head
|
||||
for _ in 0 ..< index {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
head = head?.next
|
||||
}
|
||||
return head
|
||||
}
|
||||
|
||||
/* Find the first node with value target in the linked list */
|
||||
func find(head: ListNode, target: Int) -> Int {
|
||||
var head: ListNode? = head
|
||||
var index = 0
|
||||
while head != nil {
|
||||
if head?.val == target {
|
||||
return index
|
||||
}
|
||||
head = head?.next
|
||||
index += 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@main
|
||||
enum LinkedList {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize linked list */
|
||||
// Initialize each node
|
||||
let n0 = ListNode(x: 1)
|
||||
let n1 = ListNode(x: 3)
|
||||
let n2 = ListNode(x: 2)
|
||||
let n3 = ListNode(x: 5)
|
||||
let n4 = ListNode(x: 4)
|
||||
// Build references between nodes
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
n3.next = n4
|
||||
print("Initialized linked list is")
|
||||
PrintUtil.printLinkedList(head: n0)
|
||||
|
||||
/* Insert node */
|
||||
insert(n0: n0, P: ListNode(x: 0))
|
||||
print("Linked list after inserting node is")
|
||||
PrintUtil.printLinkedList(head: n0)
|
||||
|
||||
/* Remove node */
|
||||
remove(n0: n0)
|
||||
print("Linked list after removing node is")
|
||||
PrintUtil.printLinkedList(head: n0)
|
||||
|
||||
/* Access node */
|
||||
let node = access(head: n0, index: 3)
|
||||
print("Value of node at index 3 in linked list = \(node!.val)")
|
||||
|
||||
/* Search node */
|
||||
let index = find(head: n0, target: 2)
|
||||
print("Index of node with value 2 in linked list = \(index)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* File: list.swift
|
||||
* Created Time: 2023-01-08
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
@main
|
||||
enum List {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize list */
|
||||
var nums = [1, 3, 2, 5, 4]
|
||||
print("List nums = \(nums)")
|
||||
|
||||
/* Update element */
|
||||
let num = nums[1]
|
||||
print("Access element at index 1, get num = \(num)")
|
||||
|
||||
/* Add elements at the end */
|
||||
nums[1] = 0
|
||||
print("Update element at index 1 to 0, get nums = \(nums)")
|
||||
|
||||
/* Remove element */
|
||||
nums.removeAll()
|
||||
print("After clearing list, nums = \(nums)")
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
nums.append(5)
|
||||
nums.append(4)
|
||||
print("After adding elements, nums = \(nums)")
|
||||
|
||||
/* Sort list */
|
||||
nums.insert(6, at: 3)
|
||||
print("Insert number 6 at index 3, get nums = \(nums)")
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(at: 3)
|
||||
print("Delete element at index 3, get nums = \(nums)")
|
||||
|
||||
/* Traverse list by index */
|
||||
var count = 0
|
||||
for i in nums.indices {
|
||||
count += nums[i]
|
||||
}
|
||||
/* Directly traverse list elements */
|
||||
count = 0
|
||||
for x in nums {
|
||||
count += x
|
||||
}
|
||||
|
||||
/* Concatenate two lists */
|
||||
let nums1 = [6, 8, 7, 10, 9]
|
||||
nums.append(contentsOf: nums1)
|
||||
print("After concatenating list nums1 to nums, get nums = \(nums)")
|
||||
|
||||
/* Sort list */
|
||||
nums.sort()
|
||||
print("After sorting list, nums = \(nums)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* File: my_list.swift
|
||||
* Created Time: 2023-01-08
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* List class */
|
||||
class MyList {
|
||||
private var arr: [Int] // Array (stores list elements)
|
||||
private var _capacity: Int // List capacity
|
||||
private var _size: Int // List length (current number of elements)
|
||||
private let extendRatio: Int // Multiple by which the list capacity is extended each time
|
||||
|
||||
/* Constructor */
|
||||
init() {
|
||||
_capacity = 10
|
||||
_size = 0
|
||||
extendRatio = 2
|
||||
arr = Array(repeating: 0, count: _capacity)
|
||||
}
|
||||
|
||||
/* Get list length (current number of elements) */
|
||||
func size() -> Int {
|
||||
_size
|
||||
}
|
||||
|
||||
/* Get list capacity */
|
||||
func capacity() -> Int {
|
||||
_capacity
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
func get(index: Int) -> Int {
|
||||
// Throw error if index out of bounds, same below
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("Index out of bounds")
|
||||
}
|
||||
return arr[index]
|
||||
}
|
||||
|
||||
/* Add elements at the end */
|
||||
func set(index: Int, num: Int) {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("Index out of bounds")
|
||||
}
|
||||
arr[index] = num
|
||||
}
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
func add(num: Int) {
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
arr[size()] = num
|
||||
// Update the number of elements
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* Sort list */
|
||||
func insert(index: Int, num: Int) {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("Index out of bounds")
|
||||
}
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if size() == capacity() {
|
||||
extendCapacity()
|
||||
}
|
||||
// Move all elements after index index forward by one position
|
||||
for j in (index ..< size()).reversed() {
|
||||
arr[j + 1] = arr[j]
|
||||
}
|
||||
arr[index] = num
|
||||
// Update the number of elements
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* Remove element */
|
||||
@discardableResult
|
||||
func remove(index: Int) -> Int {
|
||||
if index < 0 || index >= size() {
|
||||
fatalError("Index out of bounds")
|
||||
}
|
||||
let num = arr[index]
|
||||
// Move all elements after index forward by one position
|
||||
for j in index ..< (size() - 1) {
|
||||
arr[j] = arr[j + 1]
|
||||
}
|
||||
// Update the number of elements
|
||||
_size -= 1
|
||||
// Return the removed element
|
||||
return num
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func extendCapacity() {
|
||||
// Create a new array with length extendRatio times the original array and copy the original array to the new array
|
||||
arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))
|
||||
// Add elements at the end
|
||||
_capacity = arr.count
|
||||
}
|
||||
|
||||
/* Convert list to array */
|
||||
func toArray() -> [Int] {
|
||||
Array(arr.prefix(size()))
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum _MyList {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize list */
|
||||
let nums = MyList()
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(num: 1)
|
||||
nums.add(num: 3)
|
||||
nums.add(num: 2)
|
||||
nums.add(num: 5)
|
||||
nums.add(num: 4)
|
||||
print("List nums = \(nums.toArray()), capacity = \(nums.capacity()), length = \(nums.size())")
|
||||
|
||||
/* Sort list */
|
||||
nums.insert(index: 3, num: 6)
|
||||
print("Insert number 6 at index 3, get nums = \(nums.toArray())")
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(index: 3)
|
||||
print("Delete element at index 3, get nums = \(nums.toArray())")
|
||||
|
||||
/* Update element */
|
||||
let num = nums.get(index: 1)
|
||||
print("Access element at index 1, get num = \(num)")
|
||||
|
||||
/* Add elements at the end */
|
||||
nums.set(index: 1, num: 0)
|
||||
print("Update element at index 1 to 0, get nums = \(nums.toArray())")
|
||||
|
||||
/* Test capacity expansion mechanism */
|
||||
for i in 0 ..< 10 {
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
|
||||
nums.add(num: i)
|
||||
}
|
||||
print("After expansion, list nums = \(nums.toArray()), capacity = \(nums.capacity()), length = \(nums.size())")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user