Решение на Matrix 4 от Чудомир Ченков

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

Към профила на Чудомир Ченков

Резултати

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

Код

use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
rows: usize,
cols: usize,
data: Vec<T>
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
rows: 2,
cols: 2,
data: data.iter().cloned().collect()
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut ret : Vec<Cell<T>> = Vec::new();
for elem in &self.data {
ret.push(Cell::<T>(elem.clone()));
}
ret
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut ret : Vec<Cell<T>> = Vec::new();
for i in 0..self.cols {
let mut cur : usize = i;
for _ in 0..self.rows {
ret.push(Cell::<T>(self.data[cur].clone()));
cur += self.cols;
}
}
ret
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Self::Output {
let tmp = format!("{} {}", self.0.abs(), rhs.0);
let ret = if self.0 < 0 { tmp.chars().rev().collect() } else { tmp };
Cell::<String>(ret)
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
let tmp = if self.0 < 0 { rhs.0.chars().rev().collect() } else { rhs.0 };
let ret = tmp.repeat(self.0.abs() as usize);
Cell::<String>(ret)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Self::Output {
let mut res_vec : Vec<String> = Vec::new();
let rows_i32 = self.by_row();
let rows_str = rhs.by_row();
for it in rows_i32.into_iter().zip(rows_str.into_iter()) {
let (cell_i32, cell_str) = it;
let res_cell = cell_i32 + cell_str;
res_vec.push(res_cell.0);
}
let res_data : [String; 4] = [
res_vec[0].clone(),
res_vec[1].clone(),
res_vec[2].clone(),
res_vec[3].clone()
];
Matrix::<String>::new(&res_data)
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
let mut res_vec : Vec<String> = Vec::new();
let rows_i32 = self.by_row();
let cols_str = rhs.by_col();
for it in rows_i32.into_iter().zip(cols_str.into_iter()) {
let (cell_i32, cell_str) = it;
let res_cell = cell_i32 * cell_str;
res_vec.push(res_cell.0);
}
res_vec.join(" ")
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-1xas8gq/solution)
    Finished test [unoptimized + debuginfo] target(s) in 7.92s
     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.00s

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

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

Чудомир качи първо решение на 15.11.2021 18:26 (преди почти 4 години)