mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 22:46:07 +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:
@@ -24,7 +24,7 @@ class ArrayHashMap {
|
||||
|
||||
public:
|
||||
ArrayHashMap() {
|
||||
// Initialize an array, containing 100 buckets
|
||||
// Initialize array with 100 buckets
|
||||
buckets = vector<Pair *>(100);
|
||||
}
|
||||
|
||||
@@ -107,4 +107,4 @@ class ArrayHashMap {
|
||||
}
|
||||
};
|
||||
|
||||
// See test case in array_hash_map_test.cpp
|
||||
// See array_hash_map_test.cpp for test cases
|
||||
|
||||
@@ -13,23 +13,23 @@ int main() {
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
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");
|
||||
cout << "\nAfter adding is complete, hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
// Input key into hash table to get value
|
||||
string name = map.get(15937);
|
||||
cout << "\nEnter student ID 15937, found name " << name << endl;
|
||||
cout << "\nInput student ID 15937, query name " << name << endl;
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(10583);
|
||||
cout << "\nAfter removing 10583, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\nAfter removing 10583, hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* Traverse hash table */
|
||||
@@ -38,12 +38,12 @@ int main() {
|
||||
cout << kv->key << " -> " << kv->val << endl;
|
||||
}
|
||||
|
||||
cout << "\nIndividually traverse keys Key" << endl;
|
||||
cout << "\nTraverse keys only Key" << endl;
|
||||
for (auto key : map.keySet()) {
|
||||
cout << key << endl;
|
||||
}
|
||||
|
||||
cout << "\nIndividually traverse values Value" << endl;
|
||||
cout << "\nTraverse values only Value" << endl;
|
||||
for (auto val : map.valueSet()) {
|
||||
cout << val << endl;
|
||||
}
|
||||
|
||||
@@ -10,20 +10,20 @@
|
||||
int main() {
|
||||
int num = 3;
|
||||
size_t hashNum = hash<int>()(num);
|
||||
cout << "The hash value of integer " << num << " is " << hashNum << "\n";
|
||||
cout << "Hash value of integer " << num << " is " << hashNum << "\n";
|
||||
|
||||
bool bol = true;
|
||||
size_t hashBol = hash<bool>()(bol);
|
||||
cout << "The hash value of boolean " << bol << " is " << hashBol << "\n";
|
||||
cout << "Hash value of boolean " << bol << " is " << hashBol << "\n";
|
||||
|
||||
double dec = 3.14159;
|
||||
size_t hashDec = hash<double>()(dec);
|
||||
cout << "The hash value of decimal " << dec << " is " << hashDec << "\n";
|
||||
cout << "Hash value of decimal " << dec << " is " << hashDec << "\n";
|
||||
|
||||
string str = "Hello algorithm";
|
||||
string str = "Hello Algo";
|
||||
size_t hashStr = hash<string>()(str);
|
||||
cout << "The hash value of string " << str << " is " << hashStr << "\n";
|
||||
cout << "Hash value of string " << str << " is " << hashStr << "\n";
|
||||
|
||||
// In C++, the built-in std:hash() only provides hash values for basic data types
|
||||
// Hash value calculation for arrays and objects must be implemented manually
|
||||
// In C++, built-in std::hash() only provides hash calculation for basic data types
|
||||
// Hash calculation for arrays and objects needs to be implemented manually
|
||||
}
|
||||
|
||||
@@ -13,23 +13,23 @@ int main() {
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map[12836] = "Ha";
|
||||
map[15937] = "Luo";
|
||||
map[16750] = "Suan";
|
||||
map[13276] = "Fa";
|
||||
map[10583] = "Ya";
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
map[12836] = "Xiao Ha";
|
||||
map[15937] = "Xiao Luo";
|
||||
map[16750] = "Xiao Suan";
|
||||
map[13276] = "Xiao Fa";
|
||||
map[10583] = "Xiao Ya";
|
||||
cout << "\nAfter adding is complete, hash table is\nKey -> Value" << endl;
|
||||
printHashMap(map);
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
// Input key into hash table to get value
|
||||
string name = map[15937];
|
||||
cout << "\nEnter student ID 15937, found name " << name << endl;
|
||||
cout << "\nInput student ID 15937, query name " << name << endl;
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.erase(10583);
|
||||
cout << "\nAfter removing 10583, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\nAfter removing 10583, hash table is\nKey -> Value" << endl;
|
||||
printHashMap(map);
|
||||
|
||||
/* Traverse hash table */
|
||||
@@ -37,7 +37,7 @@ int main() {
|
||||
for (auto kv : map) {
|
||||
cout << kv.first << " -> " << kv.second << endl;
|
||||
}
|
||||
cout << "\nIterate through Key->Value using an iterator" << endl;
|
||||
cout << "\nTraverse Key->Value using iterator" << endl;
|
||||
for (auto iter = map.begin(); iter != map.end(); iter++) {
|
||||
cout << iter->first << "->" << iter->second << endl;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "./array_hash_map.cpp"
|
||||
|
||||
/* Chained address hash table */
|
||||
/* Hash table with separate chaining */
|
||||
class HashMapChaining {
|
||||
private:
|
||||
int size; // Number of key-value pairs
|
||||
@@ -44,31 +44,31 @@ class HashMapChaining {
|
||||
/* Query operation */
|
||||
string get(int key) {
|
||||
int index = hashFunc(key);
|
||||
// Traverse the bucket, if the key is found, return the corresponding val
|
||||
// Traverse bucket, if key is found, return corresponding val
|
||||
for (Pair *pair : buckets[index]) {
|
||||
if (pair->key == key) {
|
||||
return pair->val;
|
||||
}
|
||||
}
|
||||
// If key not found, return an empty string
|
||||
// Return empty string if key not found
|
||||
return "";
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
void put(int key, string val) {
|
||||
// When the load factor exceeds the threshold, perform expansion
|
||||
// When load factor exceeds threshold, perform expansion
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
int index = hashFunc(key);
|
||||
// Traverse the bucket, if the specified key is encountered, update the corresponding val and return
|
||||
// Traverse bucket, if specified key is encountered, update corresponding val and return
|
||||
for (Pair *pair : buckets[index]) {
|
||||
if (pair->key == key) {
|
||||
pair->val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// If the key is not found, add the key-value pair to the end
|
||||
// If key does not exist, append key-value pair to the end
|
||||
buckets[index].push_back(new Pair(key, val));
|
||||
size++;
|
||||
}
|
||||
@@ -77,11 +77,11 @@ class HashMapChaining {
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
auto &bucket = buckets[index];
|
||||
// Traverse the bucket, remove the key-value pair from it
|
||||
// Traverse bucket and remove key-value pair from it
|
||||
for (int i = 0; i < bucket.size(); i++) {
|
||||
if (bucket[i]->key == key) {
|
||||
Pair *tmp = bucket[i];
|
||||
bucket.erase(bucket.begin() + i); // Remove key-value pair
|
||||
bucket.erase(bucket.begin() + i); // Remove key-value pair from it
|
||||
delete tmp; // Free memory
|
||||
size--;
|
||||
return;
|
||||
@@ -89,16 +89,16 @@ class HashMapChaining {
|
||||
}
|
||||
}
|
||||
|
||||
/* Extend hash table */
|
||||
/* Expand hash table */
|
||||
void extend() {
|
||||
// Temporarily store the original hash table
|
||||
vector<vector<Pair *>> bucketsTmp = buckets;
|
||||
// Initialize the extended new hash table
|
||||
// Initialize expanded new hash table
|
||||
capacity *= extendRatio;
|
||||
buckets.clear();
|
||||
buckets.resize(capacity);
|
||||
size = 0;
|
||||
// Move key-value pairs from the original hash table to the new hash table
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
for (auto &bucket : bucketsTmp) {
|
||||
for (Pair *pair : bucket) {
|
||||
put(pair->key, pair->val);
|
||||
@@ -127,23 +127,23 @@ int main() {
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
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");
|
||||
cout << "\nAfter adding is complete, hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
// Input key into hash table to get value
|
||||
string name = map.get(13276);
|
||||
cout << "\nEnter student ID 13276, found name " << name << endl;
|
||||
cout << "\nInput student ID 13276, query name " << name << endl;
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(12836);
|
||||
cout << "\nAfter removing 12836, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\nAfter removing 12836, hash table is\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "./array_hash_map.cpp"
|
||||
|
||||
/* Open addressing hash table */
|
||||
/* Hash table with open addressing */
|
||||
class HashMapOpenAddressing {
|
||||
private:
|
||||
int size; // Number of key-value pairs
|
||||
@@ -14,7 +14,7 @@ class HashMapOpenAddressing {
|
||||
const double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
|
||||
const int extendRatio = 2; // Expansion multiplier
|
||||
vector<Pair *> buckets; // Bucket array
|
||||
Pair *TOMBSTONE = new Pair(-1, "-1"); // Removal mark
|
||||
Pair *TOMBSTONE = new Pair(-1, "-1"); // Removal marker
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -41,15 +41,15 @@ class HashMapOpenAddressing {
|
||||
return (double)size / capacity;
|
||||
}
|
||||
|
||||
/* Search for the bucket index corresponding to key */
|
||||
/* 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] != nullptr) {
|
||||
// If the key is encountered, return the corresponding bucket index
|
||||
// If key is encountered, return the corresponding bucket index
|
||||
if (buckets[index]->key == key) {
|
||||
// If a removal mark was encountered earlier, move the key-value pair to that index
|
||||
// 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;
|
||||
@@ -57,52 +57,52 @@ class HashMapOpenAddressing {
|
||||
}
|
||||
return index; // Return bucket index
|
||||
}
|
||||
// Record the first encountered removal mark
|
||||
// Record the first removal marker encountered
|
||||
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// Calculate the bucket index, return to the head if exceeding the tail
|
||||
// Calculate bucket index, wrap around to the head if past the tail
|
||||
index = (index + 1) % capacity;
|
||||
}
|
||||
// If the key does not exist, return the index of the insertion point
|
||||
// If key does not exist, return the index for insertion
|
||||
return firstTombstone == -1 ? index : firstTombstone;
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
string get(int key) {
|
||||
// Search for the bucket index corresponding to key
|
||||
// Search for bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, return the corresponding val
|
||||
// If key-value pair is found, return corresponding val
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index]->val;
|
||||
}
|
||||
// If key-value pair does not exist, return an empty string
|
||||
// Return empty string if key-value pair does not exist
|
||||
return "";
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
void put(int key, string val) {
|
||||
// When the load factor exceeds the threshold, perform expansion
|
||||
// When load factor exceeds threshold, perform expansion
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
// Search for the bucket index corresponding to key
|
||||
// Search for bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, overwrite val and return
|
||||
// If key-value pair is found, overwrite val and return
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
buckets[index]->val = val;
|
||||
return;
|
||||
}
|
||||
// If the key-value pair does not exist, add the key-value pair
|
||||
// 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 the bucket index corresponding to key
|
||||
// Search for bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, cover it with a removal mark
|
||||
// If key-value pair is found, overwrite it with removal marker
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
delete buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
@@ -110,15 +110,15 @@ class HashMapOpenAddressing {
|
||||
}
|
||||
}
|
||||
|
||||
/* Extend hash table */
|
||||
/* Expand hash table */
|
||||
void extend() {
|
||||
// Temporarily store the original hash table
|
||||
vector<Pair *> bucketsTmp = buckets;
|
||||
// Initialize the extended new hash table
|
||||
// Initialize expanded new hash table
|
||||
capacity *= extendRatio;
|
||||
buckets = vector<Pair *>(capacity, nullptr);
|
||||
size = 0;
|
||||
// Move key-value pairs from the original hash table to the new hash table
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
for (Pair *pair : bucketsTmp) {
|
||||
if (pair != nullptr && pair != TOMBSTONE) {
|
||||
put(pair->key, pair->val);
|
||||
@@ -148,23 +148,23 @@ int main() {
|
||||
|
||||
// Add operation
|
||||
// Add key-value pair (key, val) to the hash table
|
||||
hashmap.put(12836, "Ha");
|
||||
hashmap.put(15937, "Luo");
|
||||
hashmap.put(16750, "Suan");
|
||||
hashmap.put(13276, "Fa");
|
||||
hashmap.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
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");
|
||||
cout << "\nAfter adding is complete, hash table is\nKey -> Value" << endl;
|
||||
hashmap.print();
|
||||
|
||||
// Query operation
|
||||
// Enter key to the hash table, get value val
|
||||
// Input key into hash table to get value val
|
||||
string name = hashmap.get(13276);
|
||||
cout << "\nEnter student ID 13276, found name " << name << endl;
|
||||
cout << "\nInput student ID 13276, query name " << name << endl;
|
||||
|
||||
// Remove operation
|
||||
// Remove key-value pair (key, val) from the hash table
|
||||
// Remove key-value pair (key, val) from hash table
|
||||
hashmap.remove(16750);
|
||||
cout << "\nAfter removing 16750, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\nAfter removing 16750, hash table is\nKey -> Value" << endl;
|
||||
hashmap.print();
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -48,7 +48,7 @@ int rotHash(string key) {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
string key = "Hello algorithm";
|
||||
string key = "Hello Algo";
|
||||
|
||||
int hash = addHash(key);
|
||||
cout << "Additive hash value is " << hash << endl;
|
||||
|
||||
Reference in New Issue
Block a user