Skip to content

Commit

Permalink
update (matrix): filling gaps with zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
lumbrjx committed Jan 21, 2024
1 parent 7c66635 commit b6b200f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/encpt/math/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn derive_fnct(drv_times: i32, fnct: Vec<i32>) {}
65 changes: 45 additions & 20 deletions src/encpt/math/matrix.rs
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);
}
}
30 changes: 23 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{encpt::mapping::mapper::*, shared::parse::str2String_vec};
use crate::{
encpt::{mapping::mapper::*, math::matrix::fill_mtrx_gaps},
shared::parse::str2_string_vec,
};
use encpt::{mapping::switcher::switch_chars, math::matrix::char_to_mtrx};
pub mod maps {
pub mod chars;
Expand Down Expand Up @@ -32,11 +35,24 @@ fn main() {
// let res = chr_to_mxas(charvc);
// println!("{:?}", res)

let vecs = vec![
vec!["785", "535", "789", "987", "123"],
vec!["543", "528", "693", "285", "147"],
vec!["753"],
// let vecs = vec![
// vec!["785", "535", "789", "987", "123"],
// vec!["543", "528", "693", "285", "147"],
// vec!["753"],
// ];
// let res = char_to_mtrx(vecs.iter().map(|c| str2_string_vec(c.to_vec())).collect());
// println!("{:?}", res);

let vecs1 = vec![
vec![785, 535, 789, 987, 123, 789],
vec![785, 535, 789, 987, 123, 787],
vec![785, 535, 789, 987, 123, 456],
vec![543, 528, 693, 285, 147, 556],
vec![753, 456, 456, 564],
vec![],
];
let res = char_to_mtrx(vecs.iter().map(|c| str2String_vec(c.to_vec())).collect());
println!("{:?}", res)
let res1 = fill_mtrx_gaps(6, vecs1);
for a in res1 {
println!("{:?}", a);
}
}
31 changes: 27 additions & 4 deletions src/shared/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ pub fn flatten_vec(nstd: Vec<String>) -> Vec<String> {
flatten
}

pub fn str2String_vec(nsrd: Vec<&str>) -> Vec<String> {
pub fn str2_string_vec(nsrd: Vec<&str>) -> Vec<String> {
let nsrd_as_strings: Vec<String> = nsrd.iter().map(|&s| s.to_string()).collect();
nsrd_as_strings
}

pub fn split_by_five(chunk: Vec<String>) -> Vec<Vec<String>> {
let split_vectors: Vec<Vec<String>> = chunk.chunks(5).map(|chunk| chunk.to_vec()).collect();
// need to change
pub fn split_by_n(n: usize, chunk: Vec<String>) -> Vec<Vec<String>> {
let mut split_vectors: Vec<Vec<String>> = chunk.chunks(n).map(|chunk| chunk.to_vec()).collect();
if split_vectors.len() < n {
split_vectors.push(vec![]);
}
split_vectors
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -62,7 +67,25 @@ mod tests {
#[test]
fn flatten_test() {
let nsrd = vec!["Av", "bQ", "TG"];
let res = flatten_vec(str2String_vec(nsrd));
let res = flatten_vec(str2_string_vec(nsrd));
assert_eq!(res, vec!["A", "v", "b", "Q", "T", "G"])
}
#[test]
fn split_by_6_28_elemnets() {
let ele = vec![
"785", "535", "789", "987", "123", "789", "785", "535", "789", "987", "123", "787",
"785", "535", "789", "987", "123", "456", "543", "528", "693", "285", "147", "556",
"753", "456", "456", "564",
];
let exp = vec![
vec!["785", "535", "789", "987", "123", "789"],
vec!["785", "535", "789", "987", "123", "787"],
vec!["785", "535", "789", "987", "123", "456"],
vec!["543", "528", "693", "285", "147", "556"],
vec!["753", "456", "456", "564"],
vec![],
];
let res = split_by_n(6, str2_string_vec(ele));
assert_eq!(res, exp)
}
}

0 comments on commit b6b200f

Please sign in to comment.