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,124 @@
/**
* File: array_hash_map.rs
* Created Time: 2023-2-18
* Author: xBLACICEx (xBLACKICEx@outlook.com)
*/
/* Key-value pair */
#[derive(Debug, Clone, PartialEq)]
pub struct Pair {
pub key: i32,
pub val: String,
}
/* Hash table based on array implementation */
pub struct ArrayHashMap {
buckets: Vec<Option<Pair>>,
}
impl ArrayHashMap {
pub fn new() -> ArrayHashMap {
// Initialize array with 100 buckets
Self {
buckets: vec![None; 100],
}
}
/* Hash function */
fn hash_func(&self, key: i32) -> usize {
key as usize % 100
}
/* Query operation */
pub fn get(&self, key: i32) -> Option<&String> {
let index = self.hash_func(key);
self.buckets[index].as_ref().map(|pair| &pair.val)
}
/* Add operation */
pub fn put(&mut self, key: i32, val: &str) {
let index = self.hash_func(key);
self.buckets[index] = Some(Pair {
key,
val: val.to_string(),
});
}
/* Remove operation */
pub fn remove(&mut self, key: i32) {
let index = self.hash_func(key);
// Set to None to represent removal
self.buckets[index] = None;
}
/* Get all key-value pairs */
pub fn entry_set(&self) -> Vec<&Pair> {
self.buckets
.iter()
.filter_map(|pair| pair.as_ref())
.collect()
}
/* Get all keys */
pub fn key_set(&self) -> Vec<&i32> {
self.buckets
.iter()
.filter_map(|pair| pair.as_ref().map(|pair| &pair.key))
.collect()
}
/* Get all values */
pub fn value_set(&self) -> Vec<&String> {
self.buckets
.iter()
.filter_map(|pair| pair.as_ref().map(|pair| &pair.val))
.collect()
}
/* Print hash table */
pub fn print(&self) {
for pair in self.entry_set() {
println!("{} -> {}", pair.key, pair.val);
}
}
}
fn main() {
/* Initialize hash table */
let mut map = ArrayHashMap::new();
/* Add operation */
// Add key-value pair (key, value) to 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
let name = map.get(15937).unwrap();
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 pair in map.entry_set() {
println!("{} -> {}", pair.key, pair.val);
}
println!("\nTraverse keys only Key");
for key in map.key_set() {
println!("{}", key);
}
println!("\nTraverse values only Value");
for val in map.value_set() {
println!("{}", val);
}
}
@@ -0,0 +1,49 @@
/*
* File: build_in_hash.rs
* Created Time: 2023-7-6
* Author: WSL0809 (wslzzy@outlook.com)
*/
use hello_algo_rust::include::ListNode;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
/* Driver Code */
fn main() {
let num = 3;
let mut num_hasher = DefaultHasher::new();
num.hash(&mut num_hasher);
let hash_num = num_hasher.finish();
println!("Hash value of integer {} is {}", num, hash_num);
let bol = true;
let mut bol_hasher = DefaultHasher::new();
bol.hash(&mut bol_hasher);
let hash_bol = bol_hasher.finish();
println!("Hash value of boolean {} is {}", bol, hash_bol);
let dec: f32 = 3.14159;
let mut dec_hasher = DefaultHasher::new();
dec.to_bits().hash(&mut dec_hasher);
let hash_dec = dec_hasher.finish();
println!("Hash value of decimal {} is {}", dec, hash_dec);
let str = "Hello Algo";
let mut str_hasher = DefaultHasher::new();
str.hash(&mut str_hasher);
let hash_str = str_hasher.finish();
println!("Hash value of string {} is {}", str, hash_str);
let arr = (&12836, &"Xiao Ha");
let mut tup_hasher = DefaultHasher::new();
arr.hash(&mut tup_hasher);
let hash_tup = tup_hasher.finish();
println!("Hash value of tuple {:?} is {}", arr, hash_tup);
let node = ListNode::new(42);
let mut hasher = DefaultHasher::new();
node.borrow().val.hash(&mut hasher);
let hash = hasher.finish();
println!("Hash value of node object {:?} is {}", node, hash);
}
+48
View File
@@ -0,0 +1,48 @@
/*
* File: hash_map.rs
* Created Time: 2023-02-05
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::print_util;
use std::collections::HashMap;
/* Driver Code */
pub fn main() {
// Initialize hash table
let mut map = HashMap::new();
// Add operation
// Add key-value pair (key, value) to the hash table
map.insert(12836, "Xiao Ha");
map.insert(15937, "Xiao Luo");
map.insert(16750, "Xiao Suan");
map.insert(13276, "Xiao Fa");
map.insert(10583, "Xiao Ya");
println!("\nAfter adding is complete, hash table is\nKey -> Value");
print_util::print_hash_map(&map);
// Query operation
// Input key into hash table to get value
let name = map.get(&15937).copied().unwrap();
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");
print_util::print_hash_map(&map);
// Traverse hash table
println!("\nTraverse key-value pairs Key->Value");
print_util::print_hash_map(&map);
println!("\nTraverse keys only Key");
for key in map.keys() {
println!("{key}");
}
println!("\nTraverse values separately");
for value in map.values() {
println!("{value}");
}
}
@@ -0,0 +1,160 @@
/*
* File: hash_map_chaining.rs
* Created Time: 2023-07-07
* Author: WSL0809 (wslzzy@outlook.com)
*/
#[derive(Clone)]
/* Key-value pair */
struct Pair {
key: i32,
val: String,
}
/* Hash table with separate chaining */
struct HashMapChaining {
size: usize,
capacity: usize,
load_thres: f32,
extend_ratio: usize,
buckets: Vec<Vec<Pair>>,
}
impl HashMapChaining {
/* Constructor */
fn new() -> Self {
Self {
size: 0,
capacity: 4,
load_thres: 2.0 / 3.0,
extend_ratio: 2,
buckets: vec![vec![]; 4],
}
}
/* Hash function */
fn hash_func(&self, key: i32) -> usize {
key as usize % self.capacity
}
/* Load factor */
fn load_factor(&self) -> f32 {
self.size as f32 / self.capacity as f32
}
/* Remove operation */
fn remove(&mut self, key: i32) -> Option<String> {
let index = self.hash_func(key);
// Traverse bucket and remove key-value pair from it
for (i, p) in self.buckets[index].iter_mut().enumerate() {
if p.key == key {
let pair = self.buckets[index].remove(i);
self.size -= 1;
return Some(pair.val);
}
}
// If key is not found, return None
None
}
/* Expand hash table */
fn extend(&mut self) {
// Temporarily store the original hash table
let buckets_tmp = std::mem::take(&mut self.buckets);
// Initialize expanded new hash table
self.capacity *= self.extend_ratio;
self.buckets = vec![Vec::new(); self.capacity as usize];
self.size = 0;
// Move key-value pairs from original hash table to new hash table
for bucket in buckets_tmp {
for pair in bucket {
self.put(pair.key, pair.val);
}
}
}
/* Print hash table */
fn print(&self) {
for bucket in &self.buckets {
let mut res = Vec::new();
for pair in bucket {
res.push(format!("{} -> {}", pair.key, pair.val));
}
println!("{:?}", res);
}
}
/* Add operation */
fn put(&mut self, key: i32, val: String) {
// When load factor exceeds threshold, perform expansion
if self.load_factor() > self.load_thres {
self.extend();
}
let index = self.hash_func(key);
// Traverse bucket, if specified key is encountered, update corresponding val and return
for pair in self.buckets[index].iter_mut() {
if pair.key == key {
pair.val = val;
return;
}
}
// If key does not exist, append key-value pair to the end
let pair = Pair { key, val };
self.buckets[index].push(pair);
self.size += 1;
}
/* Query operation */
fn get(&self, key: i32) -> Option<&str> {
let index = self.hash_func(key);
// Traverse bucket, if key is found, return corresponding val
for pair in self.buckets[index].iter() {
if pair.key == key {
return Some(&pair.val);
}
}
// If key is not found, return None
None
}
}
/* Driver Code */
pub fn main() {
/* Initialize hash table */
let mut map = HashMapChaining::new();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, "Xiao Ha".to_string());
map.put(15937, "Xiao Luo".to_string());
map.put(16750, "Xiao Suan".to_string());
map.put(13276, "Xiao Fa".to_string());
map.put(10583, "Xiao Ya".to_string());
println!("\nAfter adding is complete, hash table is\nKey -> Value");
map.print();
/* Query operation */
// Input key into hash table to get value
println!(
"\nInput student ID 13276, found name {}",
match map.get(13276) {
Some(value) => value,
None => "Not a valid Key",
}
);
/* 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,181 @@
/*
* File: hash_map_open_addressing.rs
* Created Time: 2023-07-16
* Author: WSL0809 (wslzzy@outlook.com), night-cruise (2586447362@qq.com)
*/
#![allow(non_snake_case)]
#![allow(unused)]
mod array_hash_map;
use array_hash_map::Pair;
/* Hash table with open addressing */
struct HashMapOpenAddressing {
size: usize, // Number of key-value pairs
capacity: usize, // Hash table capacity
load_thres: f64, // Load factor threshold for triggering expansion
extend_ratio: usize, // Expansion multiplier
buckets: Vec<Option<Pair>>, // Bucket array
TOMBSTONE: Option<Pair>, // Removal marker
}
impl HashMapOpenAddressing {
/* Constructor */
fn new() -> Self {
Self {
size: 0,
capacity: 4,
load_thres: 2.0 / 3.0,
extend_ratio: 2,
buckets: vec![None; 4],
TOMBSTONE: Some(Pair {
key: -1,
val: "-1".to_string(),
}),
}
}
/* Hash function */
fn hash_func(&self, key: i32) -> usize {
(key % self.capacity as i32) as usize
}
/* Load factor */
fn load_factor(&self) -> f64 {
self.size as f64 / self.capacity as f64
}
/* Search for bucket index corresponding to key */
fn find_bucket(&mut self, key: i32) -> usize {
let mut index = self.hash_func(key);
let mut first_tombstone = -1;
// Linear probing, break when encountering an empty bucket
while self.buckets[index].is_some() {
// If key is found, return corresponding bucket index
if self.buckets[index].as_ref().unwrap().key == key {
// If deletion marker was encountered before, move key-value pair to that index
if first_tombstone != -1 {
self.buckets[first_tombstone as usize] = self.buckets[index].take();
self.buckets[index] = self.TOMBSTONE.clone();
return first_tombstone as usize; // Return the moved bucket index
}
return index; // Return bucket index
}
// Record the first removal marker encountered
if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {
first_tombstone = index as i32;
}
// Calculate bucket index, wrap around to the head if past the tail
index = (index + 1) % self.capacity;
}
// If key does not exist, return the index for insertion
if first_tombstone == -1 {
index
} else {
first_tombstone as usize
}
}
/* Query operation */
fn get(&mut self, key: i32) -> Option<&str> {
// Search for bucket index corresponding to key
let index = self.find_bucket(key);
// If key-value pair is found, return corresponding val
if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {
return self.buckets[index].as_ref().map(|pair| &pair.val as &str);
}
// If key-value pair does not exist, return null
None
}
/* Add operation */
fn put(&mut self, key: i32, val: String) {
// When load factor exceeds threshold, perform expansion
if self.load_factor() > self.load_thres {
self.extend();
}
// Search for bucket index corresponding to key
let index = self.find_bucket(key);
// If key-value pair is found, overwrite val and return
if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {
self.buckets[index].as_mut().unwrap().val = val;
return;
}
// If key-value pair does not exist, add the key-value pair
self.buckets[index] = Some(Pair { key, val });
self.size += 1;
}
/* Remove operation */
fn remove(&mut self, key: i32) {
// Search for bucket index corresponding to key
let index = self.find_bucket(key);
// If key-value pair is found, overwrite it with removal marker
if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {
self.buckets[index] = self.TOMBSTONE.clone();
self.size -= 1;
}
}
/* Expand hash table */
fn extend(&mut self) {
// Temporarily store the original hash table
let buckets_tmp = self.buckets.clone();
// Initialize expanded new hash table
self.capacity *= self.extend_ratio;
self.buckets = vec![None; self.capacity];
self.size = 0;
// Move key-value pairs from original hash table to new hash table
for pair in buckets_tmp {
if pair.is_none() || pair == self.TOMBSTONE {
continue;
}
let pair = pair.unwrap();
self.put(pair.key, pair.val);
}
}
/* Print hash table */
fn print(&self) {
for pair in &self.buckets {
if pair.is_none() {
println!("null");
} else if pair == &self.TOMBSTONE {
println!("TOMBSTONE");
} else {
let pair = pair.as_ref().unwrap();
println!("{} -> {}", pair.key, pair.val);
}
}
}
}
/* Driver Code */
fn main() {
/* Initialize hash table */
let mut hashmap = HashMapOpenAddressing::new();
/* Add operation */
// Add key-value pair (key, value) to the hash table
hashmap.put(12836, "Xiao Ha".to_string());
hashmap.put(15937, "Xiao Luo".to_string());
hashmap.put(16750, "Xiao Suan".to_string());
hashmap.put(13276, "Xiao Fa".to_string());
hashmap.put(10583, "Xiao Ya".to_string());
println!("\nAfter adding is complete, hash table is\nKey -> Value");
hashmap.print();
/* Query operation */
// Input key into hash table to get value val
let name = hashmap.get(13276).unwrap();
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,70 @@
/*
* File: simple_hash.rs
* Created Time: 2023-09-07
* Author: night-cruise (2586447362@qq.com)
*/
/* Additive hash */
fn add_hash(key: &str) -> i32 {
let mut hash = 0_i64;
const MODULUS: i64 = 1000000007;
for c in key.chars() {
hash = (hash + c as i64) % MODULUS;
}
hash as i32
}
/* Multiplicative hash */
fn mul_hash(key: &str) -> i32 {
let mut hash = 0_i64;
const MODULUS: i64 = 1000000007;
for c in key.chars() {
hash = (31 * hash + c as i64) % MODULUS;
}
hash as i32
}
/* XOR hash */
fn xor_hash(key: &str) -> i32 {
let mut hash = 0_i64;
const MODULUS: i64 = 1000000007;
for c in key.chars() {
hash ^= c as i64;
}
(hash & MODULUS) as i32
}
/* Rotational hash */
fn rot_hash(key: &str) -> i32 {
let mut hash = 0_i64;
const MODULUS: i64 = 1000000007;
for c in key.chars() {
hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;
}
hash as i32
}
/* Driver Code */
fn main() {
let key = "Hello Algo";
let hash = add_hash(key);
println!("Additive hash value is {hash}");
let hash = mul_hash(key);
println!("Multiplicative hash value is {hash}");
let hash = xor_hash(key);
println!("XOR hash value is {hash}");
let hash = rot_hash(key);
println!("Rotational hash value is {hash}");
}