Решение на Matrix 4 от Мартин Карликов

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

Към профила на Мартин Карликов

Резултати

  • 19 точки от тестове
  • 0 бонус точки
  • 19 точки общо
  • 14 успешни тест(а)
  • 1 неуспешни тест(а)

Код

use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells : Vec<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, rhs: Cell<String>) -> Self::Output {
let mut result = self.0.abs().to_string();
result += " ";
result += rhs.0.as_str();
Cell(if self.0 < 0 { result.chars().rev().collect() } else { result })
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
let mut result : String = String::new();
for _ in 0..self.0.abs()
{
result += rhs.0.as_str();
}
Cell(if self.0 < 0 { result.chars().rev().collect() } else { result })
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut cells = Vec::new();
for object in data
{
cells.push(object.clone())
}
Matrix { cells }
}
fn by_helper(&self, indexes : &[usize; 4]) -> Vec<Cell<T>> {
let mut result_vec = Vec::new();
for index in indexes {
result_vec.push(Cell(self.cells[*index].clone()));
}
result_vec
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let indexes = [0,1,2,3];
self.by_helper(&indexes)
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let indexes = [0,2,1,3];
self.by_helper(&indexes)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Self::Output {
let mut cells = Vec::new();
let mut int_rows_iter = self.by_row().into_iter();
let mut str_rows_iter = rhs.by_row().into_iter();
while let Some(cell_num) = int_rows_iter.next() {
match str_rows_iter.next() {
Some(cell_str) => { cells.push((cell_num + cell_str).0); }
None => { panic!("Adding matrices of different dimensions!") }
}
}
Matrix { cells }
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
let mut result = String::new();
let mut int_rows_iter = self.by_row().into_iter();
let mut str_cols_iter = rhs.by_col().into_iter();
while let Some(cell_num) = int_rows_iter.next() {
match str_cols_iter.next() {
Some(cell_str) => {
result += (cell_num * cell_str).0.as_str();
result += " "
}
None => { panic!("Adding matrices of different dimensions!") }
}
}
result.pop();
result
}
}

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

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

failures:

---- solution_test::test_adding_matrices_2 stdout ----
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `[Cell("1 едно"), Cell("0 две"), Cell("  3"), Cell(" иритеч 73")]`,
 right: `[Cell("1 едно"), Cell("0 две"), Cell("  3"), Cell(" иритеч 37")]`', tests/solution_test.rs:125:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    solution_test::test_adding_matrices_2

test result: FAILED. 14 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

error: test failed, to rerun pass '--test solution_test'

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

Мартин качи първо решение на 18.11.2021 16:55 (преди почти 4 години)