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,126 @@
/**
* File: array_hash_map.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_hashing
/* Key-value pair */
class Pair(
var key: Int,
var _val: String
)
/* Hash table based on array implementation */
class ArrayHashMap {
// Initialize array with 100 buckets
private val buckets = arrayOfNulls<Pair>(100)
/* Hash function */
fun hashFunc(key: Int): Int {
val index = key % 100
return index
}
/* Query operation */
fun get(key: Int): String? {
val index = hashFunc(key)
val pair = buckets[index] ?: return null
return pair._val
}
/* Add operation */
fun put(key: Int, _val: String) {
val pair = Pair(key, _val)
val index = hashFunc(key)
buckets[index] = pair
}
/* Remove operation */
fun remove(key: Int) {
val index = hashFunc(key)
// Set to null to represent deletion
buckets[index] = null
}
/* Get all key-value pairs */
fun pairSet(): MutableList<Pair> {
val pairSet = mutableListOf<Pair>()
for (pair in buckets) {
if (pair != null)
pairSet.add(pair)
}
return pairSet
}
/* Get all keys */
fun keySet(): MutableList<Int> {
val keySet = mutableListOf<Int>()
for (pair in buckets) {
if (pair != null)
keySet.add(pair.key)
}
return keySet
}
/* Get all values */
fun valueSet(): MutableList<String> {
val valueSet = mutableListOf<String>()
for (pair in buckets) {
if (pair != null)
valueSet.add(pair._val)
}
return valueSet
}
/* Print hash table */
fun print() {
for (kv in pairSet()) {
val key = kv.key
val _val = kv._val
println("$key -> $_val")
}
}
}
/* Driver Code */
fun main() {
/* Initialize hash table */
val map = ArrayHashMap()
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, "Xiao Ha")
map.put(15937, "Xiao Luo")
map.put(16750, "Xiao Suan")
map.put(13276, "Xiao Fa")
map.put(10583, "Xiao Ya")
println("\nAfter adding is complete, hash table is\nKey -> Value")
map.print()
/* Query operation */
// Input key into hash table to get value
val name = map.get(15937)
println("\nInput student ID 15937, found name $name")
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(10583)
println("\nAfter removing 10583, hash table is\nKey -> Value")
map.print()
/* Traverse hash table */
println("\nTraverse key-value pairs Key -> Value")
for (kv in map.pairSet()) {
println("${kv.key} -> ${kv._val}")
}
println("\nTraverse keys only Key")
for (key in map.keySet()) {
println(key)
}
println("\nTraverse values only Value")
for (_val in map.valueSet()) {
println(_val)
}
}
@@ -0,0 +1,36 @@
/**
* File: built_in_hash.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_hashing
import utils.ListNode
/* Driver Code */
fun main() {
val num = 3
val hashNum = num.hashCode()
println("Hash value of integer $num is $hashNum")
val bol = true
val hashBol = bol.hashCode()
println("Hash value of boolean $bol is $hashBol")
val dec = 3.14159
val hashDec = dec.hashCode()
println("Hash value of decimal $dec is $hashDec")
val str = "Hello Algo"
val hashStr = str.hashCode()
println("Hash value of string $str is $hashStr")
val arr = arrayOf<Any>(12836, "Xiao Ha")
val hashTup = arr.contentHashCode()
println("Hash value of array ${arr.contentToString()} is $hashTup")
val obj = ListNode(0)
val hashObj = obj.hashCode()
println("Hash value of node object $obj is $hashObj")
}
@@ -0,0 +1,50 @@
/**
* File: hash_map.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_hashing
import utils.printHashMap
/* Driver Code */
fun main() {
/* Initialize hash table */
val map = HashMap<Int, String>()
/* Add operation */
// Add key-value pair (key, value) to the hash table
map[12836] = "Xiao Ha"
map[15937] = "Xiao Luo"
map[16750] = "Xiao Suan"
map[13276] = "Xiao Fa"
map[10583] = "Xiao Ya"
println("\nAfter adding is complete, hash table is\nKey -> Value")
printHashMap(map)
/* Query operation */
// Input key into hash table to get value
val name = map[15937]
println("\nInput student ID 15937, found name $name")
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(10583)
println("\nAfter removing 10583, hash table is\nKey -> Value")
printHashMap(map)
/* Traverse hash table */
println("\nTraverse key-value pairs Key->Value")
for ((key, value) in map) {
println("$key -> $value")
}
println("\nTraverse keys only Key")
for (key in map.keys) {
println(key)
}
println("\nTraverse values only Value")
for (_val in map.values) {
println(_val)
}
}
@@ -0,0 +1,145 @@
/**
* File: hash_map_chaining.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_hashing
/* Hash table with separate chaining */
class HashMapChaining {
var size: Int // Number of key-value pairs
var capacity: Int // Hash table capacity
val loadThres: Double // Load factor threshold for triggering expansion
val extendRatio: Int // Expansion multiplier
var buckets: MutableList<MutableList<Pair>> // Bucket array
/* Constructor */
init {
size = 0
capacity = 4
loadThres = 2.0 / 3.0
extendRatio = 2
buckets = mutableListOf()
for (i in 0..<capacity) {
buckets.add(mutableListOf())
}
}
/* Hash function */
fun hashFunc(key: Int): Int {
return key % capacity
}
/* Load factor */
fun loadFactor(): Double {
return (size / capacity).toDouble()
}
/* Query operation */
fun get(key: Int): String? {
val index = hashFunc(key)
val bucket = buckets[index]
// Traverse bucket, if key is found, return corresponding val
for (pair in bucket) {
if (pair.key == key) return pair._val
}
// If key is not found, return null
return null
}
/* Add operation */
fun put(key: Int, _val: String) {
// When load factor exceeds threshold, perform expansion
if (loadFactor() > loadThres) {
extend()
}
val index = hashFunc(key)
val bucket = buckets[index]
// Traverse bucket, if specified key is encountered, update corresponding val and return
for (pair in bucket) {
if (pair.key == key) {
pair._val = _val
return
}
}
// If key does not exist, append key-value pair to the end
val pair = Pair(key, _val)
bucket.add(pair)
size++
}
/* Remove operation */
fun remove(key: Int) {
val index = hashFunc(key)
val bucket = buckets[index]
// Traverse bucket and remove key-value pair from it
for (pair in bucket) {
if (pair.key == key) {
bucket.remove(pair)
size--
break
}
}
}
/* Expand hash table */
fun extend() {
// Temporarily store the original hash table
val bucketsTmp = buckets
// Initialize expanded new hash table
capacity *= extendRatio
// mutablelist has no fixed size
buckets = mutableListOf()
for (i in 0..<capacity) {
buckets.add(mutableListOf())
}
size = 0
// Move key-value pairs from original hash table to new hash table
for (bucket in bucketsTmp) {
for (pair in bucket) {
put(pair.key, pair._val)
}
}
}
/* Print hash table */
fun print() {
for (bucket in buckets) {
val res = mutableListOf<String>()
for (pair in bucket) {
val k = pair.key
val v = pair._val
res.add("$k -> $v")
}
println(res)
}
}
}
/* Driver Code */
fun main() {
/* Initialize hash table */
val map = HashMapChaining()
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, "Xiao Ha")
map.put(15937, "Xiao Luo")
map.put(16750, "Xiao Suan")
map.put(13276, "Xiao Fa")
map.put(10583, "Xiao Ya")
println("\nAfter adding is complete, hash table is\nKey -> Value")
map.print()
/* Query operation */
// Input key into hash table to get value
val name = map.get(13276)
println("\nInput student ID 13276, found name $name")
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(12836)
println("\nAfter removing 12836, hash table is\nKey -> Value")
map.print()
}
@@ -0,0 +1,161 @@
/**
* File: hash_map_open_addressing.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_hashing
/* Hash table with open addressing */
class HashMapOpenAddressing {
private var size: Int // Number of key-value pairs
private var capacity: Int // Hash table capacity
private val loadThres: Double // Load factor threshold for triggering expansion
private val extendRatio: Int // Expansion multiplier
private var buckets: Array<Pair?> // Bucket array
private val TOMBSTONE: Pair // Removal marker
/* Constructor */
init {
size = 0
capacity = 4
loadThres = 2.0 / 3.0
extendRatio = 2
buckets = arrayOfNulls(capacity)
TOMBSTONE = Pair(-1, "-1")
}
/* Hash function */
fun hashFunc(key: Int): Int {
return key % capacity
}
/* Load factor */
fun loadFactor(): Double {
return (size / capacity).toDouble()
}
/* Search for bucket index corresponding to key */
fun findBucket(key: Int): Int {
var index = hashFunc(key)
var firstTombstone = -1
// Linear probing, break when encountering an empty bucket
while (buckets[index] != null) {
// If key is encountered, return the corresponding bucket index
if (buckets[index]?.key == key) {
// If a removal marker was encountered before, move the key-value pair to that index
if (firstTombstone != -1) {
buckets[firstTombstone] = buckets[index]
buckets[index] = TOMBSTONE
return firstTombstone // Return the moved bucket index
}
return index // Return bucket index
}
// Record the first removal marker encountered
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
firstTombstone = index
}
// Calculate bucket index, wrap around to the head if past the tail
index = (index + 1) % capacity
}
// If key does not exist, return the index for insertion
return if (firstTombstone == -1) index else firstTombstone
}
/* Query operation */
fun get(key: Int): String? {
// Search for bucket index corresponding to key
val index = findBucket(key)
// If key-value pair is found, return corresponding val
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
return buckets[index]?._val
}
// If key-value pair does not exist, return null
return null
}
/* Add operation */
fun put(key: Int, _val: String) {
// When load factor exceeds threshold, perform expansion
if (loadFactor() > loadThres) {
extend()
}
// Search for bucket index corresponding to key
val index = findBucket(key)
// If key-value pair is found, overwrite val and return
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index]!!._val = _val
return
}
// If key-value pair does not exist, add the key-value pair
buckets[index] = Pair(key, _val)
size++
}
/* Remove operation */
fun remove(key: Int) {
// Search for bucket index corresponding to key
val index = findBucket(key)
// If key-value pair is found, overwrite it with removal marker
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
buckets[index] = TOMBSTONE
size--
}
}
/* Expand hash table */
fun extend() {
// Temporarily store the original hash table
val bucketsTmp = buckets
// Initialize expanded new hash table
capacity *= extendRatio
buckets = arrayOfNulls(capacity)
size = 0
// Move key-value pairs from original hash table to new hash table
for (pair in bucketsTmp) {
if (pair != null && pair != TOMBSTONE) {
put(pair.key, pair._val)
}
}
}
/* Print hash table */
fun print() {
for (pair in buckets) {
if (pair == null) {
println("null")
} else if (pair == TOMBSTONE) {
println("TOMESTOME")
} else {
println("${pair.key} -> ${pair._val}")
}
}
}
}
/* Driver Code */
fun main() {
// Initialize hash table
val hashmap = HashMapOpenAddressing()
// Add operation
// Add key-value pair (key, val) to the hash table
hashmap.put(12836, "Xiao Ha")
hashmap.put(15937, "Xiao Luo")
hashmap.put(16750, "Xiao Suan")
hashmap.put(13276, "Xiao Fa")
hashmap.put(10583, "Xiao Ya")
println("\nAfter adding is complete, hash table is\nKey -> Value")
hashmap.print()
// Query operation
// Input key into hash table to get value val
val name = hashmap.get(13276)
println("\nInput student ID 13276, found name $name")
// Remove operation
// Remove key-value pair (key, val) from hash table
hashmap.remove(16750)
println("\nAfter removing 16750, hash table is\nKey -> Value")
hashmap.print()
}
@@ -0,0 +1,64 @@
/**
* File: simple_hash.kt
* Created Time: 2024-01-25
* Author: curtishd (1023632660@qq.com)
*/
package chapter_hashing
/* Additive hash */
fun addHash(key: String): Int {
var hash = 0L
val MODULUS = 1000000007
for (c in key.toCharArray()) {
hash = (hash + c.code) % MODULUS
}
return hash.toInt()
}
/* Multiplicative hash */
fun mulHash(key: String): Int {
var hash = 0L
val MODULUS = 1000000007
for (c in key.toCharArray()) {
hash = (31 * hash + c.code) % MODULUS
}
return hash.toInt()
}
/* XOR hash */
fun xorHash(key: String): Int {
var hash = 0
val MODULUS = 1000000007
for (c in key.toCharArray()) {
hash = hash xor c.code
}
return hash and MODULUS
}
/* Rotational hash */
fun rotHash(key: String): Int {
var hash = 0L
val MODULUS = 1000000007
for (c in key.toCharArray()) {
hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS
}
return hash.toInt()
}
/* Driver Code */
fun main() {
val key = "Hello Algo"
var hash = addHash(key)
println("Additive hash value is $hash")
hash = mulHash(key)
println("Multiplicative hash value is $hash")
hash = xorHash(key)
println("XOR hash value is $hash")
hash = rotHash(key)
println("Rotational hash value is $hash")
}