Решение на Matrix 4 от Жечко Попов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug, Clone)]
pub struct Matrix<T: Clone> {
matrix : [[Cell<T>; 2];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 {
Cell(String::from(format!("{} {}", self.0, other.0)))
} else {
let reversed : String = other.0.chars().rev().collect();
Cell(String::from(format!("{} {}", reversed, -1*self.0)))
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
if self.0 > 0 {
let mut res : String = String::from("");
for _ in 0..self.0 {
res.push_str(&other.0);
}
Cell(String::from(res))
} else if self.0 == 0 {
Cell(String::from(""))
} else {
let s : String = other.0.chars().rev().collect();
let mut res : String = String::from("");
for _ in 0..(-1*self.0) {
res.push_str(&s);
}
Cell(String::from(res))
}
}
}
impl<T : Clone> Matrix<T> {
pub fn new(data : &[T; 4]) -> Matrix<T> {
Self {
matrix : [
[Cell(data[0].clone()),Cell(data[1].clone())],
[Cell(data[2].clone()),Cell(data[3].clone())]
]
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut v = Vec::new();
v.push(self.matrix[0][0].clone());
v.push(self.matrix[0][1].clone());
v.push(self.matrix[1][0].clone());
v.push(self.matrix[1][1].clone());
v
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut v = Vec::new();
v.push(self.matrix[0][0].clone());
v.push(self.matrix[1][0].clone());
v.push(self.matrix[0][1].clone());
v.push(self.matrix[1][1].clone());
v
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix {
matrix : [
[self.matrix[0][0].clone() + other.matrix[0][0].clone(), self.matrix[0][1].clone() + other.matrix[0][1].clone()],
[self.matrix[1][0].clone() + other.matrix[1][0].clone(), self.matrix[1][1].clone() + other.matrix[1][1].clone()]
]
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output {
let mut s = String::from("");
s.push_str(&(self.matrix[0][0].clone() * other.matrix[0][0].clone()).0);
s.push_str(&" ");
s.push_str(&(self.matrix[0][1].clone() * other.matrix[1][0].clone()).0);
s.push_str(&" ");
s.push_str(&(self.matrix[1][0].clone() * other.matrix[0][1].clone()).0);
s.push_str(&" ");
s.push_str(&(self.matrix[1][1].clone() * other.matrix[1][1].clone()).0);
s
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1vqhxnh/solution) Finished test [unoptimized + debuginfo] target(s) in 7.36s 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