Решение на Matrix 4 от Симеон Михайлов

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

Към профила на Симеон Михайлов

Резултати

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

Код

#[derive(Debug)]
pub struct Matrix<T: Clone> {
elements: Vec<Cell<T>> // e00, e01, 10, e11
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
use std::ops::Add;
use std::ops::Mul;
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
let mut s = String::new();
if self.0 < 0 {
s.push_str(&other.0.chars().rev().collect::<String>().clone());
s.push(' ');
s.push_str(&(self.0 * -1).to_string().clone());
}
else{
s.push_str(&self.0.to_string().clone());
s.push(' ');
s.push_str(&other.0.clone());
}
Cell(s)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
Matrix::new(&[(self.elements[0].clone() + other.elements[0].clone()).0,
(self.elements[1].clone() + other.elements[1].clone()).0,
(self.elements[2].clone() + other.elements[2].clone()).0,
(self.elements[3].clone() + other.elements[3].clone()).0]
)
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let mut s = String::new();
s.push_str(&(self.elements[0].clone()*other.elements[0].clone()).0);
s.push(' ');
s.push_str(&(self.elements[1].clone()*other.elements[2].clone()).0);
s.push(' ');
s.push_str(&(self.elements[2].clone()*other.elements[1].clone()).0);
s.push(' ');
s.push_str(&(self.elements[3].clone()*other.elements[3].clone()).0);
s
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let mut s = String::new();
if self.0 < 0 {
let it = self.0.clone() * -1;
for _i in 0..it {
s.push_str(&other.0.chars().rev().collect::<String>().clone());
}
}
else{
let it = self.0.clone();
for _i in 0..it {
s.push_str(&other.0.clone());
}
}
Cell(s)
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let matrix = Matrix {
elements: vec![Cell(data[0].clone()),
Cell(data[1].clone()),
Cell(data[2].clone()),
Cell(data[3].clone())]};
matrix
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let row_m = vec![self.elements[0].clone(),
self.elements[1].clone(),
self.elements[2].clone(),
self.elements[3].clone()];
row_m
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let col_m = vec![self.elements[0].clone(),
self.elements[2].clone(),
self.elements[1].clone(),
self.elements[3].clone()];
col_m
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-1e1mtyc/solution)
    Finished test [unoptimized + debuginfo] target(s) in 7.44s
     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

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

Симеон качи първо решение на 17.11.2021 18:50 (преди почти 4 години)