Решение на Matrix 4 от Наделина Шипочка

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

Към профила на Наделина Шипочка

Резултати

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

Код

use std::ops::Add;
use std::ops::Mul;
///Cell struct
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
///Add for Cell
impl Add<Cell<String>> for Cell<i32>{
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String>{
let out;
if self.0 >= 0 {
out = self.0.to_string() + " " + &other.0
}
else {
out = other.0.chars().rev().collect::<String>() + " " + &(-self.0).to_string()
}
Cell(out)
}
}
///Mul for Cell
impl Mul<Cell<String>> for Cell<i32>{
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String>{
let mut out = String::from("");
if self.0 > 0 {
for _n in 0..self.0 {
out += &other.0;
}
}
if self.0 < 0 {
for _n in self.0..0 {
out += &other.0.chars().rev().collect::<String>()
}
}
Cell(out)
}
}
///Matrix struct
#[derive(Debug)]
pub struct Matrix<T: Clone> {
// Каквито данни ви вършат 'работа'
a11: Cell<T>,
a12: Cell<T>,
a21: Cell<T>,
a22: Cell<T>,
}
///Add for Matrix
impl Add<Matrix<String>> for Matrix<i32>{
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String>{
Matrix{
a11: self.a11 + other.a11,
a12: self.a12 + other.a12,
a21: self.a21 + other.a21,
a22: self.a22 + other.a22,
}
}
}
///Mul for Matrix
impl Mul<Matrix<String>> for Matrix<i32>{
type Output = String;
fn mul(self, other: Matrix<String>) -> String{
(self.a11 * other.a11).0 + " " +
&(self.a12 * other.a21).0 + " " +
&(self.a21 * other.a12).0 + " " +
&(self.a22 * other.a22).0
}
}
//Assoc. functions for Matrix
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix{
a11: Cell(data[0].clone()),
a12: Cell(data[1].clone()),
a21: Cell(data[2].clone()),
a22: Cell(data[3].clone())
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut res: Vec<Cell<T>> = Vec::new();
res.push(self.a11.clone());
res.push(self.a12.clone());
res.push(self.a21.clone());
res.push(self.a22.clone());
res
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut res: Vec<Cell<T>> = Vec::new();
res.push(self.a11.clone());
res.push(self.a21.clone());
res.push(self.a12.clone());
res.push(self.a22.clone());
res
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-1v00oxr/solution)
    Finished test [unoptimized + debuginfo] target(s) in 7.32s
     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 коментара)

Наделина качи първо решение на 14.11.2021 18:32 (преди почти 4 години)