-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update (matrix): filling gaps with zeros
- Loading branch information
Showing
4 changed files
with
96 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub fn derive_fnct(drv_times: i32, fnct: Vec<i32>) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,69 @@ | ||
use crate::shared::parse::split_string; | ||
use std::f64; | ||
pub fn calc_n(chunk_len: usize) -> usize { | ||
let mut n = (chunk_len as f64).sqrt().round() as usize; | ||
let dif = n * n; | ||
if chunk_len > dif { | ||
n = n + 1 | ||
} | ||
n | ||
} | ||
|
||
pub fn char_to_mtrx(chars: Vec<Vec<String>>) -> Vec<Vec<Vec<i32>>> { | ||
let final_vec: Vec<Vec<Vec<i32>>> = chars | ||
pub fn char_to_mtrx(chars: Vec<Vec<String>>) -> Vec<Vec<i32>> { | ||
let final_vec: Vec<Vec<i32>> = chars | ||
.into_iter() | ||
.map(|e| { | ||
e.iter() | ||
.map(|c| { | ||
split_string(c.to_string()) | ||
.iter() | ||
.map(|r| r.parse::<i32>().unwrap()) | ||
.collect() | ||
}) | ||
.collect() | ||
}) | ||
.map(|e| e.iter().map(|c| c.parse::<i32>().unwrap()).collect()) | ||
.collect(); | ||
final_vec | ||
} | ||
|
||
// from [["145","456","789"],["741","789","123"]] | ||
|
||
// to [[[1,4,5],[4,5,6],[7,8,9]],[[7,4,1],[7,8,9],[1,2,3]]] | ||
pub fn fill_mtrx_gaps(n: usize, orgnl_mtrx: Vec<Vec<i32>>) -> Vec<Vec<i32>> { | ||
let filled_mtrx: Vec<Vec<i32>> = orgnl_mtrx | ||
.iter() | ||
.map(|c| { | ||
let mut row = c.clone(); // Create a new row based on the original row | ||
if row.len() < n { | ||
// If the row is shorter than n, extend it with zeros | ||
row.extend(std::iter::repeat(0).take(n - row.len())); | ||
} | ||
row // Return the modified row | ||
}) | ||
.collect(); | ||
|
||
filled_mtrx | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::shared::parse::str2String_vec; | ||
|
||
use super::*; | ||
use crate::shared::parse::str2_string_vec; | ||
|
||
#[test] | ||
fn mtrx_vals() { | ||
let expected = vec![ | ||
vec![vec![1, 4, 5], vec![4, 5, 6], vec![7, 8, 9]], | ||
vec![vec![7, 4, 1], vec![7, 8, 9], vec![1, 2, 3]], | ||
]; | ||
let expected = vec![vec![145, 456, 789], vec![741, 789, 123]]; | ||
let nstd_vec = vec![vec!["145", "456", "789"], vec!["741", "789", "123"]]; | ||
let res = char_to_mtrx( | ||
nstd_vec | ||
.iter() | ||
.map(|c| str2String_vec(c.to_vec())) | ||
.map(|c| str2_string_vec(c.to_vec())) | ||
.collect(), | ||
); | ||
assert_eq!(res, expected) | ||
} | ||
|
||
#[test] | ||
fn calc_n_test1() { | ||
let e = 28; | ||
let res = calc_n(e); | ||
assert_eq!(res, 6); | ||
} | ||
|
||
#[test] | ||
fn calc_n_test2() { | ||
let e = 25; | ||
let res = calc_n(e); | ||
assert_eq!(res, 5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters