Решение на Matrix 4 от Димитър Дамянов

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

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

Резултати

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

Код

use std::ops;
use std::convert::TryInto;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
values: 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 {
values: data.to_vec()
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.values.iter().map(|value| Cell(value.clone())).collect()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut cols = Vec::<Cell<T>>::new();
for offset in 0..2 {
let mut i = offset;
while i < self.values.len() {
cols.push(Cell(self.values[i].clone()));
i += 2;
}
}
cols
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _rhs: Cell<String>) -> Cell<String> {
let Cell(val) = self;
if self.0 >= 0 {
Cell(format!("{} {}", val, _rhs.0))
} else {
Cell(format!("{} {}", _rhs.0.chars().rev().collect::<String>(), val.abs()))
}
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _rhs: Cell<String>) -> Cell<String> {
let Cell(val) = self;
if self.0 >= 0 {
Cell(_rhs.0.repeat(val as usize))
} else {
Cell(_rhs.0.chars().rev().collect::<String>().repeat(val.abs() as usize))
}
}
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, _rhs: Matrix<String>) -> Matrix<String> {
let self_rows = self.by_row();
let other_rows = _rhs.by_row();
let v = (0..4).map(|i| (self_rows[i].clone() + other_rows[i].clone()).0).collect::<Vec<String>>();
let boxed_slice = v.into_boxed_slice();
let boxed_array: Box<[String; 4]> = match boxed_slice.try_into() {
Ok(ba) => ba,
Err(o) => panic!("Expected a Vec of length {} but it was {}", 4, o.len()),
};
Matrix::<String>::new(&*boxed_array)
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, _rhs: Matrix<String>) -> String {
let mut v = Vec::new();
let self_rows = self.by_row();
let other_cols = _rhs.by_col();
for i in 0..2 {
for j in 0..2 {
v.push((self_rows[2*i + j].clone() * other_cols[2*i + j].clone()).0);
}
}
v.join(" ")
}
}

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

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