Решение на Matrix 4 от Боян Дафов

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

Към профила на Боян Дафов

Резултати

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

Код

#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub elements : Vec<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 mut vec = Vec::new();
for elem in data {
let cell = Cell(elem.clone());
vec.push(cell);
}
let result = Matrix{elements : vec};
result
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.elements.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut col1 = Vec::new();
let mut col2 = Vec::new();
let mut index : u8 = 0;
for elem in &self.elements {
if index % 2 == 0 {
col1.push(elem.clone());
} else {
col2.push(elem.clone());
}
index = index + 1;
}
col1.append(&mut col2);
col1
}
}
use std::ops::Add;
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Self::Output) -> Self::Output {
let mut res = String::from(other.0);
if self.0 >= 0 {
res = format!("{} {}", self.0.to_string(), res)
} else {
res = format!("{} {}", res.chars().rev().collect::<String>(), (self.0 * -1).to_string())
}
Cell(res)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Self::Output) -> Self::Output {
let elements_left = self.by_row();
let elements_right = other.by_row();
let mut elements_result = Vec::new();
for (left, right) in elements_left.iter().zip(elements_right.iter()) {
let a = left.clone();
let b = right.clone();
elements_result.push(a + b);
}
Matrix{elements : elements_result}
}
}
use std::ops::Mul;
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
let mut res = String::from(rhs.0);
if self.0 >= 0 {
res = res.repeat(self.0 as usize);
} else {
res = res.chars().rev().collect::<String>();
res = res.repeat((self.0 * -1) as usize);
}
Cell(res)
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
let elements_left = self.by_row();
let elements_right = rhs.by_col();
let mut result = String::from("");
for (left, right) in elements_left.iter().zip(elements_right.iter()) {
let a = left.clone();
let b = right.clone();
result = format!("{} {}", result, (a * b).0)
}
result[1..].to_string()
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-12x58cn/solution)
    Finished test [unoptimized + debuginfo] target(s) in 7.72s
     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 17:26 (преди почти 4 години)