Решение на Matrix 4 от Иван-Асен Чакъров

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

Към профила на Иван-Асен Чакъров

Резултати

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

Код

use std::convert::TryInto;
use std::ops;
fn to_array<T, const N: usize>(v: Vec<T>) -> [T; N] {
v.try_into()
.unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
}
#[derive(Debug, PartialEq)]
pub struct Matrix<T: Clone> {
cells: [Cell<T>; 4],
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let cells = data.iter().map(|c| Cell(c.clone())).collect();
Matrix {
cells: to_array(cells),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.cells.to_vec()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
Vec::from([
self.cells[0].clone(),
self.cells[2].clone(),
self.cells[1].clone(),
self.cells[3].clone(),
])
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
return Cell(format!("{} {}", self.0, rhs.0));
}
Cell(format!(
"{} {}",
rhs.0.chars().rev().collect::<String>(),
i32::abs(self.0)
))
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut result = String::new();
let repeated = if self.0 < 0 {
rhs.0.chars().rev().collect::<String>()
} else {
rhs.0
};
for _ in 0..(i32::abs(self.0)) {
result.push_str(&repeated);
}
Cell(result)
}
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let added_cells = self
.cells
.iter()
.zip(rhs.cells.iter())
.map(|(a, b)| a.clone() + b.clone())
.collect();
Matrix {
cells: to_array(added_cells),
}
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
format!(
"{} {} {} {}",
(self.cells[0].clone() * rhs.cells[0].clone()).0,
(self.cells[1].clone() * rhs.cells[2].clone()).0,
(self.cells[2].clone() * rhs.cells[1].clone()).0,
(self.cells[3].clone() * rhs.cells[3].clone()).0
)
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-wgqsi8/solution)
    Finished test [unoptimized + debuginfo] target(s) in 8.05s
     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 коментара)

Иван-Асен качи първо решение на 15.11.2021 21:54 (преди почти 4 години)