Решение на Matrix 4 от Цветан Габровски

Обратно към всички решения

Към профила на Цветан Габровски

Резултати

  • 20 точки от тестове
  • 0 бонус точки
  • 20 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

#[derive(Debug)]
pub struct Matrix<T: Clone> {
first: Cell<T>,
second: Cell<T>,
thirh: Cell<T>,
fourth: 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 _11 = Cell(data[0].clone());
let _12 = Cell(data[1].clone());
let _21 = Cell(data[2].clone());
let _22 = Cell(data[3].clone());
let a = Matrix{
first: _11,
second: _12,
thirh: _21,
fourth: _22,
};
a
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut vec: Vec<Cell<T>> = Vec::new();
vec.push(self.first.clone());
vec.push(self.second.clone());
vec.push(self.thirh.clone());
vec.push(self.fourth.clone());
vec
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut vec: Vec<Cell<T>> = Vec::new();
vec.push(self.first.clone());
vec.push(self.thirh.clone());
vec.push(self.second.clone());
vec.push(self.fourth.clone());
vec
}
}
use std::ops::Add;
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
let c = &self.0.to_string();
let mut numb = c.parse::<i32>().unwrap();
if numb >= 0 {
let mut tmp = numb.to_string();
tmp.push(' ');
let a = other.0.to_string();
tmp.push_str(&a);
return Cell(tmp);
}
let mut a = other.0.to_string().chars().rev().collect::<String>();
numb *=-1;
let tmp = numb.to_string();
a.push(' ');
a.push_str(&tmp);
Cell(a)
}
}
use std::ops::Mul;
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) ->Cell<String> {
let mut temp = other.0.to_string();
if self.0 == 0{
let ret = String::from("");
return Cell(ret);
}
if self.0 > 0{
for i in 1..self.0 {
temp.push_str(&other.0);
}
let ret = Cell(temp);
return ret;
}
temp = other.0.to_string().chars().rev().collect::<String>();
for i in 1..self.0*-1{
temp.push_str(&other.0.to_string().chars().rev().collect::<String>());
}
Cell(temp)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) ->Matrix<String> {
let m = Matrix::<String>{
first: self.first + other.first,
second: self.second + other.second,
thirh: self.thirh + other.thirh,
fourth: self.fourth + other.fourth,
};
m
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul (self, other: Matrix<String>) ->String{
let mut tmp = self.first*other.first;
let mut _str = tmp.0;
let mut multy: String = String::from(_str);
multy.push_str(" ");
tmp = self.second*other.thirh;
multy.push_str(&tmp.0);
tmp = self.thirh * other.second;
multy.push_str(" ");
multy.push_str(&tmp.0);
multy.push_str(" ");
tmp = self.fourth*other.fourth;
multy.push_str(&tmp.0);
multy
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20220112-2706256-y0uhz9/solution)
warning: unused variable: `i`
  --> src/lib.rs:90:17
   |
90 |             for i in 1..self.0 {
   |                 ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `i`
  --> src/lib.rs:99:13
   |
99 |         for i in 1..self.0*-1{
   |             ^ help: if this is intentional, prefix it with an underscore: `_i`

warning: `solution` (lib) generated 2 warnings
    Finished test [unoptimized + debuginfo] target(s) in 7.33s
     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

История (1 версия и 1 коментар)