mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-23 03:46:05 +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,79 @@
|
||||
// File: array.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
/* Random access to element */
|
||||
func randomAccess(nums []int) (randomNum int) {
|
||||
// Randomly select a number in the interval [0, nums.length)
|
||||
randomIndex := rand.Intn(len(nums))
|
||||
// Retrieve and return the random element
|
||||
randomNum = nums[randomIndex]
|
||||
return
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
func extend(nums []int, enlarge int) []int {
|
||||
// Initialize an array with extended length
|
||||
res := make([]int, len(nums)+enlarge)
|
||||
// Copy all elements from the original array to the new array
|
||||
for i, num := range nums {
|
||||
res[i] = num
|
||||
}
|
||||
// Return the extended new array
|
||||
return res
|
||||
}
|
||||
|
||||
/* Insert element num at index index in the array */
|
||||
func insert(nums []int, num int, index int) {
|
||||
// Move all elements at and after index index backward by one position
|
||||
for i := len(nums) - 1; i > index; i-- {
|
||||
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 []int, index int) {
|
||||
// Move all elements after index index forward by one position
|
||||
for i := index; i < len(nums)-1; i++ {
|
||||
nums[i] = nums[i+1]
|
||||
}
|
||||
}
|
||||
|
||||
/* Traverse array */
|
||||
func traverse(nums []int) {
|
||||
count := 0
|
||||
// Traverse array by index
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count += nums[i]
|
||||
}
|
||||
count = 0
|
||||
// Direct traversal of array elements
|
||||
for _, num := range nums {
|
||||
count += num
|
||||
}
|
||||
// Traverse simultaneously data index and elements
|
||||
for i, num := range nums {
|
||||
count += nums[i]
|
||||
count += num
|
||||
}
|
||||
}
|
||||
|
||||
/* Find the specified element in the array */
|
||||
func find(nums []int, target int) (index int) {
|
||||
index = -1
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if nums[i] == target {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// File: array_test.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
/**
|
||||
We treat Go Slice as Array here. This reduces
|
||||
the learning cost and allows us to focus on data structures and algorithms.
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestArray(t *testing.T) {
|
||||
/* Initialize array */
|
||||
var arr [5]int
|
||||
fmt.Println("Array arr =", arr)
|
||||
// In Go, specifying length ([5]int) creates an array, not specifying length ([]int) creates a slice
|
||||
// Since Go arrays are designed to have their length determined at compile time, only constants can be used to specify the length
|
||||
// For convenience in implementing the extend() function, slices are treated as arrays below
|
||||
nums := []int{1, 3, 2, 5, 4}
|
||||
fmt.Println("Array nums =", nums)
|
||||
|
||||
/* Insert element */
|
||||
randomNum := randomAccess(nums)
|
||||
fmt.Println("Get random element in nums", randomNum)
|
||||
|
||||
/* Traverse array */
|
||||
nums = extend(nums, 3)
|
||||
fmt.Println("Extend array length to 8, get nums =", nums)
|
||||
|
||||
/* Insert element */
|
||||
insert(nums, 6, 3)
|
||||
fmt.Println("Insert number 6 at index 3, get nums =", nums)
|
||||
|
||||
/* Remove element */
|
||||
remove(nums, 2)
|
||||
fmt.Println("Remove element at index 2, get nums =", nums)
|
||||
|
||||
/* Traverse array */
|
||||
traverse(nums)
|
||||
|
||||
/* Find element */
|
||||
index := find(nums, 3)
|
||||
fmt.Println("Find element 3 in nums, get index =", index)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// File: linked_list.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
/* Insert node P after node n0 in the linked list */
|
||||
func insertNode(n0 *ListNode, P *ListNode) {
|
||||
n1 := n0.Next
|
||||
P.Next = n1
|
||||
n0.Next = P
|
||||
}
|
||||
|
||||
/* Remove the first node after node n0 in the linked list */
|
||||
func removeItem(n0 *ListNode) {
|
||||
if n0.Next == nil {
|
||||
return
|
||||
}
|
||||
// n0 -> P -> n1
|
||||
P := n0.Next
|
||||
n1 := P.Next
|
||||
n0.Next = n1
|
||||
}
|
||||
|
||||
/* Access the node at index index in the linked list */
|
||||
func access(head *ListNode, index int) *ListNode {
|
||||
for i := 0; i < index; i++ {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
head = head.Next
|
||||
}
|
||||
return head
|
||||
}
|
||||
|
||||
/* Find the first node with value target in the linked list */
|
||||
func findNode(head *ListNode, target int) int {
|
||||
index := 0
|
||||
for head != nil {
|
||||
if head.Val == target {
|
||||
return index
|
||||
}
|
||||
head = head.Next
|
||||
index++
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// File: linked_list_test.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: cathay (cathaycchen@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestLinkedList(t *testing.T) {
|
||||
/* Initialize linked list 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// Initialize each node
|
||||
n0 := NewListNode(1)
|
||||
n1 := NewListNode(3)
|
||||
n2 := NewListNode(2)
|
||||
n3 := NewListNode(5)
|
||||
n4 := NewListNode(4)
|
||||
|
||||
// Build references between nodes
|
||||
n0.Next = n1
|
||||
n1.Next = n2
|
||||
n2.Next = n3
|
||||
n3.Next = n4
|
||||
fmt.Println("Initialized linked list is")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* Insert node */
|
||||
insertNode(n0, NewListNode(0))
|
||||
fmt.Println("Linked list after inserting node is")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* Remove node */
|
||||
removeItem(n0)
|
||||
fmt.Println("Linked list after removing node is")
|
||||
PrintLinkedList(n0)
|
||||
|
||||
/* Access node */
|
||||
node := access(n0, 3)
|
||||
fmt.Println("Value of node at index 3 in linked list =", node)
|
||||
|
||||
/* Search node */
|
||||
index := findNode(n0, 2)
|
||||
fmt.Println("Index of node with value 2 in linked list =", index)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// File: list_test.go
|
||||
// Created Time: 2022-12-18
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestList(t *testing.T) {
|
||||
/* Initialize list */
|
||||
nums := []int{1, 3, 2, 5, 4}
|
||||
fmt.Println("List nums =", nums)
|
||||
|
||||
/* Update element */
|
||||
num := nums[1] // Access element at index 1
|
||||
fmt.Println("Access element at index 1, get num =", num)
|
||||
|
||||
/* Add elements at the end */
|
||||
nums[1] = 0 // Update element at index 1 to 0
|
||||
fmt.Println("Update element at index 1 to 0, get nums =", nums)
|
||||
|
||||
/* Remove element */
|
||||
nums = nil
|
||||
fmt.Println("After clearing list, nums =", nums)
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
nums = append(nums, 1)
|
||||
nums = append(nums, 3)
|
||||
nums = append(nums, 2)
|
||||
nums = append(nums, 5)
|
||||
nums = append(nums, 4)
|
||||
fmt.Println("After adding elements, nums =", nums)
|
||||
|
||||
/* Sort list */
|
||||
nums = append(nums[:3], append([]int{6}, nums[3:]...)...) // Insert number 6 at index 3
|
||||
fmt.Println("Insert number 6 at index 3, get nums =", nums)
|
||||
|
||||
/* Remove element */
|
||||
nums = append(nums[:3], nums[4:]...) // Remove element at index 3
|
||||
fmt.Println("Remove element at index 3, get nums =", nums)
|
||||
|
||||
/* Traverse list by index */
|
||||
count := 0
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count += nums[i]
|
||||
}
|
||||
/* Directly traverse list elements */
|
||||
count = 0
|
||||
for _, x := range nums {
|
||||
count += x
|
||||
}
|
||||
|
||||
/* Concatenate two lists */
|
||||
nums1 := []int{6, 8, 7, 10, 9}
|
||||
nums = append(nums, nums1...) // Concatenate list nums1 to nums
|
||||
fmt.Println("Concatenate list nums1 to nums, get nums =", nums)
|
||||
|
||||
/* Sort list */
|
||||
sort.Ints(nums) // After sorting, list elements are arranged from smallest to largest
|
||||
fmt.Println("After sorting list, nums =", nums)
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// File: my_list.go
|
||||
// Created Time: 2022-12-18
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
/* List class */
|
||||
type myList struct {
|
||||
arrCapacity int
|
||||
arr []int
|
||||
arrSize int
|
||||
extendRatio int
|
||||
}
|
||||
|
||||
/* Constructor */
|
||||
func newMyList() *myList {
|
||||
return &myList{
|
||||
arrCapacity: 10, // List capacity
|
||||
arr: make([]int, 10), // Array (stores list elements)
|
||||
arrSize: 0, // List length (current number of elements)
|
||||
extendRatio: 2, // Multiple by which the list capacity is extended each time
|
||||
}
|
||||
}
|
||||
|
||||
/* Get list length (current number of elements) */
|
||||
func (l *myList) size() int {
|
||||
return l.arrSize
|
||||
}
|
||||
|
||||
/* Get list capacity */
|
||||
func (l *myList) capacity() int {
|
||||
return l.arrCapacity
|
||||
}
|
||||
|
||||
/* Update element */
|
||||
func (l *myList) get(index int) int {
|
||||
// If the index is out of bounds, throw an exception, as below
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("Index out of bounds")
|
||||
}
|
||||
return l.arr[index]
|
||||
}
|
||||
|
||||
/* Add elements at the end */
|
||||
func (l *myList) set(num, index int) {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("Index out of bounds")
|
||||
}
|
||||
l.arr[index] = num
|
||||
}
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
func (l *myList) add(num int) {
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if l.arrSize == l.arrCapacity {
|
||||
l.extendCapacity()
|
||||
}
|
||||
l.arr[l.arrSize] = num
|
||||
// Update the number of elements
|
||||
l.arrSize++
|
||||
}
|
||||
|
||||
/* Sort list */
|
||||
func (l *myList) insert(num, index int) {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("Index out of bounds")
|
||||
}
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if l.arrSize == l.arrCapacity {
|
||||
l.extendCapacity()
|
||||
}
|
||||
// Move all elements after index index forward by one position
|
||||
for j := l.arrSize - 1; j >= index; j-- {
|
||||
l.arr[j+1] = l.arr[j]
|
||||
}
|
||||
l.arr[index] = num
|
||||
// Update the number of elements
|
||||
l.arrSize++
|
||||
}
|
||||
|
||||
/* Remove element */
|
||||
func (l *myList) remove(index int) int {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("Index out of bounds")
|
||||
}
|
||||
num := l.arr[index]
|
||||
// Create a new array with length _extend_ratio times the original array, and copy the original array to the new array
|
||||
for j := index; j < l.arrSize-1; j++ {
|
||||
l.arr[j] = l.arr[j+1]
|
||||
}
|
||||
// Update the number of elements
|
||||
l.arrSize--
|
||||
// Return the removed element
|
||||
return num
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func (l *myList) extendCapacity() {
|
||||
// Create a new array with length extendRatio times the original array and copy the original array to the new array
|
||||
l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)
|
||||
// Add elements at the end
|
||||
l.arrCapacity = len(l.arr)
|
||||
}
|
||||
|
||||
/* Return list with valid length */
|
||||
func (l *myList) toArray() []int {
|
||||
// Elements enqueue
|
||||
return l.arr[:l.arrSize]
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// File: my_list_test.go
|
||||
// Created Time: 2022-12-18
|
||||
// Author: msk397 (machangxinq@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestMyList(t *testing.T) {
|
||||
/* Initialize list */
|
||||
nums := newMyList()
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
fmt.Printf("List nums = %v, capacity = %v, length = %v\n", nums.toArray(), nums.capacity(), nums.size())
|
||||
|
||||
/* Sort list */
|
||||
nums.insert(6, 3)
|
||||
fmt.Printf("Insert number 6 at index 3, get nums = %v\n", nums.toArray())
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3)
|
||||
fmt.Printf("Remove element at index 3, get nums = %v\n", nums.toArray())
|
||||
|
||||
/* Update element */
|
||||
num := nums.get(1)
|
||||
fmt.Printf("Access element at index 1, get num = %v\n", num)
|
||||
|
||||
/* Add elements at the end */
|
||||
nums.set(0, 1)
|
||||
fmt.Printf("Update element at index 1 to 0, get nums = %v\n", nums.toArray())
|
||||
|
||||
/* Test capacity expansion mechanism */
|
||||
for i := 0; i < 10; i++ {
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
|
||||
nums.add(i)
|
||||
}
|
||||
fmt.Printf("After expansion, list nums = %v, capacity = %v, length = %v\n", nums.toArray(), nums.capacity(), nums.size())
|
||||
}
|
||||
Reference in New Issue
Block a user