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