Решение на Matrix 4 от Светломир Станков
Към профила на Светломир Станков
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<Cell<T>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut cell_vec: Vec<Cell<T>> = Vec::new();
for i in 0..4 {
cell_vec.push(Cell(data[i].clone()));
}
Self{cells: cell_vec}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.cells.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result_vec: Vec<Cell<T>> = self.cells.clone();
result_vec.swap(1,2);
result_vec
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String>{
if self.0 >= 0 {
let str: String = format!("{} {}", self.0, rhs.0);
Cell(str)
} else {
let rev_string: String = rhs.0.chars().rev().collect();
let n_abs: i32 = self.0.abs();
let str: String = format!("{} {}", rev_string, n_abs);
Cell(str)
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String>{
if self.0 >= 0 {
let repeated = rhs.0.repeat(self.0.try_into().unwrap());
Cell(repeated)
} else {
let rev_string: String = rhs.0.chars().rev().collect();
let n_abs: i32 = self.0.abs();
let str: String = rev_string.repeat(n_abs.try_into().unwrap());
Cell(str)
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String>{
let mut new_matrix_arr: [String; 4] = Default::default();
for i in 0..4 {
new_matrix_arr[i] = (self.cells[i].clone() + rhs.cells[i].clone()).0;
}
let result_matrix = Matrix::<String>::new(&new_matrix_arr);
result_matrix
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String{
let mut result_string: String = "".to_string();
let self_by_row = self.by_row();
let rhs_by_col = rhs.by_col();
for i in 0..4 {
let mult_cell: Cell<String> = self_by_row[i].clone() * rhs_by_col[i].clone();
result_string.push_str(&mult_cell.0);
if i != 3 {
result_string.push_str(" ");
}
}
result_string
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-okdx2m/solution) Finished test [unoptimized + debuginfo] target(s) in 7.55s Running tests/solution_test.rs (target/debug/deps/solution_test-4c880d3f0adaac34) running 15 tests test solution_test::test_adding_int_and_string_negative ... ok test solution_test::test_adding_int_and_string_positive ... ok test solution_test::test_adding_int_and_string_unicode ... ok test solution_test::test_adding_int_and_string_zero ... ok test solution_test::test_adding_matrices_1 ... ok test solution_test::test_adding_matrices_2 ... ok test solution_test::test_blank_strings ... ok test solution_test::test_iterating_i32s ... ok test solution_test::test_iterating_strings ... ok test solution_test::test_multiplying_int_and_string_negative ... ok test solution_test::test_multiplying_int_and_string_positive ... ok test solution_test::test_multiplying_int_and_string_unicode ... ok test solution_test::test_multiplying_int_and_string_zero ... ok test solution_test::test_multiplying_matrices_1 ... ok test solution_test::test_multiplying_matrices_2 ... ok test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s