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.dart
* Created Time: 2023-03-29
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Key-value pair */
class Pair {
int key;
String val;
Pair(this.key, this.val);
}
/* Hash table based on array implementation */
class ArrayHashMap {
late List<Pair?> _buckets;
ArrayHashMap() {
// Initialize array with 100 buckets
_buckets = List.filled(100, null);
}
/* Hash function */
int _hashFunc(int key) {
final int index = key % 100;
return index;
}
/* Query operation */
String? get(int key) {
final int index = _hashFunc(key);
final Pair? pair = _buckets[index];
if (pair == null) {
return null;
}
return pair.val;
}
/* Add operation */
void put(int key, String val) {
final Pair pair = Pair(key, val);
final int index = _hashFunc(key);
_buckets[index] = pair;
}
/* Remove operation */
void remove(int key) {
final int index = _hashFunc(key);
_buckets[index] = null;
}
/* Get all key-value pairs */
List<Pair> pairSet() {
List<Pair> pairSet = [];
for (final Pair? pair in _buckets) {
if (pair != null) {
pairSet.add(pair);
}
}
return pairSet;
}
/* Get all keys */
List<int> keySet() {
List<int> keySet = [];
for (final Pair? pair in _buckets) {
if (pair != null) {
keySet.add(pair.key);
}
}
return keySet;
}
/* Get all values */
List<String> values() {
List<String> valueSet = [];
for (final Pair? pair in _buckets) {
if (pair != null) {
valueSet.add(pair.val);
}
}
return valueSet;
}
/* Print hash table */
void printHashMap() {
for (final Pair kv in pairSet()) {
print("${kv.key} -> ${kv.val}");
}
}
}
/* Driver Code */
void main() {
/* Initialize hash table */
final ArrayHashMap 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");
print("\nAfter adding is complete, hash table is\nKey -> Value");
map.printHashMap();
/* Query operation */
// Input key into hash table to get value
String? name = map.get(15937);
print("\nInput student ID 15937, found name $name");
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(10583);
print("\nAfter removing 10583, hash table is\nKey -> Value");
map.printHashMap();
/* Traverse hash table */
print("\nTraverse key-value pairs Key->Value");
map.pairSet().forEach((kv) => print("${kv.key} -> ${kv.val}"));
print("\nTraverse keys only Key");
map.keySet().forEach((key) => print("$key"));
print("\nTraverse values only Value");
map.values().forEach((val) => print("$val"));
}
@@ -0,0 +1,34 @@
/**
* File: built_in_hash.dart
* Created Time: 2023-06-25
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../chapter_stack_and_queue/linkedlist_deque.dart';
/* Driver Code */
void main() {
int _num = 3;
int hashNum = _num.hashCode;
print("Hash value of integer $_num is $hashNum");
bool bol = true;
int hashBol = bol.hashCode;
print("Hash value of boolean $bol is $hashBol");
double dec = 3.14159;
int hashDec = dec.hashCode;
print("Hash value of decimal $dec is $hashDec");
String str = "Hello Algo";
int hashStr = str.hashCode;
print("Hash value of string $str is $hashStr");
List arr = [12836, "Xiao Ha"];
int hashArr = arr.hashCode;
print("Hash value of array $arr is $hashArr");
ListNode obj = new ListNode(0);
int hashObj = obj.hashCode;
print("Hash value of node object $obj is $hashObj");
}
@@ -0,0 +1,41 @@
/**
* File: hash_map.dart
* Created Time: 2023-03-29
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Driver Code */
void main() {
/* Initialize hash table */
final Map<int, String> map = {};
/* 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";
print("\nAfter adding is complete, hash table is\nKey -> Value");
map.forEach((key, value) => print("$key -> $value"));
/* Query operation */
// Input key into hash table to get value
final String? name = map[15937];
print("\nInput student ID 15937, found name $name");
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(10583);
print("\nAfter removing 10583, hash table is\nKey -> Value");
map.forEach((key, value) => print("$key -> $value"));
/* Traverse hash table */
print("\nTraverse key-value pairs Key->Value");
map.forEach((key, value) => print("$key -> $value"));
print("\nTraverse keys only Key");
map.keys.forEach((key) => print(key));
print("\nTraverse values only Value");
map.forEach((key, value) => print("$value"));
map.values.forEach((value) => print(value));
}
@@ -0,0 +1,138 @@
/**
* File: hash_map_chaining.dart
* Created Time: 2023-06-24
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import 'array_hash_map.dart';
/* Hash table with separate chaining */
class HashMapChaining {
late int size; // Number of key-value pairs
late int capacity; // Hash table capacity
late double loadThres; // Load factor threshold for triggering expansion
late int extendRatio; // Expansion multiplier
late List<List<Pair>> buckets; // Bucket array
/* Constructor */
HashMapChaining() {
size = 0;
capacity = 4;
loadThres = 2.0 / 3.0;
extendRatio = 2;
buckets = List.generate(capacity, (_) => []);
}
/* Hash function */
int hashFunc(int key) {
return key % capacity;
}
/* Load factor */
double loadFactor() {
return size / capacity;
}
/* Query operation */
String? get(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets[index];
// Traverse bucket, if key is found, return corresponding val
for (Pair pair in bucket) {
if (pair.key == key) {
return pair.val;
}
}
// If key is not found, return null
return null;
}
/* Add operation */
void put(int key, String val) {
// When load factor exceeds threshold, perform expansion
if (loadFactor() > loadThres) {
extend();
}
int index = hashFunc(key);
List<Pair> bucket = buckets[index];
// Traverse bucket, if specified key is encountered, update corresponding val and return
for (Pair pair in bucket) {
if (pair.key == key) {
pair.val = val;
return;
}
}
// If key does not exist, append key-value pair to the end
Pair pair = Pair(key, val);
bucket.add(pair);
size++;
}
/* Remove operation */
void remove(int key) {
int index = hashFunc(key);
List<Pair> bucket = buckets[index];
// Traverse bucket and remove key-value pair from it
for (Pair pair in bucket) {
if (pair.key == key) {
bucket.remove(pair);
size--;
break;
}
}
}
/* Expand hash table */
void extend() {
// Temporarily store the original hash table
List<List<Pair>> bucketsTmp = buckets;
// Initialize expanded new hash table
capacity *= extendRatio;
buckets = List.generate(capacity, (_) => []);
size = 0;
// Move key-value pairs from original hash table to new hash table
for (List<Pair> bucket in bucketsTmp) {
for (Pair pair in bucket) {
put(pair.key, pair.val);
}
}
}
/* Print hash table */
void printHashMap() {
for (List<Pair> bucket in buckets) {
List<String> res = [];
for (Pair pair in bucket) {
res.add("${pair.key} -> ${pair.val}");
}
print(res);
}
}
}
/* Driver Code */
void main() {
/* Initialize hash table */
HashMapChaining 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");
print("\nAfter adding is complete, hash table is\nKey -> Value");
map.printHashMap();
/* Query operation */
// Input key into hash table to get value
String? name = map.get(13276);
print("\nInput student ID 13276, found name ${name}");
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(12836);
print("\nAfter removing 12836, hash table is\nKey -> Value");
map.printHashMap();
}
@@ -0,0 +1,157 @@
/**
* File: hash_map_open_addressing.dart
* Created Time: 2023-06-25
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import 'array_hash_map.dart';
/* Hash table with open addressing */
class HashMapOpenAddressing {
late int _size; // Number of key-value pairs
int _capacity = 4; // Hash table capacity
double _loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
int _extendRatio = 2; // Expansion multiplier
late List<Pair?> _buckets; // Bucket array
Pair _TOMBSTONE = Pair(-1, "-1"); // Removal marker
/* Constructor */
HashMapOpenAddressing() {
_size = 0;
_buckets = List.generate(_capacity, (index) => null);
}
/* Hash function */
int hashFunc(int key) {
return key % _capacity;
}
/* Load factor */
double loadFactor() {
return _size / _capacity;
}
/* Search for bucket index corresponding to key */
int findBucket(int key) {
int index = hashFunc(key);
int 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 firstTombstone == -1 ? index : firstTombstone;
}
/* Query operation */
String? get(int key) {
// Search for bucket index corresponding to key
int 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 */
void put(int key, String val) {
// When load factor exceeds threshold, perform expansion
if (loadFactor() > _loadThres) {
extend();
}
// Search for bucket index corresponding to key
int 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] = new Pair(key, val);
_size++;
}
/* Remove operation */
void remove(int key) {
// Search for bucket index corresponding to key
int 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 */
void extend() {
// Temporarily store the original hash table
List<Pair?> bucketsTmp = _buckets;
// Initialize expanded new hash table
_capacity *= _extendRatio;
_buckets = List.generate(_capacity, (index) => null);
_size = 0;
// Move key-value pairs from original hash table to new hash table
for (Pair? pair in bucketsTmp) {
if (pair != null && pair != _TOMBSTONE) {
put(pair.key, pair.val);
}
}
}
/* Print hash table */
void printHashMap() {
for (Pair? pair in _buckets) {
if (pair == null) {
print("null");
} else if (pair == _TOMBSTONE) {
print("TOMBSTONE");
} else {
print("${pair.key} -> ${pair.val}");
}
}
}
}
/* Driver Code */
void main() {
/* Initialize hash table */
HashMapOpenAddressing map = HashMapOpenAddressing();
/* 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");
print("\nAfter adding is complete, hash table is\nKey -> Value");
map.printHashMap();
/* Query operation */
// Input key into hash table to get value
String? name = map.get(13276);
print("\nInput student ID 13276, found name $name");
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(16750);
print("\nAfter removing 16750, hash table is\nKey -> Value");
map.printHashMap();
}
@@ -0,0 +1,62 @@
/**
* File: simple_hash.dart
* Created Time: 2023-06-25
* Author: liuyuxin (gvenusleo@gmail.com)
*/
/* Additive hash */
int addHash(String key) {
int hash = 0;
final int MODULUS = 1000000007;
for (int i = 0; i < key.length; i++) {
hash = (hash + key.codeUnitAt(i)) % MODULUS;
}
return hash;
}
/* Multiplicative hash */
int mulHash(String key) {
int hash = 0;
final int MODULUS = 1000000007;
for (int i = 0; i < key.length; i++) {
hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;
}
return hash;
}
/* XOR hash */
int xorHash(String key) {
int hash = 0;
final int MODULUS = 1000000007;
for (int i = 0; i < key.length; i++) {
hash ^= key.codeUnitAt(i);
}
return hash & MODULUS;
}
/* Rotational hash */
int rotHash(String key) {
int hash = 0;
final int MODULUS = 1000000007;
for (int i = 0; i < key.length; i++) {
hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;
}
return hash;
}
/* Dirver Code */
void main() {
String key = "Hello Algo";
int hash = addHash(key);
print("Additive hash value is $hash");
hash = mulHash(key);
print("Multiplicative hash value is $hash");
hash = xorHash(key);
print("XOR hash value is $hash");
hash = rotHash(key);
print("Rotational hash value is $hash");
}