Решение на Matrix 4 от Ива Караджова
Резултати
- 16 точки от тестове
- 0 бонус точки
- 16 точки общо
- 12 успешни тест(а)
- 3 неуспешни тест(а)
Код
use std::ops;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub data: 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_data = Vec::new();
for i in 0..4 {
cell_data.push(Cell::<T>(data[i].clone()));
}
Matrix {data: cell_data}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result = Vec::new();
for item in &self.data {
result.push((*item).clone())
}
result
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result = Vec::new();
result.push(self.data[0].clone());
result.push(self.data[2].clone());
result.push(self.data[1].clone());
result.push(self.data[3].clone());
result
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, str: Cell<String>) -> Cell<String> {
let Cell::<i32>(integer_cell) = self;
let Cell::<String>(string_cell) = str;
let mut result = String::new();
if integer_cell < 0 {
result.push_str(&string_cell);
result.push_str(" ");
result.push_str(&((-1 * integer_cell).to_string()));
} else {
result.push_str(&(integer_cell.to_string()));
result.push_str(" ");
result.push_str(&string_cell);
};
Cell::<String>(result)
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, str: Cell<String>) -> Cell<String> {
let Cell::<i32>(mut integer_cell) = self;
let Cell::<String>(mut string_cell) = str;
let mut result = String::new();
if integer_cell < 0 {
integer_cell = integer_cell * -1;
string_cell = string_cell.chars().rev().collect::<String>();
};
for _i in 0..integer_cell {
result.push_str(&string_cell);
}
Cell::<String>(result)
}
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, str_matrix: Matrix<String>) -> Matrix<String> {
let mut result = Vec::new();
for i in 0..4 {
result.push(self.data[i].clone() + str_matrix.data[i].clone());
}
Matrix{data: result}
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, str_matrix: Matrix<String>) -> String {
let mut result = String::new();
let integer_matrix = self.by_row();
let string_matrix = str_matrix.by_col();
for i in 0..4 {
let Cell::<String> (element) = integer_matrix[i].clone()*string_matrix[i].clone();
result.push_str(&element);
if i != 3 {
result.push_str(" ");
}
}
result
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1py82uj/solution) Finished test [unoptimized + debuginfo] target(s) in 7.62s Running tests/solution_test.rs (target/debug/deps/solution_test-4c880d3f0adaac34) running 15 tests test solution_test::test_adding_int_and_string_negative ... FAILED test solution_test::test_adding_int_and_string_positive ... ok test solution_test::test_adding_int_and_string_unicode ... FAILED 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_int_and_string_negative stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `"badger 2"`, right: `"regdab 2"`', tests/solution_test.rs:55:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_adding_int_and_string_unicode stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `"опа 3"`, right: `"апо 3"`', tests/solution_test.rs:64:5 ---- solution_test::test_adding_matrices_2 stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `[Cell("1 едно"), Cell("0 две"), Cell(" 3"), Cell("четири 37")]`, right: `[Cell("1 едно"), Cell("0 две"), Cell(" 3"), Cell(" иритеч 37")]`', tests/solution_test.rs:125:5 failures: solution_test::test_adding_int_and_string_negative solution_test::test_adding_int_and_string_unicode solution_test::test_adding_matrices_2 test result: FAILED. 12 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass '--test solution_test'