Решение на Matrix 4 от Марио Лалов
Резултати
- 17 точки от тестове
- 0 бонус точки
- 17 точки общо
- 13 успешни тест(а)
- 2 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub row1: [Cell<T>; 2],
pub row2: [Cell<T>; 2],
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Self::Output {
if self.0 < 0 {
let rev_string = String::from(other.0.chars().rev().collect::<String>());
Cell::<String>(rev_string + " " + &self.0.abs().to_string())
} else {
Cell::<String>(self.0.to_string() + " " + &other.0)
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Self::Output {
let mut result = String::new();
//buffer to store the string to be repeated
let mut buff = String::new();
if self.0 < 0 {
buff += &other.0.chars().rev().collect::<String>();
} else {
buff += &other.0;
}
for _i in 0..self.0.abs() {
result += &buff;
}
Cell::<String>(result)
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix::<T> {
row1: [Cell::<T>(data[0].clone()), Cell::<T>(data[1].clone())],
row2: [Cell::<T>(data[2].clone()), Cell::<T>(data[3].clone())],
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
vec![
self.row1[0].clone(),
self.row1[1].clone(),
self.row2[0].clone(),
self.row2[1].clone(),
]
}
pub fn by_col(&self) -> Vec<Cell<T>> {
vec![
self.row1[0].clone(),
self.row1[0].clone(),
self.row2[1].clone(),
self.row2[1].clone(),
]
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix::<String> {
row1: [
self.row1[0].clone() + other.row1[0].clone(),
self.row1[1].clone() + other.row1[1].clone(),
],
row2: [
self.row2[0].clone() + other.row2[0].clone(),
self.row2[1].clone() + other.row2[1].clone(),
],
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output {
let mut result = String::new();
//multiply cells and add up the resulting strings
result = result
+ &(self.row1[0].clone() * other.row1[0].clone()).0
+ " "
+ &(self.row1[1].clone() * other.row2[0].clone()).0;
result = result
+ " "
+ &(self.row2[0].clone() * other.row1[1].clone()).0
+ " "
+ &(self.row2[1].clone() * other.row2[1].clone()).0;
result
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1oy72rn/solution) Finished test [unoptimized + debuginfo] target(s) in 7.35s 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 ... FAILED test solution_test::test_iterating_strings ... FAILED 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_iterating_i32s stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `[Cell(1), Cell(1), Cell(4), Cell(4)]`, right: `[Cell(1), Cell(3), Cell(2), Cell(4)]`', tests/solution_test.rs:21:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_iterating_strings stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `[Cell("a"), Cell("a"), Cell("d"), Cell("d")]`, right: `[Cell("a"), Cell("c"), Cell("b"), Cell("d")]`', tests/solution_test.rs:31:5 failures: solution_test::test_iterating_i32s solution_test::test_iterating_strings test result: FAILED. 13 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s error: test failed, to rerun pass '--test solution_test'