Решение на Matrix 4 от Мирослав Фурнаджиев

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

Към профила на Мирослав Фурнаджиев

Резултати

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

Код

use std::ops:: {Add, Mul};
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
fn reverse_string(text: String) -> String {
text.chars().rev().collect::<String>()
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
let num = self.0;
let text = other.0;
let result: Cell<String> = if num >= 0 {
Cell((num).to_string() + " " + &text)
} else {
Cell(reverse_string(text) + " " + &(i32::abs(num)).to_string())
};
result
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let num = self.0;
let text = other.0;
let result = if num >= 0 {
Cell((text).repeat(num as usize))
} else {
Cell(reverse_string(text).repeat(i32::abs(num) as usize))
};
result
}
}
#[derive(Debug)]
pub struct Matrix<T: Clone> {
_data: Vec<Cell<T>>
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
_data: data.to_vec().into_iter().map(Cell).collect::<Vec<Cell<T>>>()
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self._data.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
vec![
self._data[0].clone(),
self._data[2].clone(),
self._data[1].clone(),
self._data[3].clone()
]
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
let mut result = Matrix::new(
&[String::from(""), String::from(""), String::from(""), String::from("")]
);
for i in 0..4 {
result._data[i] = self._data[i].clone() + other._data[i].clone();
}
result
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let mut result = String::from("");
let a = self.by_row();
let b = other.by_col();
for i in 0..4 {
result += &(a[i].clone() * b[i].clone()).0;
if i < 3 {
result += " ";
}
}
result
}
}

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

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

Мирослав качи първо решение на 18.11.2021 10:57 (преди почти 4 години)