Решение на Matrix 4 от Волен Димитров
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
matrix_row: Vec<Cell<T>>,
matrix_col: Vec<Cell<T>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
match self.0 {
0.. => Cell(self.0.to_string() + " " + &rhs.0),
_ => Cell(rhs.0.chars().rev().collect::<String>() + " " + &self.0.abs().to_string()),
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
match self.0 {
0.. => Cell(rhs.0.repeat(self.0.try_into().unwrap())),
_ => Cell(rhs.0.chars().rev().collect::<String>().repeat(self.0.abs().try_into().unwrap())),
}
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
matrix_row: Vec::from([Cell(data[0].clone()), Cell(data[1].clone()), Cell(data[2].clone()), Cell(data[3].clone())]),
matrix_col: Vec::from([Cell(data[0].clone()), Cell(data[2].clone()), Cell(data[1].clone()), Cell(data[3].clone())]),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.matrix_row.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
self.matrix_col.clone()
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
Matrix::new(&[
(self.matrix_row[0].clone()+rhs.matrix_row[0].clone()).0,
(self.matrix_row[1].clone()+rhs.matrix_row[1].clone()).0,
(self.matrix_row[2].clone()+rhs.matrix_row[2].clone()).0,
(self.matrix_row[3].clone()+rhs.matrix_row[3].clone()).0
])
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
(self.matrix_row[0].clone()*rhs.matrix_row[0].clone()).0 + " " +
&(self.matrix_row[1].clone()*rhs.matrix_row[2].clone()).0 + " " +
&(self.matrix_row[2].clone()*rhs.matrix_row[1].clone()).0 + " " +
&(self.matrix_row[3].clone()*rhs.matrix_row[3].clone()).0
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1fqvny9/solution) Finished test [unoptimized + debuginfo] target(s) in 7.52s 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