mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 21:50:59 +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,41 @@
|
||||
/*
|
||||
* File: binary_search_recur.rs
|
||||
* Created Time: 2023-07-15
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
/* Binary search: problem f(i, j) */
|
||||
fn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {
|
||||
// If the interval is empty, it means there is no target element, return -1
|
||||
if i > j {
|
||||
return -1;
|
||||
}
|
||||
let m: i32 = i + (j - i) / 2;
|
||||
if nums[m as usize] < target {
|
||||
// Recursion subproblem f(m+1, j)
|
||||
return dfs(nums, target, m + 1, j);
|
||||
} else if nums[m as usize] > target {
|
||||
// Recursion subproblem f(i, m-1)
|
||||
return dfs(nums, target, i, m - 1);
|
||||
} else {
|
||||
// Found the target element, return its index
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
/* Binary search */
|
||||
fn binary_search(nums: &[i32], target: i32) -> i32 {
|
||||
let n = nums.len() as i32;
|
||||
// Solve the problem f(0, n-1)
|
||||
dfs(nums, target, 0, n - 1)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let target = 6;
|
||||
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
|
||||
// Binary search (closed interval on both sides)
|
||||
let index = binary_search(&nums, target);
|
||||
println!("Index of target element 6 is {index}");
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* File: build_tree.rs
|
||||
* Created Time: 2023-07-15
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::{print_util, TreeNode};
|
||||
use std::collections::HashMap;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
/* Build binary tree: divide and conquer */
|
||||
fn dfs(
|
||||
preorder: &[i32],
|
||||
inorder_map: &HashMap<i32, i32>,
|
||||
i: i32,
|
||||
l: i32,
|
||||
r: i32,
|
||||
) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
// Terminate when the subtree interval is empty
|
||||
if r - l < 0 {
|
||||
return None;
|
||||
}
|
||||
// Initialize the root node
|
||||
let root = TreeNode::new(preorder[i as usize]);
|
||||
// Query m to divide the left and right subtrees
|
||||
let m = inorder_map.get(&preorder[i as usize]).unwrap();
|
||||
// Subproblem: build the left subtree
|
||||
root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);
|
||||
// Subproblem: build the right subtree
|
||||
root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);
|
||||
// Return the root node
|
||||
Some(root)
|
||||
}
|
||||
|
||||
/* Build binary tree */
|
||||
fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
|
||||
// Initialize hash map, storing the mapping from inorder elements to indices
|
||||
let mut inorder_map: HashMap<i32, i32> = HashMap::new();
|
||||
for i in 0..inorder.len() {
|
||||
inorder_map.insert(inorder[i], i as i32);
|
||||
}
|
||||
let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);
|
||||
root
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let preorder = [3, 9, 2, 1, 7];
|
||||
let inorder = [9, 3, 1, 2, 7];
|
||||
println!("In-order traversal = {:?}", preorder);
|
||||
println!("Pre-order traversal = {:?}", inorder);
|
||||
|
||||
let root = build_tree(&preorder, &inorder);
|
||||
println!("The constructed binary tree is:");
|
||||
print_util::print_tree(root.as_ref().unwrap());
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* File: hanota.rs
|
||||
* Created Time: 2023-07-15
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
/* Move a disk */
|
||||
fn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {
|
||||
// Take out a disk from the top of src
|
||||
let pan = src.pop().unwrap();
|
||||
// Place the disk on top of tar
|
||||
tar.push(pan);
|
||||
}
|
||||
|
||||
/* Solve the Tower of Hanoi problem f(i) */
|
||||
fn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {
|
||||
// If there is only one disk left in src, move it directly to tar
|
||||
if i == 1 {
|
||||
move_pan(src, tar);
|
||||
return;
|
||||
}
|
||||
// Subproblem f(i-1): move the top i-1 disks from src to buf using tar
|
||||
dfs(i - 1, src, tar, buf);
|
||||
// Subproblem f(1): move the remaining disk from src to tar
|
||||
move_pan(src, tar);
|
||||
// Subproblem f(i-1): move the top i-1 disks from buf to tar using src
|
||||
dfs(i - 1, buf, src, tar);
|
||||
}
|
||||
|
||||
/* Solve the Tower of Hanoi problem */
|
||||
fn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {
|
||||
let n = A.len() as i32;
|
||||
// Move the top n disks from A to C using B
|
||||
dfs(n, A, B, C);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let mut A = vec![5, 4, 3, 2, 1];
|
||||
let mut B = Vec::new();
|
||||
let mut C = Vec::new();
|
||||
println!("In initial state:");
|
||||
println!("A = {:?}", A);
|
||||
println!("B = {:?}", B);
|
||||
println!("C = {:?}", C);
|
||||
|
||||
solve_hanota(&mut A, &mut B, &mut C);
|
||||
|
||||
println!("After disk movement is complete:");
|
||||
println!("A = {:?}", A);
|
||||
println!("B = {:?}", B);
|
||||
println!("C = {:?}", C);
|
||||
}
|
||||
Reference in New Issue
Block a user