mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-24 17:17: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,34 @@
|
||||
// File: binary_search_recur.go
|
||||
// Created Time: 2023-07-19
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
/* Binary search: problem f(i, j) */
|
||||
func dfs(nums []int, target, i, j int) int {
|
||||
// If interval is empty, indicating no target element, return -1
|
||||
if i > j {
|
||||
return -1
|
||||
}
|
||||
// Calculate midpoint index
|
||||
m := i + ((j - i) >> 1)
|
||||
// Compare midpoint with target element
|
||||
if nums[m] < target {
|
||||
// If smaller, recurse on right half of array
|
||||
// Recursion subproblem f(m+1, j)
|
||||
return dfs(nums, target, m+1, j)
|
||||
} else if nums[m] > target {
|
||||
// If larger, recurse on left half of array
|
||||
// Recursion subproblem f(i, m-1)
|
||||
return dfs(nums, target, i, m-1)
|
||||
} else {
|
||||
// Found the target element, return its index
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
/* Binary search */
|
||||
func binarySearch(nums []int, target int) int {
|
||||
n := len(nums)
|
||||
return dfs(nums, target, 0, n-1)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// File: binary_search_recur_test.go
|
||||
// Created Time: 2023-07-19
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBinarySearch(t *testing.T) {
|
||||
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||
target := 6
|
||||
noTarget := 99
|
||||
targetIndex := binarySearch(nums, target)
|
||||
fmt.Println("Index of target element 6 = ", targetIndex)
|
||||
noTargetIndex := binarySearch(nums, noTarget)
|
||||
fmt.Println("Index of non-existent target element = ", noTargetIndex)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// File: build_tree.go
|
||||
// Created Time: 2023-07-20
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import . "github.com/krahets/hello-algo/pkg"
|
||||
|
||||
/* Build binary tree: divide and conquer */
|
||||
func dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {
|
||||
// Terminate when the subtree interval is empty
|
||||
if r-l < 0 {
|
||||
return nil
|
||||
}
|
||||
// Initialize the root node
|
||||
root := NewTreeNode(preorder[i])
|
||||
// Query m to divide the left and right subtrees
|
||||
m := inorderMap[preorder[i]]
|
||||
// Subproblem: build the left subtree
|
||||
root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)
|
||||
// Subproblem: build the right subtree
|
||||
root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)
|
||||
// Return the root node
|
||||
return root
|
||||
}
|
||||
|
||||
/* Build binary tree */
|
||||
func buildTree(preorder, inorder []int) *TreeNode {
|
||||
// Initialize hash map, storing the mapping from inorder elements to indices
|
||||
inorderMap := make(map[int]int, len(inorder))
|
||||
for i := 0; i < len(inorder); i++ {
|
||||
inorderMap[inorder[i]] = i
|
||||
}
|
||||
|
||||
root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)
|
||||
return root
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// File: build_tree_test.go
|
||||
// Created Time: 2023-07-20
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestBuildTree(t *testing.T) {
|
||||
preorder := []int{3, 9, 2, 1, 7}
|
||||
inorder := []int{9, 3, 1, 2, 7}
|
||||
fmt.Print("Preorder traversal = ")
|
||||
PrintSlice(preorder)
|
||||
fmt.Print("Inorder traversal = ")
|
||||
PrintSlice(inorder)
|
||||
|
||||
root := buildTree(preorder, inorder)
|
||||
fmt.Println("The constructed binary tree is:")
|
||||
PrintTree(root)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// File: hanota.go
|
||||
// Created Time: 2023-07-21
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import "container/list"
|
||||
|
||||
/* Move a disk */
|
||||
func move(src, tar *list.List) {
|
||||
// Take out a disk from the top of src
|
||||
pan := src.Back()
|
||||
// Place the disk on top of tar
|
||||
tar.PushBack(pan.Value)
|
||||
// Remove top disk from src
|
||||
src.Remove(pan)
|
||||
}
|
||||
|
||||
/* Solve the Tower of Hanoi problem f(i) */
|
||||
func dfsHanota(i int, src, buf, tar *list.List) {
|
||||
// If there is only one disk left in src, move it directly to tar
|
||||
if i == 1 {
|
||||
move(src, tar)
|
||||
return
|
||||
}
|
||||
// Subproblem f(i-1): move the top i-1 disks from src to buf using tar
|
||||
dfsHanota(i-1, src, tar, buf)
|
||||
// Subproblem f(1): move the remaining disk from src to tar
|
||||
move(src, tar)
|
||||
// Subproblem f(i-1): move the top i-1 disks from buf to tar using src
|
||||
dfsHanota(i-1, buf, src, tar)
|
||||
}
|
||||
|
||||
/* Solve the Tower of Hanoi problem */
|
||||
func solveHanota(A, B, C *list.List) {
|
||||
n := A.Len()
|
||||
// Move the top n disks from A to C using B
|
||||
dfsHanota(n, A, B, C)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// File: hanota_test.go
|
||||
// Created Time: 2023-07-21
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestHanota(t *testing.T) {
|
||||
// The tail of the list is the top of the rod
|
||||
A := list.New()
|
||||
for i := 5; i > 0; i-- {
|
||||
A.PushBack(i)
|
||||
}
|
||||
B := list.New()
|
||||
C := list.New()
|
||||
fmt.Println("In initial state:")
|
||||
fmt.Print("A = ")
|
||||
PrintList(A)
|
||||
fmt.Print("B = ")
|
||||
PrintList(B)
|
||||
fmt.Print("C = ")
|
||||
PrintList(C)
|
||||
|
||||
solveHanota(A, B, C)
|
||||
|
||||
fmt.Println("After disk movement is complete:")
|
||||
fmt.Print("A = ")
|
||||
PrintList(A)
|
||||
fmt.Print("B = ")
|
||||
PrintList(B)
|
||||
fmt.Print("C = ")
|
||||
PrintList(C)
|
||||
}
|
||||
Reference in New Issue
Block a user