mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-21 11:06: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:
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* File: array_hash_map.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* Key-value pair int->string */
|
||||
class Pair(int key, string val) {
|
||||
public int key = key;
|
||||
public string val = val;
|
||||
}
|
||||
|
||||
/* Hash table based on array implementation */
|
||||
class ArrayHashMap {
|
||||
List<Pair?> buckets;
|
||||
public ArrayHashMap() {
|
||||
// Initialize array with 100 buckets
|
||||
buckets = [];
|
||||
for (int i = 0; i < 100; i++) {
|
||||
buckets.Add(null);
|
||||
}
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
int HashFunc(int key) {
|
||||
int index = key % 100;
|
||||
return index;
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
public string? Get(int key) {
|
||||
int index = HashFunc(key);
|
||||
Pair? pair = buckets[index];
|
||||
if (pair == null) return null;
|
||||
return pair.val;
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
public void Put(int key, string val) {
|
||||
Pair pair = new(key, val);
|
||||
int index = HashFunc(key);
|
||||
buckets[index] = pair;
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
public void Remove(int key) {
|
||||
int index = HashFunc(key);
|
||||
// Set to null to represent deletion
|
||||
buckets[index] = null;
|
||||
}
|
||||
|
||||
/* Get all key-value pairs */
|
||||
public List<Pair> PairSet() {
|
||||
List<Pair> pairSet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
pairSet.Add(pair);
|
||||
}
|
||||
return pairSet;
|
||||
}
|
||||
|
||||
/* Get all keys */
|
||||
public List<int> KeySet() {
|
||||
List<int> keySet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
keySet.Add(pair.key);
|
||||
}
|
||||
return keySet;
|
||||
}
|
||||
|
||||
/* Get all values */
|
||||
public List<string> ValueSet() {
|
||||
List<string> valueSet = [];
|
||||
foreach (Pair? pair in buckets) {
|
||||
if (pair != null)
|
||||
valueSet.Add(pair.val);
|
||||
}
|
||||
return valueSet;
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
public void Print() {
|
||||
foreach (Pair kv in PairSet()) {
|
||||
Console.WriteLine(kv.key + " -> " + kv.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class array_hash_map {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize hash table */
|
||||
ArrayHashMap map = new();
|
||||
|
||||
/* 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");
|
||||
Console.WriteLine("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
string? name = map.Get(15937);
|
||||
Console.WriteLine("\nInput student ID 15937, query name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.Remove(10583);
|
||||
Console.WriteLine("\nAfter removing 10583, hash table is\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* Traverse hash table */
|
||||
Console.WriteLine("\nTraverse key-value pairs Key->Value");
|
||||
foreach (Pair kv in map.PairSet()) {
|
||||
Console.WriteLine(kv.key + " -> " + kv.val);
|
||||
}
|
||||
Console.WriteLine("\nTraverse keys only Key");
|
||||
foreach (int key in map.KeySet()) {
|
||||
Console.WriteLine(key);
|
||||
}
|
||||
Console.WriteLine("\nTraverse values only Value");
|
||||
foreach (string val in map.ValueSet()) {
|
||||
Console.WriteLine(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* File: built_in_hash.cs
|
||||
* Created Time: 2023-06-26
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
public class built_in_hash {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int num = 3;
|
||||
int hashNum = num.GetHashCode();
|
||||
Console.WriteLine("Integer " + num + " has hash value " + hashNum);
|
||||
|
||||
bool bol = true;
|
||||
int hashBol = bol.GetHashCode();
|
||||
Console.WriteLine("Boolean " + bol + " has hash value " + hashBol);
|
||||
|
||||
double dec = 3.14159;
|
||||
int hashDec = dec.GetHashCode();
|
||||
Console.WriteLine("Decimal " + dec + " has hash value " + hashDec);
|
||||
|
||||
string str = "Hello Algo";
|
||||
int hashStr = str.GetHashCode();
|
||||
Console.WriteLine("String " + str + " has hash value " + hashStr);
|
||||
|
||||
object[] arr = [12836, "Xiao Ha"];
|
||||
int hashTup = arr.GetHashCode();
|
||||
Console.WriteLine("Array [" + string.Join(", ", arr) + "] hash value is " + hashTup);
|
||||
|
||||
ListNode obj = new(0);
|
||||
int hashObj = obj.GetHashCode();
|
||||
Console.WriteLine("Node object " + obj + " has hash value " + hashObj);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
/**
|
||||
* File: hash_map.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
public class hash_map {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize hash table */
|
||||
Dictionary<int, string> map = new() {
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
{ 12836, "Xiao Ha" },
|
||||
{ 15937, "Xiao Luo" },
|
||||
{ 16750, "Xiao Suan" },
|
||||
{ 13276, "Xiao Fa" },
|
||||
{ 10583, "Xiao Ya" }
|
||||
};
|
||||
Console.WriteLine("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
PrintUtil.PrintHashMap(map);
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
string name = map[15937];
|
||||
Console.WriteLine("\nInput student ID 15937, query name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.Remove(10583);
|
||||
Console.WriteLine("\nAfter removing 10583, hash table is\nKey -> Value");
|
||||
PrintUtil.PrintHashMap(map);
|
||||
|
||||
/* Traverse hash table */
|
||||
Console.WriteLine("\nTraverse key-value pairs Key->Value");
|
||||
foreach (var kv in map) {
|
||||
Console.WriteLine(kv.Key + " -> " + kv.Value);
|
||||
}
|
||||
Console.WriteLine("\nTraverse keys only Key");
|
||||
foreach (int key in map.Keys) {
|
||||
Console.WriteLine(key);
|
||||
}
|
||||
Console.WriteLine("\nTraverse values only Value");
|
||||
foreach (string val in map.Values) {
|
||||
Console.WriteLine(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* File: hash_map_chaining.cs
|
||||
* Created Time: 2023-06-26
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* Hash table with separate chaining */
|
||||
class HashMapChaining {
|
||||
int size; // Number of key-value pairs
|
||||
int capacity; // Hash table capacity
|
||||
double loadThres; // Load factor threshold for triggering expansion
|
||||
int extendRatio; // Expansion multiplier
|
||||
List<List<Pair>> buckets; // Bucket array
|
||||
|
||||
/* Constructor */
|
||||
public HashMapChaining() {
|
||||
size = 0;
|
||||
capacity = 4;
|
||||
loadThres = 2.0 / 3.0;
|
||||
extendRatio = 2;
|
||||
buckets = new List<List<Pair>>(capacity);
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
buckets.Add([]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
int HashFunc(int key) {
|
||||
return key % capacity;
|
||||
}
|
||||
|
||||
/* Load factor */
|
||||
double LoadFactor() {
|
||||
return (double)size / capacity;
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
public string? Get(int key) {
|
||||
int index = HashFunc(key);
|
||||
// Traverse bucket, if key is found, return corresponding val
|
||||
foreach (Pair pair in buckets[index]) {
|
||||
if (pair.key == key) {
|
||||
return pair.val;
|
||||
}
|
||||
}
|
||||
// If key is not found, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
public void Put(int key, string val) {
|
||||
// When load factor exceeds threshold, perform expansion
|
||||
if (LoadFactor() > loadThres) {
|
||||
Extend();
|
||||
}
|
||||
int index = HashFunc(key);
|
||||
// Traverse bucket, if specified key is encountered, update corresponding val and return
|
||||
foreach (Pair pair in buckets[index]) {
|
||||
if (pair.key == key) {
|
||||
pair.val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// If key does not exist, append key-value pair to the end
|
||||
buckets[index].Add(new Pair(key, val));
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
public void Remove(int key) {
|
||||
int index = HashFunc(key);
|
||||
// Traverse bucket and remove key-value pair from it
|
||||
foreach (Pair pair in buckets[index].ToList()) {
|
||||
if (pair.key == key) {
|
||||
buckets[index].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 = new List<List<Pair>>(capacity);
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
buckets.Add([]);
|
||||
}
|
||||
size = 0;
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
foreach (List<Pair> bucket in bucketsTmp) {
|
||||
foreach (Pair pair in bucket) {
|
||||
Put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
public void Print() {
|
||||
foreach (List<Pair> bucket in buckets) {
|
||||
List<string> res = [];
|
||||
foreach (Pair pair in bucket) {
|
||||
res.Add(pair.key + " -> " + pair.val);
|
||||
}
|
||||
foreach (string kv in res) {
|
||||
Console.WriteLine(kv);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class hash_map_chaining {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize hash table */
|
||||
HashMapChaining map = new();
|
||||
|
||||
/* 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");
|
||||
Console.WriteLine("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
string? name = map.Get(13276);
|
||||
Console.WriteLine("\nInput student ID 13276, query name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.Remove(12836);
|
||||
Console.WriteLine("\nAfter removing 12836, hash table is\nKey -> Value");
|
||||
map.Print();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
* File: hash_map_open_addressing.cs
|
||||
* Created Time: 2023-06-26
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* Hash table with open addressing */
|
||||
class HashMapOpenAddressing {
|
||||
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
|
||||
Pair[] buckets; // Bucket array
|
||||
Pair TOMBSTONE = new(-1, "-1"); // Removal marker
|
||||
|
||||
/* Constructor */
|
||||
public HashMapOpenAddressing() {
|
||||
size = 0;
|
||||
buckets = new Pair[capacity];
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
int HashFunc(int key) {
|
||||
return key % capacity;
|
||||
}
|
||||
|
||||
/* Load factor */
|
||||
double LoadFactor() {
|
||||
return (double)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 */
|
||||
public 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 */
|
||||
public 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 */
|
||||
public 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
|
||||
Pair[] bucketsTmp = buckets;
|
||||
// Initialize expanded new hash table
|
||||
capacity *= extendRatio;
|
||||
buckets = new Pair[capacity];
|
||||
size = 0;
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
foreach (Pair pair in bucketsTmp) {
|
||||
if (pair != null && pair != TOMBSTONE) {
|
||||
Put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
public void Print() {
|
||||
foreach (Pair pair in buckets) {
|
||||
if (pair == null) {
|
||||
Console.WriteLine("null");
|
||||
} else if (pair == TOMBSTONE) {
|
||||
Console.WriteLine("TOMBSTONE");
|
||||
} else {
|
||||
Console.WriteLine(pair.key + " -> " + pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class hash_map_open_addressing {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize hash table */
|
||||
HashMapOpenAddressing map = new();
|
||||
|
||||
/* 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");
|
||||
Console.WriteLine("\nAfter adding is complete, hash table is\nKey -> Value");
|
||||
map.Print();
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
string? name = map.Get(13276);
|
||||
Console.WriteLine("\nInput student ID 13276, query name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.Remove(16750);
|
||||
Console.WriteLine("\nAfter removing 16750, hash table is\nKey -> Value");
|
||||
map.Print();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: simple_hash.cs
|
||||
* Created Time: 2023-06-26
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
public class simple_hash {
|
||||
/* Additive hash */
|
||||
int AddHash(string key) {
|
||||
long hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
foreach (char c in key) {
|
||||
hash = (hash + c) % MODULUS;
|
||||
}
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/* Multiplicative hash */
|
||||
int MulHash(string key) {
|
||||
long hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
foreach (char c in key) {
|
||||
hash = (31 * hash + c) % MODULUS;
|
||||
}
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/* XOR hash */
|
||||
int XorHash(string key) {
|
||||
int hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
foreach (char c in key) {
|
||||
hash ^= c;
|
||||
}
|
||||
return hash & MODULUS;
|
||||
}
|
||||
|
||||
/* Rotational hash */
|
||||
int RotHash(string key) {
|
||||
long hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
foreach (char c in key) {
|
||||
hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;
|
||||
}
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
string key = "Hello Algo";
|
||||
|
||||
int hash = AddHash(key);
|
||||
Console.WriteLine("Additive hash value is " + hash);
|
||||
|
||||
hash = MulHash(key);
|
||||
Console.WriteLine("Multiplicative hash value is " + hash);
|
||||
|
||||
hash = XorHash(key);
|
||||
Console.WriteLine("XOR hash value is " + hash);
|
||||
|
||||
hash = RotHash(key);
|
||||
Console.WriteLine("Rotational hash value is " + hash);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user