{"task_id": "Rust/32", "prompt": "\n/*\n xs are coefficients of a polynomial.\n find_zero find x such that poly(x) = 0.\n find_zero returns only only zero point, even if there are many.\n Moreover, find_zero only takes list xs having even number of coefficients\n and largest non zero coefficient as it guarantees\n a solution.\n \n*/\n", "declaration": "\nuse std::{slice::Iter, cmp::{max, self}, mem::replace, collections::{HashSet, HashMap}, ops::Index, ascii::AsciiExt};\nuse rand::Rng;\nuse regex::Regex;\nuse md5;\nuse std::any::{Any, TypeId};\n\nfn poly(xs: &Vec<f64>, x: f64) -> f64 {\n\n", "canonical_solution": "\n let mut sum = 0.0;\n for i in 0..xs.len() {\n sum += xs[i] * x.powi(i as i32);\n }\n sum\n }\n \n fn find_zero(xs: &Vec<f64>) -> f64 {\n let mut ans = 0.0;\n let mut value = poly(xs, ans);\n while value.abs() > 1e-6 {\n let mut driv = 0.0;\n for i in 1..xs.len() {\n driv += xs[i] * ans.powi((i - 1) as i32) * (i as f64);\n }\n ans = ans - value / driv;\n value = poly(xs, ans);\n }\n ans\n }\n", "test": "\n/*\n#[cfg(test)]\nmod tests {\n use super::*;\n\n#[test]\n fn test_poly() {\n let mut rng = rand::thread_rng();\n let mut solution: f64;\n let mut ncoeff: i32;\n for _ in 0..100 {\n ncoeff = 2 * (1 + rng.gen_range(0, 4));\n let mut coeffs = vec![];\n for _ in 0..ncoeff {\n let coeff = -10 + rng.gen_range(0, 21);\n if coeff == 0 {\n coeffs.push(1.0);\n } else {\n coeffs.push(coeff as f64);\n }\n }\n solution = find_zero(&coeffs);\n assert!(poly(&coeffs, solution).abs() < 1e-3);\n }\n }\n\n}\n*/\n", "example_test": "None"}
In
test
, the unit test is surrounded by/* */
.