BINS solution, and LIA file
This commit is contained in:
parent
843838f626
commit
7ca6bad320
68
rust/src/solutions/bins.rs
Normal file
68
rust/src/solutions/bins.rs
Normal file
@ -0,0 +1,68 @@
|
||||
fn bin_search(a: &[i32], id_vec: &[i32], q: i32) -> i32 {
|
||||
let l = a.len();
|
||||
|
||||
if l == 0 {
|
||||
return -1;
|
||||
}
|
||||
|
||||
let mid: usize = l / 2;
|
||||
if a[mid] == q {
|
||||
return id_vec[mid];
|
||||
}
|
||||
|
||||
if q < a[mid] {
|
||||
return bin_search(&a[0..mid], &id_vec[0..mid], q);
|
||||
}
|
||||
|
||||
return bin_search(&a[mid + 1..l], &id_vec[mid + 1..l], q);
|
||||
}
|
||||
|
||||
pub fn bins(file_content: String) -> String {
|
||||
let lines: Vec<&str> = file_content.lines().collect();
|
||||
let a: Vec<i32> = lines[2]
|
||||
.split_whitespace()
|
||||
.map(|x| x.parse().expect("Couldn't parse one of the numbers in a"))
|
||||
.collect();
|
||||
let result: Vec<String> = lines[3]
|
||||
.split_whitespace()
|
||||
.map(|x| {
|
||||
let l: i32 = a.len() as i32;
|
||||
let id_vec: Vec<i32> = (0..l).collect();
|
||||
let res: i32 = bin_search(
|
||||
&a,
|
||||
&id_vec,
|
||||
x.parse().expect("Couldn't parse one of m integers"),
|
||||
);
|
||||
if res > -1 {
|
||||
return (res + 1).to_string();
|
||||
} else {
|
||||
return res.to_string();
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
result.join(" ")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_bins() {
|
||||
let input_content = r#"5
|
||||
6
|
||||
10 20 30 40 50
|
||||
40 10 35 15 40 20"#
|
||||
.to_string();
|
||||
let expected = "4 1 -1 -1 4 2".to_string();
|
||||
assert_eq!(bins(input_content), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bin_search() {
|
||||
let a = vec![10, 20, 30, 40, 50];
|
||||
let id_vec = vec![0, 1, 2, 3, 4];
|
||||
let q = 40;
|
||||
assert_eq!(bin_search(&a, &id_vec, q), 3);
|
||||
}
|
||||
}
|
16
rust/src/solutions/lia.rs
Normal file
16
rust/src/solutions/lia.rs
Normal file
@ -0,0 +1,16 @@
|
||||
pub fn lia(file_content: String) -> String {
|
||||
//todo
|
||||
"".to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_lia() {
|
||||
let input_content = "2 1".to_string();
|
||||
let expected = "0.684";
|
||||
assert_eq!(lia(input_content), expected);
|
||||
}
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
mod bins;
|
||||
mod iev;
|
||||
mod lia;
|
||||
mod prtm;
|
||||
mod revp;
|
||||
pub use bins::bins;
|
||||
pub use iev::iev;
|
||||
pub use lia::lia;
|
||||
pub use prtm::prtm;
|
||||
pub use revp::revp;
|
||||
|
@ -34,6 +34,8 @@ pub enum Problem {
|
||||
REVP,
|
||||
PRTM,
|
||||
IEV,
|
||||
LIA,
|
||||
BINS,
|
||||
}
|
||||
|
||||
pub fn run_solution(problem: Problem, in_file: &String, out_file: &String) {
|
||||
@ -43,6 +45,8 @@ pub fn run_solution(problem: Problem, in_file: &String, out_file: &String) {
|
||||
Problem::REVP => Ok(solutions::revp(content)),
|
||||
Problem::PRTM => Ok(solutions::prtm(content)),
|
||||
Problem::IEV => Ok(solutions::iev(content)),
|
||||
Problem::LIA => Ok(solutions::lia(content)),
|
||||
Problem::BINS => Ok(solutions::bins(content)),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user