Решение на Matrix 4 от Тодор Тодоров
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::{Add, Mul};
#[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{
0: _rhs.0.chars().rev().collect::<String>() + &" " + &(-1*self.0).to_string()
}
}
else {
Cell {
0 : self.0.to_string() + &" " + &_rhs.0
}
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _rhs: Cell<String>) -> Self::Output {
let mut str = "".to_string();
let mut res = "".to_string();
let mut n = self.0;
if self.0 < 0 {
n = -n;
str = _rhs.0.clone().chars().rev().collect::<String>();
}
else if self.0 > 0 {
str = _rhs.0.clone();
};
for _i in 0..n {
res += &str;
}
Cell{
0: res
}
}
}
#[derive(Debug)]
pub struct Matrix<T: Clone> {
a1: Cell<T>,
a2: Cell<T>,
a3: Cell<T>,
a4: Cell<T>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix{
a1: Cell(data[0].clone()),
a2: Cell(data[1].clone()),
a3: Cell(data[2].clone()),
a4: Cell(data[3].clone())
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
[ self.a1.clone(),
self.a2.clone(),
self.a3.clone(),
self.a4.clone()
]
.into_iter().collect()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut v = self.by_row();
v.swap(1, 2);
v
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix {
a1: self.a1 + other.a1,
a2: self.a2 + other.a2,
a3: self.a3 + other.a3,
a4: self.a4 + other.a4
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output {
let m = Matrix {
a1: self.a1 * other.a1,
a2: self.a2 * other.a3,
a3: self.a3 * other.a2,
a4: self.a4 * other.a4
};
[m.a1.0, m.a2.0, m.a3.0, m.a4.0].join(" ")
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1bw8iqz/solution) Finished test [unoptimized + debuginfo] target(s) in 7.57s 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