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

View File

@@ -0,0 +1,128 @@
/**
* File: array_hash_map.js
* Created Time: 2022-12-26
* Author: Justin (xiefahit@gmail.com)
*/
/* Key-value pair Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
this.val = val;
}
}
/* Hash table based on array implementation */
class ArrayHashMap {
#buckets;
constructor() {
// Initialize array with 100 buckets
this.#buckets = new Array(100).fill(null);
}
/* Hash function */
#hashFunc(key) {
return key % 100;
}
/* Query operation */
get(key) {
let index = this.#hashFunc(key);
let pair = this.#buckets[index];
if (pair === null) return null;
return pair.val;
}
/* Add operation */
set(key, val) {
let index = this.#hashFunc(key);
this.#buckets[index] = new Pair(key, val);
}
/* Remove operation */
delete(key) {
let index = this.#hashFunc(key);
// Set to null to represent deletion
this.#buckets[index] = null;
}
/* Get all key-value pairs */
entries() {
let arr = [];
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]);
}
}
return arr;
}
/* Get all keys */
keys() {
let arr = [];
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i].key);
}
}
return arr;
}
/* Get all values */
values() {
let arr = [];
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i].val);
}
}
return arr;
}
/* Print hash table */
print() {
let pairSet = this.entries();
for (const pair of pairSet) {
console.info(`${pair.key} -> ${pair.val}`);
}
}
}
/* Driver Code */
/* Initialize hash table */
const map = new ArrayHashMap();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.set(12836, 'Xiao Ha');
map.set(15937, 'Xiao Luo');
map.set(16750, 'Xiao Suan');
map.set(13276, 'Xiao Fa');
map.set(10583, 'Xiao Ya');
console.info('\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);
console.info('\nInput student ID 15937, query name ' + name);
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.delete(10583);
console.info('\nAfter removing 10583, hash table is\nKey -> Value');
map.print();
/* Traverse hash table */
console.info('\nTraverse key-value pairs Key->Value');
for (const pair of map.entries()) {
if (!pair) continue;
console.info(pair.key + ' -> ' + pair.val);
}
console.info('\nTraverse keys only Key');
for (const key of map.keys()) {
console.info(key);
}
console.info('\nTraverse values only Value');
for (const val of map.values()) {
console.info(val);
}

View File

@@ -0,0 +1,44 @@
/**
* File: hash_map.js
* Created Time: 2022-12-26
* Author: Justin (xiefahit@gmail.com)
*/
/* Driver Code */
/* Initialize hash table */
const map = new Map();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.set(12836, 'Xiao Ha');
map.set(15937, 'Xiao Luo');
map.set(16750, 'Xiao Suan');
map.set(13276, 'Xiao Fa');
map.set(10583, 'Xiao Ya');
console.info('\nAfter adding is complete, hash table is\nKey -> Value');
console.info(map);
/* Query operation */
// Input key into hash table to get value
let name = map.get(15937);
console.info('\nInput student ID 15937, query name ' + name);
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.delete(10583);
console.info('\nAfter removing 10583, hash table is\nKey -> Value');
console.info(map);
/* Traverse hash table */
console.info('\nTraverse key-value pairs Key->Value');
for (const [k, v] of map.entries()) {
console.info(k + ' -> ' + v);
}
console.info('\nTraverse keys only Key');
for (const k of map.keys()) {
console.info(k);
}
console.info('\nTraverse values only Value');
for (const v of map.values()) {
console.info(v);
}

View File

@@ -0,0 +1,142 @@
/**
* File: hash_map_chaining.js
* Created Time: 2023-08-06
* Author: yuan0221 (yl1452491917@gmail.com)
*/
/* Key-value pair Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
this.val = val;
}
}
/* Hash table with separate chaining */
class HashMapChaining {
#size; // Number of key-value pairs
#capacity; // Hash table capacity
#loadThres; // Load factor threshold for triggering expansion
#extendRatio; // Expansion multiplier
#buckets; // Bucket array
/* Constructor */
constructor() {
this.#size = 0;
this.#capacity = 4;
this.#loadThres = 2.0 / 3.0;
this.#extendRatio = 2;
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
}
/* Hash function */
#hashFunc(key) {
return key % this.#capacity;
}
/* Load factor */
#loadFactor() {
return this.#size / this.#capacity;
}
/* Query operation */
get(key) {
const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// Traverse bucket, if key is found, return corresponding val
for (const pair of bucket) {
if (pair.key === key) {
return pair.val;
}
}
// If key is not found, return null
return null;
}
/* Add operation */
put(key, val) {
// When load factor exceeds threshold, perform expansion
if (this.#loadFactor() > this.#loadThres) {
this.#extend();
}
const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// Traverse bucket, if specified key is encountered, update corresponding val and return
for (const pair of bucket) {
if (pair.key === key) {
pair.val = val;
return;
}
}
// If key does not exist, append key-value pair to the end
const pair = new Pair(key, val);
bucket.push(pair);
this.#size++;
}
/* Remove operation */
remove(key) {
const index = this.#hashFunc(key);
let bucket = this.#buckets[index];
// Traverse bucket and remove key-value pair from it
for (let i = 0; i < bucket.length; i++) {
if (bucket[i].key === key) {
bucket.splice(i, 1);
this.#size--;
break;
}
}
}
/* Expand hash table */
#extend() {
// Temporarily store the original hash table
const bucketsTmp = this.#buckets;
// Initialize expanded new hash table
this.#capacity *= this.#extendRatio;
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
this.#size = 0;
// Move key-value pairs from original hash table to new hash table
for (const bucket of bucketsTmp) {
for (const pair of bucket) {
this.put(pair.key, pair.val);
}
}
}
/* Print hash table */
print() {
for (const bucket of this.#buckets) {
let res = [];
for (const pair of bucket) {
res.push(pair.key + ' -> ' + pair.val);
}
console.log(res);
}
}
}
/* Driver Code */
/* Initialize hash table */
const map = new 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');
console.log('\nAfter adding is complete, hash table is\nKey -> Value');
map.print();
/* Query operation */
// Input key into hash table to get value
const name = map.get(13276);
console.log('\nInput student ID 13276, query name ' + name);
/* Remove operation */
// Remove key-value pair (key, value) from hash table
map.remove(12836);
console.log('\nAfter removing 12836, hash table is\nKey -> Value');
map.print();

View File

@@ -0,0 +1,177 @@
/**
* File: hashMapOpenAddressing.js
* Created Time: 2023-06-13
* Author: yuan0221 (yl1452491917@gmail.com), krahets (krahets@163.com)
*/
/* Key-value pair Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
this.val = val;
}
}
/* Hash table with open addressing */
class HashMapOpenAddressing {
#size; // Number of key-value pairs
#capacity; // Hash table capacity
#loadThres; // Load factor threshold for triggering expansion
#extendRatio; // Expansion multiplier
#buckets; // Bucket array
#TOMBSTONE; // Removal marker
/* Constructor */
constructor() {
this.#size = 0; // Number of key-value pairs
this.#capacity = 4; // Hash table capacity
this.#loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
this.#extendRatio = 2; // Expansion multiplier
this.#buckets = Array(this.#capacity).fill(null); // Bucket array
this.#TOMBSTONE = new Pair(-1, '-1'); // Removal marker
}
/* Hash function */
#hashFunc(key) {
return key % this.#capacity;
}
/* Load factor */
#loadFactor() {
return this.#size / this.#capacity;
}
/* Search for bucket index corresponding to key */
#findBucket(key) {
let index = this.#hashFunc(key);
let firstTombstone = -1;
// Linear probing, break when encountering an empty bucket
while (this.#buckets[index] !== null) {
// If key is encountered, return the corresponding bucket index
if (this.#buckets[index].key === key) {
// If a removal marker was encountered before, move the key-value pair to that index
if (firstTombstone !== -1) {
this.#buckets[firstTombstone] = this.#buckets[index];
this.#buckets[index] = this.#TOMBSTONE;
return firstTombstone; // Return the moved bucket index
}
return index; // Return bucket index
}
// Record the first removal marker encountered
if (
firstTombstone === -1 &&
this.#buckets[index] === this.#TOMBSTONE
) {
firstTombstone = index;
}
// Calculate bucket index, wrap around to the head if past the tail
index = (index + 1) % this.#capacity;
}
// If key does not exist, return the index for insertion
return firstTombstone === -1 ? index : firstTombstone;
}
/* Query operation */
get(key) {
// Search for bucket index corresponding to key
const index = this.#findBucket(key);
// If key-value pair is found, return corresponding val
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
) {
return this.#buckets[index].val;
}
// If key-value pair does not exist, return null
return null;
}
/* Add operation */
put(key, val) {
// When load factor exceeds threshold, perform expansion
if (this.#loadFactor() > this.#loadThres) {
this.#extend();
}
// Search for bucket index corresponding to key
const index = this.#findBucket(key);
// If key-value pair is found, overwrite val and return
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
) {
this.#buckets[index].val = val;
return;
}
// If key-value pair does not exist, add the key-value pair
this.#buckets[index] = new Pair(key, val);
this.#size++;
}
/* Remove operation */
remove(key) {
// Search for bucket index corresponding to key
const index = this.#findBucket(key);
// If key-value pair is found, overwrite it with removal marker
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
) {
this.#buckets[index] = this.#TOMBSTONE;
this.#size--;
}
}
/* Expand hash table */
#extend() {
// Temporarily store the original hash table
const bucketsTmp = this.#buckets;
// Initialize expanded new hash table
this.#capacity *= this.#extendRatio;
this.#buckets = Array(this.#capacity).fill(null);
this.#size = 0;
// Move key-value pairs from original hash table to new hash table
for (const pair of bucketsTmp) {
if (pair !== null && pair !== this.#TOMBSTONE) {
this.put(pair.key, pair.val);
}
}
}
/* Print hash table */
print() {
for (const pair of this.#buckets) {
if (pair === null) {
console.log('null');
} else if (pair === this.#TOMBSTONE) {
console.log('TOMBSTONE');
} else {
console.log(pair.key + ' -> ' + pair.val);
}
}
}
}
/* Driver Code */
// Initialize hash table
const hashmap = new 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');
console.log('\nAfter adding is complete, hash table is\nKey -> Value');
hashmap.print();
// Query operation
// Input key into hash table to get value val
const name = hashmap.get(13276);
console.log('\nInput student ID 13276, query name ' + name);
// Remove operation
// Remove key-value pair (key, val) from hash table
hashmap.remove(16750);
console.log('\nAfter removing 16750, hash table is\nKey -> Value');
hashmap.print();

View File

@@ -0,0 +1,60 @@
/**
* File: simple_hash.js
* Created Time: 2023-08-06
* Author: yuan0221 (yl1452491917@gmail.com)
*/
/* Additive hash */
function addHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
/* Multiplicative hash */
function mulHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (31 * hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
/* XOR hash */
function xorHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash ^= c.charCodeAt(0);
}
return hash % MODULUS;
}
/* Rotational hash */
function rotHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;
}
return hash;
}
/* Driver Code */
const key = 'Hello Algo';
let hash = addHash(key);
console.log('Additive hash value is ' + hash);
hash = mulHash(key);
console.log('Multiplicative hash value is ' + hash);
hash = xorHash(key);
console.log('XOR hash value is ' + hash);
hash = rotHash(key);
console.log('Rotational hash value is ' + hash);