Решение на Matrix 4 от Петър Ангелов

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

Към профила на Петър Ангелов

Резултати

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

Код

use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
elements: Vec<Cell<T>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
Cell(if self.0 >= 0 {
format!("{} {}", self.0, other.0)
} else {
format!("{} {}", other.0.chars().rev().collect::<String>(), -self.0)
})
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
Cell(if self.0 >= 0 {
other.0.repeat(self.0 as usize)
} else {
other
.0
.chars()
.rev()
.collect::<String>()
.repeat(-self.0 as usize)
})
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut elements: Vec<Cell<T>> = Vec::with_capacity(4);
for el in data {
let y = Clone::clone(el);
elements.push(Cell(y));
}
Matrix { elements }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.elements.to_vec()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut ret = self.elements.to_vec();
ret.swap(1, 2);
ret
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
Matrix {
elements: self
.elements
.iter()
.zip(other.elements)
.map(|(x, y)| x.clone() + y.clone())
.collect::<Vec<Cell<String>>>(),
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
self.by_row()
.iter()
.zip(other.by_col())
.map(|(x, y)| (x.clone() * y.clone()).0)
.collect::<Vec<String>>()
.join(" ")
}
}

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

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

Петър качи първо решение на 18.11.2021 00:25 (преди почти 4 години)