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,54 @@
/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* Coin change: Greedy algorithm */
fn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {
// Assume coins list is sorted
let mut i = coins.len() - 1;
let mut count = 0;
// Loop to make greedy choices until no remaining amount
while amt > 0 {
// Find the coin that is less than and closest to the remaining amount
while i > 0 && coins[i] > amt {
i -= 1;
}
// Choose coins[i]
amt -= coins[i];
count += 1;
}
// If no feasible solution is found, return -1
if amt == 0 {
count
} else {
-1
}
}
/* Driver Code */
fn main() {
// Greedy algorithm: Can guarantee finding the global optimal solution
let coins = [1, 5, 10, 20, 50, 100];
let amt = 186;
let res = coin_change_greedy(&coins, amt);
println!("\ncoins = {:?}, amt = {}", coins, amt);
println!("Minimum coins needed to make {} is {}", amt, res);
// Greedy algorithm: Cannot guarantee finding the global optimal solution
let coins = [1, 20, 50];
let amt = 60;
let res = coin_change_greedy(&coins, amt);
println!("\ncoins = {:?}, amt = {}", coins, amt);
println!("Minimum coins needed to make {} is {}", amt, res);
println!("Actually the minimum number needed is 3, i.e., 20 + 20 + 20");
// Greedy algorithm: Cannot guarantee finding the global optimal solution
let coins = [1, 49, 50];
let amt = 98;
let res = coin_change_greedy(&coins, amt);
println!("\ncoins = {:?}, amt = {}", coins, amt);
println!("Minimum coins needed to make {} is {}", amt, res);
println!("Actually the minimum number needed is 2, i.e., 49 + 49");
}
@@ -0,0 +1,59 @@
/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* Item */
struct Item {
w: i32, // Item weight
v: i32, // Item value
}
impl Item {
fn new(w: i32, v: i32) -> Self {
Self { w, v }
}
}
/* Fractional knapsack: Greedy algorithm */
fn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {
// Create item list with two attributes: weight, value
let mut items = wgt
.iter()
.zip(val.iter())
.map(|(&w, &v)| Item::new(w, v))
.collect::<Vec<Item>>();
// Sort by unit value item.v / item.w from high to low
items.sort_by(|a, b| {
(b.v as f64 / b.w as f64)
.partial_cmp(&(a.v as f64 / a.w as f64))
.unwrap()
});
// Loop for greedy selection
let mut res = 0.0;
for item in &items {
if item.w <= cap {
// If remaining capacity is sufficient, put the entire current item into the knapsack
res += item.v as f64;
cap -= item.w;
} else {
// If remaining capacity is insufficient, put part of the current item into the knapsack
res += item.v as f64 / item.w as f64 * cap as f64;
// No remaining capacity, so break out of the loop
break;
}
}
res
}
/* Driver Code */
fn main() {
let wgt = [10, 20, 30, 40, 50];
let val = [50, 120, 150, 210, 240];
let cap = 50;
// Greedy algorithm
let res = fractional_knapsack(&wgt, &val, cap);
println!("Maximum item value not exceeding knapsack capacity is {}", res);
}
@@ -0,0 +1,36 @@
/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* Max capacity: Greedy algorithm */
fn max_capacity(ht: &[i32]) -> i32 {
// Initialize i, j to be at both ends of the array
let mut i = 0;
let mut j = ht.len() - 1;
// Initial max capacity is 0
let mut res = 0;
// Loop for greedy selection until the two boards meet
while i < j {
// Update max capacity
let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;
res = std::cmp::max(res, cap);
// Move the shorter board inward
if ht[i] < ht[j] {
i += 1;
} else {
j -= 1;
}
}
res
}
/* Driver Code */
fn main() {
let ht = [3, 8, 5, 2, 7, 7, 3, 4];
// Greedy algorithm
let res = max_capacity(&ht);
println!("Maximum capacity is {}", res);
}
@@ -0,0 +1,35 @@
/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* Max product cutting: Greedy algorithm */
fn max_product_cutting(n: i32) -> i32 {
// When n <= 3, must cut out a 1
if n <= 3 {
return 1 * (n - 1);
}
// Greedily cut out 3, a is the number of 3s, b is the remainder
let a = n / 3;
let b = n % 3;
if b == 1 {
// When the remainder is 1, convert a pair of 1 * 3 to 2 * 2
3_i32.pow(a as u32 - 1) * 2 * 2
} else if b == 2 {
// When the remainder is 2, do nothing
3_i32.pow(a as u32) * 2
} else {
// When the remainder is 0, do nothing
3_i32.pow(a as u32)
}
}
/* Driver Code */
fn main() {
let n = 58;
// Greedy algorithm
let res = max_product_cutting(n);
println!("Maximum cutting product is {}", res);
}