Решение на Matrix 4 от Васил Любенов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug)]
pub struct Matrix<T: Clone> {
first: Cell<T>,
second: Cell<T>,
third: Cell<T>,
fourth: Cell<T>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl std::ops::Add<Cell<String>> for Cell<i32>{
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String>{
match self.0 >= 0{
true => {Cell(format!("{} {}", self.0, rhs.0))}
false => {
Cell(format!("{} {}", rhs.0
.chars()
.rev()
.collect::<String>(),-self.0))
}
}
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32>{
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String>{
let word = if self.0 >= 0 {rhs.0} else {rhs.0.chars().rev().collect::<String>()};
let mut new_word = String::new();
let mut x = 0;
if self.0 < 0 {
while x < -self.0{
new_word.push_str(&word.to_string());
x = x + 1;
}
}
else{
while x < self.0{
new_word.push_str(&word.to_string());
x = x + 1;
}
}
Cell(new_word)
}
}
impl std::ops::Add<Matrix<String>> for Matrix<i32>{
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String>{
Matrix{
first: self.first + rhs.first,
second: self.second + rhs.second,
third: self.third + rhs.third,
fourth: self.fourth + rhs.fourth,
}
}
}
impl std::ops::Mul<Matrix<String>> for Matrix<i32>{
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String{
let mut sentence = String::new();
sentence.push_str((self.first.clone() * rhs.first.clone()).0.as_str());
sentence.push_str(" ");
sentence.push_str((self.second.clone() * rhs.third.clone()).0.as_str());
sentence.push_str(" ");
sentence.push_str((self.third.clone() * rhs.second.clone()).0.as_str());
sentence.push_str(" ");
sentence.push_str((self.fourth.clone() * rhs.fourth.clone()).0.as_str());
sentence
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix::<T>{
first: Cell(data[0].clone()),
second: Cell(data[1].clone()),
third: Cell(data[2].clone()),
fourth: Cell(data[3].clone())
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut in_row = Vec::new();
in_row.push(self.first.clone());
in_row.push(self.second.clone());
in_row.push(self.third.clone());
in_row.push(self.fourth.clone());
in_row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut in_row = Vec::new();
in_row.push(self.first.clone());
in_row.push(self.third.clone());
in_row.push(self.second.clone());
in_row.push(self.fourth.clone());
in_row
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-xkimke/solution) Finished test [unoptimized + debuginfo] target(s) in 7.25s 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