Решение на Matrix 4 от Станислав Димов
Към профила на Станислав Димов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<Cell<T>>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut cells: Vec<Cell<T>> = vec![];
for value in data {
cells.push(Cell(value.clone()))
}
Matrix { cells }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.cells.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let cells_by_col: Vec<Cell<T>> = vec![
self.cells[0].clone(),
self.cells[2].clone(),
self.cells[1].clone(),
self.cells[3].clone(),
];
cells_by_col
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
let mut new_cells: Vec<Cell<String>> = vec![];
for index in 0..4 {
new_cells.push(self.cells[index].clone() + other.cells[index].clone());
}
Matrix { cells: new_cells }
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output {
let mut cell_values: Vec<String> = vec![];
for index in 0..4 {
cell_values.push((self.by_row()[index].clone() * other.by_col()[index].clone()).0)
}
return cell_values.join(" ");
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Self::Output {
match self.0 {
number if number >= 0 => Cell(String::from([number.to_string(), other.0].join(" "))),
number if number < 0 => Cell(String::from([reverse(other.0), number.abs().to_string()].join(" "),
)),
_ => unreachable!(),
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Self::Output {
match self.0 {
number if number > 0 => Cell(String::from(other.0.repeat(number as usize))),
number if number < 0 => Cell(String::from(reverse(other.0).repeat(number.abs() as usize))),
_ => Cell(String::from("")),
}
}
}
fn reverse(string: String) -> String {
string.chars().rev().collect::<String>()
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-12mj3hr/solution) Finished test [unoptimized + debuginfo] target(s) in 8.07s 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.01s
История (2 версии и 0 коментара)
Станислав качи решение на 17.11.2021 22:50 (преди почти 4 години)
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<Cell<T>>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut cells: Vec<Cell<T>> = vec![];
for value in data {
cells.push(Cell(value.clone()))
}
Matrix { cells }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.cells.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let cells_by_col: Vec<Cell<T>> = vec![
self.cells[0].clone(),
self.cells[2].clone(),
self.cells[1].clone(),
self.cells[3].clone(),
];
cells_by_col
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
let mut new_cells: Vec<Cell<String>> = vec![];
for index in 0..4 {
new_cells.push(self.cells[index].clone() + other.cells[index].clone());
}
Matrix { cells: new_cells }
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output {
let mut cell_values: Vec<String> = vec![];
for index in 0..4 {
cell_values.push((self.by_row()[index].clone() * other.by_col()[index].clone()).0)
}
return cell_values.join(" ");
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Self::Output {
match self.0 {
number if number >= 0 => Cell(String::from([number.to_string(), other.0].join(" "))),
number if number < 0 => Cell(String::from([reverse(other.0), number.abs().to_string()].join(" "),
)),
_ => unreachable!(),
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Self::Output {
match self.0 {
number if number > 0 => Cell(String::from(other.0.repeat(number as usize))),
- number if number < 0 => {
- Cell(String::from(reverse(other.0).repeat(number.abs() as usize)))
- }
+ number if number < 0 => Cell(String::from(reverse(other.0).repeat(number.abs() as usize))),
_ => Cell(String::from("")),
}
}
}
fn reverse(string: String) -> String {
string.chars().rev().collect::<String>()
}