Решение на Matrix 4 от Йоан Цвятков
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::{self, Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
rows: [[Cell<T>; 2]; 2],
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let rows = [
[Cell(data[0].clone()), Cell(data[1].clone())],
[Cell(data[2].clone()), Cell(data[3].clone())],
];
return Matrix { rows };
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut res: Vec<Cell<T>> = Vec::new();
for i in 0..2 {
for j in 0..2 {
res.push(self.rows[i][j].clone());
}
}
res
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut res: Vec<Cell<T>> = Vec::new();
for i in 0..2 {
for j in 0..2 {
res.push(self.rows[j][i].clone());
}
}
res
}
}
fn reverse(str: String) -> String {
str.chars().rev().collect()
}
fn repeat_n_times(str: String, n: i32) -> String {
let mut res = String::from("");
for _ in 0..n {
res = res.add(&str);
}
res
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Self::Output {
if self.0 >= 0 {
Cell(format!("{} {}", self.0, rhs.0))
} else {
Cell(format!("{} {}", reverse(rhs.0), self.0.abs()))
}
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
if self.0 >= 0 {
Cell(repeat_n_times(rhs.0, self.0))
} else {
Cell(repeat_n_times(reverse(rhs.0), self.0.abs()))
}
}
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Self::Output {
let rows: [[Cell<String>; 2]; 2] = [
[
self.rows[0][0].clone() + rhs.rows[0][0].clone(),
self.rows[0][1].clone() + rhs.rows[0][1].clone(),
],
[
self.rows[1][0].clone() + rhs.rows[1][0].clone(),
self.rows[1][1].clone() + rhs.rows[1][1].clone(),
],
];
Matrix { rows }
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
format!(
"{} {} {} {}",
(self.rows[0][0].clone() * rhs.rows[0][0].clone()).0,
(self.rows[0][1].clone() * rhs.rows[1][0].clone()).0,
(self.rows[1][0].clone() * rhs.rows[0][1].clone()).0,
(self.rows[1][1].clone() * rhs.rows[1][1].clone()).0,
)
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-10vq0qc/solution) Finished test [unoptimized + debuginfo] target(s) in 7.74s 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