Решение на Matrix 4 от Георги Кацарски
Към профила на Георги Кацарски
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug,Clone)]
pub struct Matrix<T: Clone> {
vector: Vec<Cell<T>>,
}
#[derive(Debug,Clone, PartialEq,Copy)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T>{
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut vector=Vec::new();
for i in 0..data.len(){
vector.push(Cell::<T>(data[i].clone()));
}
Matrix{
vector
}
}
pub fn by_row(&self)->Vec<Cell<T>>{
let vec_by_row = self.vector.clone();
vec_by_row
}
pub fn by_col(&self)->Vec<Cell<T>>{
let mut vec_by_col = self.vector.clone();
vec_by_col.swap(1,2);
vec_by_col
}
}
//i32+i8
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
if self.0>=0{
Cell(self.0.to_string()+" "+&other.0)
}else{
let reversed:String=other.0.chars().rev().collect();
Cell(reversed+" "+&(-1*self.0).to_string())
}
}
}
impl Mul<Cell<String>> for Cell<i32>{
type Output=Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let mut result=String::new();
if self.0>=0{
for _i in 0..self.0{
result+=&other.0;
}
Cell(result)
}
else{
let reversed:String=other.0.chars().rev().collect();
for _i in 0..-1*self.0{
result+=&reversed;
}
Cell(result)
}
}
}
impl Add<Matrix<String>> for Matrix<i32>{
type Output=Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
let mut result=Vec::new();
for i in 0..4{
let cell=self.vector[i]+other.vector[i].clone();
result.push(cell);
}
Matrix{vector:result}
}
}
impl Mul<Matrix<String>> for Matrix<i32>{
type Output=String;
fn mul(self, other: Matrix<String>) -> String {
let other_by_col=other.by_col();
let mut result=String::new();
for i in 0..4{
let product=self.vector[i]*other_by_col[i].clone();
if i==0{
result=product.0;
}else{
result=result+" "+&product.0;
}
}
result
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-11dg4kh/solution) Finished test [unoptimized + debuginfo] target(s) in 7.97s 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