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