Решение на Matrix 4 от Теодора Колева
Резултати
- 19 точки от тестове
- 0 бонус точки
- 19 точки общо
- 14 успешни тест(а)
- 1 неуспешни тест(а)
Код
#[derive(Debug)]
pub struct Matrix<T: Clone> {
elems: 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 elems = Vec::new();
for datum in data {
elems.push(Cell(datum.clone()));
}
Matrix { elems }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.elems.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut vec = Vec::new();
vec.push(self.elems[0].clone());
vec.push(self.elems[2].clone());
vec.push(self.elems[1].clone());
vec.push(self.elems[3].clone());
vec
}
}
impl std::ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
let mut new_str = String::new();
let num_str = self.0.abs().to_string();
new_str.push_str(&num_str);
new_str.push(' ');
new_str.push_str(&rhs.0);
if self.0 < 0 {
new_str = new_str.chars().rev().collect();
}
Cell(new_str)
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut new_str = String::new();
let repeating_str = if self.0 < 0 {
rhs.0.chars().rev().collect()
} else {
rhs.0
};
let mut n = self.0.abs();
while n > 0 {
new_str.push_str(repeating_str.as_str());
n = n - 1;
}
Cell(new_str)
}
}
impl std::ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let mut new_elems = Vec::new();
for i in 0..4 {
new_elems.push(self.elems[i].clone() + rhs.elems[i].clone());
}
Matrix { elems: new_elems }
}
}
impl std::ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let mut res = String::new();
let str_matrix_by_col = rhs.by_col();
for i in 0..4 {
let new_cell = self.elems[i].clone() * str_matrix_by_col[i].clone();
res.push_str(new_cell.0.as_str());
if i < 3 {
res.push(' ');
}
}
res
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1axzh23/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 ... FAILED 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 failures: ---- solution_test::test_adding_matrices_2 stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `[Cell("1 едно"), Cell("0 две"), Cell(" 3"), Cell(" иритеч 73")]`, right: `[Cell("1 едно"), Cell("0 две"), Cell(" 3"), Cell(" иритеч 37")]`', tests/solution_test.rs:125:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: solution_test::test_adding_matrices_2 test result: FAILED. 14 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s error: test failed, to rerun pass '--test solution_test'