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
@@ -0,0 +1,97 @@
// File: array_hash_map.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import "fmt"
/* Key-value pair */
type pair struct {
key int
val string
}
/* Hash table based on array implementation */
type arrayHashMap struct {
buckets []*pair
}
/* Initialize hash table */
func newArrayHashMap() *arrayHashMap {
// Initialize array with 100 buckets
buckets := make([]*pair, 100)
return &arrayHashMap{buckets: buckets}
}
/* Hash function */
func (a *arrayHashMap) hashFunc(key int) int {
index := key % 100
return index
}
/* Query operation */
func (a *arrayHashMap) get(key int) string {
index := a.hashFunc(key)
pair := a.buckets[index]
if pair == nil {
return "Not Found"
}
return pair.val
}
/* Add operation */
func (a *arrayHashMap) put(key int, val string) {
pair := &pair{key: key, val: val}
index := a.hashFunc(key)
a.buckets[index] = pair
}
/* Remove operation */
func (a *arrayHashMap) remove(key int) {
index := a.hashFunc(key)
// Set to nil to delete
a.buckets[index] = nil
}
/* Get all key pairs */
func (a *arrayHashMap) pairSet() []*pair {
var pairs []*pair
for _, pair := range a.buckets {
if pair != nil {
pairs = append(pairs, pair)
}
}
return pairs
}
/* Get all keys */
func (a *arrayHashMap) keySet() []int {
var keys []int
for _, pair := range a.buckets {
if pair != nil {
keys = append(keys, pair.key)
}
}
return keys
}
/* Get all values */
func (a *arrayHashMap) valueSet() []string {
var values []string
for _, pair := range a.buckets {
if pair != nil {
values = append(values, pair.val)
}
}
return values
}
/* Print hash table */
func (a *arrayHashMap) print() {
for _, pair := range a.buckets {
if pair != nil {
fmt.Println(pair.key, "->", pair.val)
}
}
}
@@ -0,0 +1,52 @@
// File: array_hash_map_test.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import (
"fmt"
"testing"
)
func TestArrayHashMap(t *testing.T) {
/* Initialize hash table */
hmap := newArrayHashMap()
/* Add operation */
// Add key-value pair (key, value) to the hash table
hmap.put(12836, "Xiao Ha")
hmap.put(15937, "Xiao Luo")
hmap.put(16750, "Xiao Suan")
hmap.put(13276, "Xiao Fa")
hmap.put(10583, "Xiao Ya")
fmt.Println("\nAfter adding is complete, hash table is\nKey -> Value")
hmap.print()
/* Query operation */
// Input key into hash table to get value
name := hmap.get(15937)
fmt.Println("\nInput student ID 15937, query name " + name)
/* Remove operation */
// Remove key-value pair (key, value) from hash table
hmap.remove(10583)
fmt.Println("\nAfter removing 10583, hash table is\nKey -> Value")
hmap.print()
/* Traverse hash table */
fmt.Println("\nTraverse key-value pairs Key->Value")
for _, kv := range hmap.pairSet() {
fmt.Println(kv.key, " -> ", kv.val)
}
fmt.Println("\nTraverse keys only Key")
for _, key := range hmap.keySet() {
fmt.Println(key)
}
fmt.Println("\nTraverse values only Value")
for _, val := range hmap.valueSet() {
fmt.Println(val)
}
}
@@ -0,0 +1,62 @@
// File: hash_collision_test.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import (
"fmt"
"testing"
)
func TestHashMapChaining(t *testing.T) {
/* Initialize hash table */
hmap := newHashMapChaining()
/* Add operation */
// Add key-value pair (key, value) to the hash table
hmap.put(12836, "Xiao Ha")
hmap.put(15937, "Xiao Luo")
hmap.put(16750, "Xiao Suan")
hmap.put(13276, "Xiao Fa")
hmap.put(10583, "Xiao Ya")
fmt.Println("\nAfter adding is complete, hash table is\nKey -> Value")
hmap.print()
/* Query operation */
// Input key into hash table to get value
name := hmap.get(15937)
fmt.Println("\nInput student ID 15937, found name", name)
/* Remove operation */
// Remove key-value pair (key, value) from hash table
hmap.remove(12836)
fmt.Println("\nAfter removing 12836, hash table is\nKey -> Value")
hmap.print()
}
func TestHashMapOpenAddressing(t *testing.T) {
/* Initialize hash table */
hmap := newHashMapOpenAddressing()
/* Add operation */
// Add key-value pair (key, value) to the hash table
hmap.put(12836, "Xiao Ha")
hmap.put(15937, "Xiao Luo")
hmap.put(16750, "Xiao Suan")
hmap.put(13276, "Xiao Fa")
hmap.put(10583, "Xiao Ya")
fmt.Println("\nAfter adding is complete, hash table is\nKey -> Value")
hmap.print()
/* Query operation */
// Input key into hash table to get value
name := hmap.get(13276)
fmt.Println("\nInput student ID 13276, query name ", name)
/* Remove operation */
// Remove key-value pair (key, value) from hash table
hmap.remove(16750)
fmt.Println("\nAfter removing 16750, hash table is\nKey -> Value")
hmap.print()
}
@@ -0,0 +1,134 @@
// File: hash_map_chaining.go
// Created Time: 2023-06-23
// Author: Reanon (793584285@qq.com)
package chapter_hashing
import (
"fmt"
"strconv"
"strings"
)
/* Hash table with separate chaining */
type hashMapChaining struct {
size int // Number of key-value pairs
capacity int // Hash table capacity
loadThres float64 // Load factor threshold for triggering expansion
extendRatio int // Expansion multiplier
buckets [][]pair // Bucket array
}
/* Constructor */
func newHashMapChaining() *hashMapChaining {
buckets := make([][]pair, 4)
for i := 0; i < 4; i++ {
buckets[i] = make([]pair, 0)
}
return &hashMapChaining{
size: 0,
capacity: 4,
loadThres: 2.0 / 3.0,
extendRatio: 2,
buckets: buckets,
}
}
/* Hash function */
func (m *hashMapChaining) hashFunc(key int) int {
return key % m.capacity
}
/* Load factor */
func (m *hashMapChaining) loadFactor() float64 {
return float64(m.size) / float64(m.capacity)
}
/* Query operation */
func (m *hashMapChaining) get(key int) string {
idx := m.hashFunc(key)
bucket := m.buckets[idx]
// Traverse bucket, if key is found, return corresponding val
for _, p := range bucket {
if p.key == key {
return p.val
}
}
// Return empty string if key not found
return ""
}
/* Add operation */
func (m *hashMapChaining) put(key int, val string) {
// When load factor exceeds threshold, perform expansion
if m.loadFactor() > m.loadThres {
m.extend()
}
idx := m.hashFunc(key)
// Traverse bucket, if specified key is encountered, update corresponding val and return
for i := range m.buckets[idx] {
if m.buckets[idx][i].key == key {
m.buckets[idx][i].val = val
return
}
}
// If key does not exist, append key-value pair to the end
p := pair{
key: key,
val: val,
}
m.buckets[idx] = append(m.buckets[idx], p)
m.size += 1
}
/* Remove operation */
func (m *hashMapChaining) remove(key int) {
idx := m.hashFunc(key)
// Traverse bucket and remove key-value pair from it
for i, p := range m.buckets[idx] {
if p.key == key {
// Slice deletion
m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)
m.size -= 1
break
}
}
}
/* Expand hash table */
func (m *hashMapChaining) extend() {
// Temporarily store the original hash table
tmpBuckets := make([][]pair, len(m.buckets))
for i := 0; i < len(m.buckets); i++ {
tmpBuckets[i] = make([]pair, len(m.buckets[i]))
copy(tmpBuckets[i], m.buckets[i])
}
// Initialize expanded new hash table
m.capacity *= m.extendRatio
m.buckets = make([][]pair, m.capacity)
for i := 0; i < m.capacity; i++ {
m.buckets[i] = make([]pair, 0)
}
m.size = 0
// Move key-value pairs from original hash table to new hash table
for _, bucket := range tmpBuckets {
for _, p := range bucket {
m.put(p.key, p.val)
}
}
}
/* Print hash table */
func (m *hashMapChaining) print() {
var builder strings.Builder
for _, bucket := range m.buckets {
builder.WriteString("[")
for _, p := range bucket {
builder.WriteString(strconv.Itoa(p.key) + " -> " + p.val + " ")
}
builder.WriteString("]")
fmt.Println(builder.String())
builder.Reset()
}
}
@@ -0,0 +1,126 @@
// File: hash_map_open_addressing.go
// Created Time: 2023-06-23
// Author: Reanon (793584285@qq.com)
package chapter_hashing
import (
"fmt"
)
/* Hash table with open addressing */
type hashMapOpenAddressing struct {
size int // Number of key-value pairs
capacity int // Hash table capacity
loadThres float64 // Load factor threshold for triggering expansion
extendRatio int // Expansion multiplier
buckets []*pair // Bucket array
TOMBSTONE *pair // Removal marker
}
/* Constructor */
func newHashMapOpenAddressing() *hashMapOpenAddressing {
return &hashMapOpenAddressing{
size: 0,
capacity: 4,
loadThres: 2.0 / 3.0,
extendRatio: 2,
buckets: make([]*pair, 4),
TOMBSTONE: &pair{-1, "-1"},
}
}
/* Hash function */
func (h *hashMapOpenAddressing) hashFunc(key int) int {
return key % h.capacity // Calculate hash value based on key
}
/* Load factor */
func (h *hashMapOpenAddressing) loadFactor() float64 {
return float64(h.size) / float64(h.capacity) // Calculate current load factor
}
/* Search for bucket index corresponding to key */
func (h *hashMapOpenAddressing) findBucket(key int) int {
index := h.hashFunc(key) // Get initial index
firstTombstone := -1 // Record position of first TOMBSTONE encountered
for h.buckets[index] != nil {
if h.buckets[index].key == key {
if firstTombstone != -1 {
// If a removal marker was encountered before, move the key-value pair to that index
h.buckets[firstTombstone] = h.buckets[index]
h.buckets[index] = h.TOMBSTONE
return firstTombstone // Return the moved bucket index
}
return index // Return found index
}
if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {
firstTombstone = index // Record position of first deletion marker encountered
}
index = (index + 1) % h.capacity // Linear probing, wrap around to head if past tail
}
// If key does not exist, return the index for insertion
if firstTombstone != -1 {
return firstTombstone
}
return index
}
/* Query operation */
func (h *hashMapOpenAddressing) get(key int) string {
index := h.findBucket(key) // Search for bucket index corresponding to key
if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {
return h.buckets[index].val // If key-value pair is found, return corresponding val
}
return "" // Return "" if key-value pair does not exist
}
/* Add operation */
func (h *hashMapOpenAddressing) put(key int, val string) {
if h.loadFactor() > h.loadThres {
h.extend() // When load factor exceeds threshold, perform expansion
}
index := h.findBucket(key) // Search for bucket index corresponding to key
if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {
h.buckets[index] = &pair{key, val} // If key-value pair does not exist, add the key-value pair
h.size++
} else {
h.buckets[index].val = val // If key-value pair found, overwrite val
}
}
/* Remove operation */
func (h *hashMapOpenAddressing) remove(key int) {
index := h.findBucket(key) // Search for bucket index corresponding to key
if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {
h.buckets[index] = h.TOMBSTONE // If key-value pair is found, overwrite it with removal marker
h.size--
}
}
/* Expand hash table */
func (h *hashMapOpenAddressing) extend() {
oldBuckets := h.buckets // Temporarily store the original hash table
h.capacity *= h.extendRatio // Update capacity
h.buckets = make([]*pair, h.capacity) // Initialize expanded new hash table
h.size = 0 // Reset size
// Move key-value pairs from original hash table to new hash table
for _, pair := range oldBuckets {
if pair != nil && pair != h.TOMBSTONE {
h.put(pair.key, pair.val)
}
}
}
/* Print hash table */
func (h *hashMapOpenAddressing) print() {
for _, pair := range h.buckets {
if pair == nil {
fmt.Println("nil")
} else if pair == h.TOMBSTONE {
fmt.Println("TOMBSTONE")
} else {
fmt.Printf("%d -> %s\n", pair.key, pair.val)
}
}
}
@@ -0,0 +1,74 @@
// File: hash_map_test.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import (
"fmt"
"strconv"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestHashMap(t *testing.T) {
/* Initialize hash table */
hmap := make(map[int]string)
/* Add operation */
// Add key-value pair (key, value) to the hash table
hmap[12836] = "Xiao Ha"
hmap[15937] = "Xiao Luo"
hmap[16750] = "Xiao Suan"
hmap[13276] = "Xiao Fa"
hmap[10583] = "Xiao Ya"
fmt.Println("\nAfter adding is complete, hash table is\nKey -> Value")
PrintMap(hmap)
/* Query operation */
// Input key into hash table to get value
name := hmap[15937]
fmt.Println("\nInput student ID 15937, query name ", name)
/* Remove operation */
// Remove key-value pair (key, value) from hash table
delete(hmap, 10583)
fmt.Println("\nAfter removing 10583, hash table is\nKey -> Value")
PrintMap(hmap)
/* Traverse hash table */
// Traverse key-value pairs
fmt.Println("\nTraverse key-value pairs Key->Value")
for key, value := range hmap {
fmt.Println(key, "->", value)
}
// Traverse keys only
fmt.Println("\nTraverse keys only Key")
for key := range hmap {
fmt.Println(key)
}
// Traverse values only
fmt.Println("\nTraverse values only Value")
for _, value := range hmap {
fmt.Println(value)
}
}
func TestSimpleHash(t *testing.T) {
var hash int
key := "Hello Algo"
hash = addHash(key)
fmt.Println("Additive hash value is " + strconv.Itoa(hash))
hash = mulHash(key)
fmt.Println("Multiplicative hash value is " + strconv.Itoa(hash))
hash = xorHash(key)
fmt.Println("XOR hash value is " + strconv.Itoa(hash))
hash = rotHash(key)
fmt.Println("Rotational hash value is " + strconv.Itoa(hash))
}
@@ -0,0 +1,55 @@
// File: simple_hash.go
// Created Time: 2023-06-23
// Author: Reanon (793584285@qq.com)
package chapter_hashing
import "fmt"
/* Additive hash */
func addHash(key string) int {
var hash int64
var modulus int64
modulus = 1000000007
for _, b := range []byte(key) {
hash = (hash + int64(b)) % modulus
}
return int(hash)
}
/* Multiplicative hash */
func mulHash(key string) int {
var hash int64
var modulus int64
modulus = 1000000007
for _, b := range []byte(key) {
hash = (31*hash + int64(b)) % modulus
}
return int(hash)
}
/* XOR hash */
func xorHash(key string) int {
hash := 0
modulus := 1000000007
for _, b := range []byte(key) {
fmt.Println(int(b))
hash ^= int(b)
hash = (31*hash + int(b)) % modulus
}
return hash & modulus
}
/* Rotational hash */
func rotHash(key string) int {
var hash int64
var modulus int64
modulus = 1000000007
for _, b := range []byte(key) {
hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus
}
return int(hash)
}