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