Решение на Matrix 4 от Мирослав Дионисиев
Към профила на Мирослав Дионисиев
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[cfg(test)]
mod tests {
use super::*;
fn string_cell_vec(s1: &str, s2: &str, s3: &str, s4: &str) -> Vec<Cell<String>> {
[s1, s2, s3, s4].into_iter().map(String::from).map(Cell).collect::<Vec<Cell<String>>>()
}
#[test]
fn test_basic() {
// cells addition
let strCell = Cell(String::from("text"));
assert_eq!((Cell(4) + Cell(String::from("badger"))).0, String::from("4 badger"));
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
assert_eq!(Cell(-1) + strCell.clone(), Cell(String::from("txet 1")));
// standart comparisons
assert_eq!(Cell(String::from("text")), strCell);
assert_ne!(Cell(0), Cell(1));
assert_ne!(Cell(String::from("")), Cell(String::from("text")));
// cells multipliction
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
assert_eq!((Cell(2) * Cell(String::from("mushroom"))).0, String::from("mushroommushroom"));
// matrix operations
let matrix1 = Matrix::new(&[1, 2, 3, 4]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1.by_row()[0], Cell(1));
assert_eq!(matrix1.by_row()[1], Cell(2));
assert_eq!(matrix1.by_row()[2], Cell(3));
assert_eq!(matrix1.by_col()[1], Cell(3));
assert_eq!(matrix1.by_col()[2], Cell(2));
assert_eq!(
(matrix1 + matrix2).by_row(),
string_cell_vec("1 one", "2 two", "3 three", "4 four")
);
assert_eq!(
(Matrix::new(&[1, 2, -3, 4]) + Matrix::new(&[
String::from("one"), String::from("two"),
String::from("abac"), String::from("four")
])).by_row(),
string_cell_vec("1 one", "2 two", "caba 3", "4 four")
);
let matrix1 = Matrix::new(&[1, 2, 1, 1]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1 * matrix2, String::from("one threethree two four"));
assert_eq!(
(Matrix::new(&[1, 2, -3, 4]) * Matrix::new(&[
String::from("one"), String::from("two"),
String::from("abac"), String::from("four")
])),
String::from("one abacabac owtowtowt fourfourfourfour")
);
assert_eq!(
(Matrix::new(&[1, 0, -3, 4]) * Matrix::new(&[
String::from("one"), String::from("two"),
String::from("abac"), String::from("four")
])),
String::from("one owtowtowt fourfourfourfour")
);
}
}
use std::ops;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
// Каквито данни ви вършат работа
matrix: Vec<Vec<Cell<T>>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut index:i32 = 0;
let mut matrixTemp:Vec<Vec<Cell<T>>> = Vec::new();
matrixTemp.push(Vec::new());
matrixTemp.push(Vec::new());
for value in data
{
let cell = Cell(value.clone());
match index
{
0 => matrixTemp[0].push(cell),
1 => matrixTemp[0].push(cell),
2 => matrixTemp[1].push(cell),
3 => matrixTemp[1].push(cell),
_ => unreachable!(),
}
index+=1;
}
Matrix{matrix: matrixTemp}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut cells_by_row: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_row.push(self.matrix[i][j].clone());
}
}
cells_by_row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut cells_by_col: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_col.push(self.matrix[j][i].clone());
}
}
cells_by_col
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => format!("{} {}", value, rhs.0),
Cell(value) if value < 0 => format!("{} {}", rhs.0.chars().rev().collect::<String>(), value*(-1)),
_ => unreachable!(),
});
newCell
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => multiplyString(value, &rhs.0),
Cell(value) if value < 0 => multiplyString(value*(-1), &rhs.0.chars().rev().collect::<String>()),
_ => unreachable!(),
});
newCell
}
}
fn multiplyString(value:i32, str: &String) -> String
{
let mut newStr:String = String::from("");
for _ in 0..value
{
newStr = format!("{}{}", newStr, &str);
}
newStr
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String>
{
let mut arr:[String; 4] = Default::default();
let mut index = 0;
for i in 0..2
{
for j in 0..2
{
arr[index] = (self.matrix[i][j].clone()+rhs.matrix[i][j].clone()).0;
index += 1;
}
}
Matrix::new(&arr)
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String
{
let mut str:String = String::from((self.matrix[0][0].clone()*rhs.matrix[0][0].clone()).0);
for i in 0..2
{
for j in 0..2
{
if i == 0 && j == 0
{
continue;
}
str = format!("{} {}", str, (self.matrix[i][j].clone()*rhs.matrix[j][i].clone()).0);
}
}
str
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-yvirl0/solution) warning: variable `matrixTemp` should have a snake case name --> src/lib.rs:91:17 | 91 | let mut matrixTemp:Vec<Vec<Cell<T>>> = Vec::new(); | ^^^^^^^^^^ help: convert the identifier to snake case: `matrix_temp` | = note: `#[warn(non_snake_case)]` on by default warning: variable `newCell` should have a snake case name --> src/lib.rs:142:13 | 142 | let newCell:Cell<String> = Cell(match self | ^^^^^^^ help: convert the identifier to snake case: `new_cell` warning: variable `newCell` should have a snake case name --> src/lib.rs:157:13 | 157 | let newCell:Cell<String> = Cell(match self | ^^^^^^^ help: convert the identifier to snake case: `new_cell` warning: function `multiplyString` should have a snake case name --> src/lib.rs:167:4 | 167 | fn multiplyString(value:i32, str: &String) -> String | ^^^^^^^^^^^^^^ help: convert the identifier to snake case: `multiply_string` warning: variable `newStr` should have a snake case name --> src/lib.rs:169:13 | 169 | let mut newStr:String = String::from(""); | ^^^^^^ help: convert the identifier to snake case: `new_str` warning: `solution` (lib) generated 5 warnings Finished test [unoptimized + debuginfo] target(s) in 7.80s 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
История (4 версии и 1 коментар)
Мирослав качи решение на 16.11.2021 19:07 (преди почти 4 години)
#[cfg(test)]
mod tests {
use super::*;
fn string_cell_vec(s1: &str, s2: &str, s3: &str, s4: &str) -> Vec<Cell<String>> {
[s1, s2, s3, s4].into_iter().map(String::from).map(Cell).collect::<Vec<Cell<String>>>()
}
#[test]
fn test_basic() {
let strCell = Cell(String::from("text"));
assert_eq!((Cell(4) + Cell(String::from("badger"))).0, String::from("4 badger"));
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
assert_eq!(Cell(-1) + strCell.clone(), Cell(String::from("txet 1")));
assert_eq!(Cell(String::from("text")), strCell);
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
assert_eq!((Cell(2) * Cell(String::from("mushroom"))).0, String::from("mushroommushroom"));
let matrix1 = Matrix::new(&[1, 2, 3, 4]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1.by_row()[0], Cell(1));
assert_eq!(matrix1.by_col()[1], Cell(3));
assert_eq!(matrix1, Matrix::new(&[1,2,3,4]));
assert_eq!(
(matrix1 + matrix2).by_row(),
string_cell_vec("1 one", "2 two", "3 three", "4 four")
);
let matrix1 = Matrix::new(&[1, 1, 1, 1]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1 * matrix2.clone(), String::from("one three two four"));
assert_eq!(matrix2.clone(), Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]));
}
}
use std::ops;
#[derive(Debug, Clone, PartialEq)]
pub struct Matrix<T: Clone> {
// Каквито данни ви вършат работа
matrix: Vec<Vec<Cell<T>>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut index:i32 = 0;
let mut matrixTemp:Vec<Vec<Cell<T>>> = Vec::new();
matrixTemp.push(Vec::new());
matrixTemp.push(Vec::new());
for value in data
{
let cell = Cell(value.clone());
match index
{
0 => matrixTemp[0].push(cell),
1 => matrixTemp[0].push(cell),
2 => matrixTemp[1].push(cell),
3 => matrixTemp[1].push(cell),
_ => unreachable!(),
}
index+=1;
}
Matrix{matrix: matrixTemp}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut cells_by_row: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_row.push(self.matrix[i][j].clone());
}
}
cells_by_row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut cells_by_col: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_col.push(self.matrix[j][i].clone());
}
}
cells_by_col
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => format!("{} {}", value, rhs.0),
- Cell(value) if value < 0 => format!("{} {}", rhs.0.clone().chars().rev().collect::<String>(), value*(-1)),
+ Cell(value) if value < 0 => format!("{} {}", rhs.0.chars().rev().collect::<String>(), value*(-1)),
_ => unreachable!(),
});
newCell
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => multiplyString(value, &rhs.0),
- Cell(value) if value < 0 => multiplyString(value*(-1), &rhs.0.clone().chars().rev().collect::<String>()),
+ Cell(value) if value < 0 => multiplyString(value*(-1), &rhs.0.chars().rev().collect::<String>()),
_ => unreachable!(),
});
newCell
}
}
fn multiplyString(value:i32, str: &String) -> String
{
let mut newStr:String = String::from("");
for _ in 0..value
{
newStr = format!("{}{}", newStr, &str);
}
newStr
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String>
{
let mut arr:[String; 4] = Default::default();
let mut index = 0;
for i in 0..2
{
for j in 0..2
{
arr[index] = (self.matrix[i][j].clone()+rhs.matrix[i][j].clone()).0;
index += 1;
}
}
Matrix::new(&arr)
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String
{
let mut str:String = String::from((self.matrix[0][0].clone()*rhs.matrix[0][0].clone()).0);
for i in 0..2
{
for j in 0..2
{
if i == 0 && j == 0
{
continue;
}
str = format!("{} {}", str, (self.matrix[i][j].clone()*rhs.matrix[j][i].clone()).0);
}
}
str
}
}
Мирослав качи решение на 16.11.2021 20:50 (преди почти 4 години)
#[cfg(test)]
mod tests {
use super::*;
fn string_cell_vec(s1: &str, s2: &str, s3: &str, s4: &str) -> Vec<Cell<String>> {
[s1, s2, s3, s4].into_iter().map(String::from).map(Cell).collect::<Vec<Cell<String>>>()
}
#[test]
fn test_basic() {
let strCell = Cell(String::from("text"));
assert_eq!((Cell(4) + Cell(String::from("badger"))).0, String::from("4 badger"));
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
assert_eq!(Cell(-1) + strCell.clone(), Cell(String::from("txet 1")));
assert_eq!(Cell(String::from("text")), strCell);
-
+
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
assert_eq!((Cell(2) * Cell(String::from("mushroom"))).0, String::from("mushroommushroom"));
let matrix1 = Matrix::new(&[1, 2, 3, 4]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1.by_row()[0], Cell(1));
assert_eq!(matrix1.by_col()[1], Cell(3));
- assert_eq!(matrix1, Matrix::new(&[1,2,3,4]));
assert_eq!(
(matrix1 + matrix2).by_row(),
string_cell_vec("1 one", "2 two", "3 three", "4 four")
);
let matrix1 = Matrix::new(&[1, 1, 1, 1]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
- assert_eq!(matrix1 * matrix2.clone(), String::from("one three two four"));
- assert_eq!(matrix2.clone(), Matrix::new(&[
- String::from("one"), String::from("two"),
- String::from("three"), String::from("four")
- ]));
+ assert_eq!(matrix1 * matrix2, String::from("one three two four"));
}
}
use std::ops;
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Debug)]
pub struct Matrix<T: Clone> {
// Каквито данни ви вършат работа
matrix: Vec<Vec<Cell<T>>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut index:i32 = 0;
let mut matrixTemp:Vec<Vec<Cell<T>>> = Vec::new();
matrixTemp.push(Vec::new());
matrixTemp.push(Vec::new());
for value in data
{
let cell = Cell(value.clone());
match index
{
0 => matrixTemp[0].push(cell),
1 => matrixTemp[0].push(cell),
2 => matrixTemp[1].push(cell),
3 => matrixTemp[1].push(cell),
_ => unreachable!(),
}
index+=1;
}
Matrix{matrix: matrixTemp}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut cells_by_row: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_row.push(self.matrix[i][j].clone());
}
}
cells_by_row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut cells_by_col: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_col.push(self.matrix[j][i].clone());
}
}
cells_by_col
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => format!("{} {}", value, rhs.0),
Cell(value) if value < 0 => format!("{} {}", rhs.0.chars().rev().collect::<String>(), value*(-1)),
_ => unreachable!(),
});
newCell
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => multiplyString(value, &rhs.0),
Cell(value) if value < 0 => multiplyString(value*(-1), &rhs.0.chars().rev().collect::<String>()),
_ => unreachable!(),
});
newCell
}
}
fn multiplyString(value:i32, str: &String) -> String
{
let mut newStr:String = String::from("");
for _ in 0..value
{
newStr = format!("{}{}", newStr, &str);
}
newStr
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String>
{
let mut arr:[String; 4] = Default::default();
let mut index = 0;
for i in 0..2
{
for j in 0..2
{
arr[index] = (self.matrix[i][j].clone()+rhs.matrix[i][j].clone()).0;
index += 1;
}
}
Matrix::new(&arr)
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String
{
let mut str:String = String::from((self.matrix[0][0].clone()*rhs.matrix[0][0].clone()).0);
for i in 0..2
{
for j in 0..2
{
if i == 0 && j == 0
{
continue;
}
str = format!("{} {}", str, (self.matrix[i][j].clone()*rhs.matrix[j][i].clone()).0);
}
}
str
}
}
Мирослав качи решение на 16.11.2021 22:21 (преди почти 4 години)
#[cfg(test)]
mod tests {
use super::*;
fn string_cell_vec(s1: &str, s2: &str, s3: &str, s4: &str) -> Vec<Cell<String>> {
[s1, s2, s3, s4].into_iter().map(String::from).map(Cell).collect::<Vec<Cell<String>>>()
}
#[test]
fn test_basic() {
+ // cells addition
let strCell = Cell(String::from("text"));
assert_eq!((Cell(4) + Cell(String::from("badger"))).0, String::from("4 badger"));
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
assert_eq!(Cell(-1) + strCell.clone(), Cell(String::from("txet 1")));
+
+ // standart comparisons
assert_eq!(Cell(String::from("text")), strCell);
+ assert_ne!(Cell(0), Cell(1));
+ assert_ne!(Cell(String::from("")), Cell(String::from("text")));
+ // cells multipliction
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
assert_eq!((Cell(2) * Cell(String::from("mushroom"))).0, String::from("mushroommushroom"));
+ // matrix operations
let matrix1 = Matrix::new(&[1, 2, 3, 4]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1.by_row()[0], Cell(1));
+ assert_eq!(matrix1.by_row()[1], Cell(2));
+ assert_eq!(matrix1.by_row()[2], Cell(3));
assert_eq!(matrix1.by_col()[1], Cell(3));
+ assert_eq!(matrix1.by_col()[2], Cell(2));
assert_eq!(
(matrix1 + matrix2).by_row(),
string_cell_vec("1 one", "2 two", "3 three", "4 four")
);
+ assert_eq!(
+ (Matrix::new(&[1, 2, -3, 4]) + Matrix::new(&[
+ String::from("one"), String::from("two"),
+ String::from("abac"), String::from("four")
+ ])).by_row(),
+ string_cell_vec("1 one", "2 two", "caba 3", "4 four")
+ );
- let matrix1 = Matrix::new(&[1, 1, 1, 1]);
+ let matrix1 = Matrix::new(&[1, 2, 1, 1]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
- assert_eq!(matrix1 * matrix2, String::from("one three two four"));
+ assert_eq!(matrix1 * matrix2, String::from("one threethree two four"));
+ assert_eq!(
+ (Matrix::new(&[1, 2, -3, 4]) * Matrix::new(&[
+ String::from("one"), String::from("two"),
+ String::from("abac"), String::from("four")
+ ])),
+ String::from("one abacabac owtowtowt fourfourfourfour")
+ );
+ assert_eq!(
+ (Matrix::new(&[1, 0, -3, 4]) * Matrix::new(&[
+ String::from("one"), String::from("two"),
+ String::from("abac"), String::from("four")
+ ])),
+ String::from("one owtowtowt fourfourfourfour")
+ );
}
}
use std::ops;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
// Каквито данни ви вършат работа
matrix: Vec<Vec<Cell<T>>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut index:i32 = 0;
let mut matrixTemp:Vec<Vec<Cell<T>>> = Vec::new();
matrixTemp.push(Vec::new());
matrixTemp.push(Vec::new());
for value in data
{
let cell = Cell(value.clone());
match index
{
0 => matrixTemp[0].push(cell),
1 => matrixTemp[0].push(cell),
2 => matrixTemp[1].push(cell),
3 => matrixTemp[1].push(cell),
_ => unreachable!(),
}
index+=1;
}
Matrix{matrix: matrixTemp}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut cells_by_row: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_row.push(self.matrix[i][j].clone());
}
}
cells_by_row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut cells_by_col: Vec<Cell<T>> = Vec::new();
for i in 0..2
{
for j in 0..2
{
cells_by_col.push(self.matrix[j][i].clone());
}
}
cells_by_col
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => format!("{} {}", value, rhs.0),
Cell(value) if value < 0 => format!("{} {}", rhs.0.chars().rev().collect::<String>(), value*(-1)),
_ => unreachable!(),
});
newCell
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String>
{
let newCell:Cell<String> = Cell(match self
{
Cell(value) if value >= 0 => multiplyString(value, &rhs.0),
Cell(value) if value < 0 => multiplyString(value*(-1), &rhs.0.chars().rev().collect::<String>()),
_ => unreachable!(),
});
newCell
}
}
fn multiplyString(value:i32, str: &String) -> String
{
let mut newStr:String = String::from("");
for _ in 0..value
{
newStr = format!("{}{}", newStr, &str);
}
newStr
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String>
{
let mut arr:[String; 4] = Default::default();
let mut index = 0;
for i in 0..2
{
for j in 0..2
{
arr[index] = (self.matrix[i][j].clone()+rhs.matrix[i][j].clone()).0;
index += 1;
}
}
Matrix::new(&arr)
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String
{
let mut str:String = String::from((self.matrix[0][0].clone()*rhs.matrix[0][0].clone()).0);
for i in 0..2
{
for j in 0..2
{
if i == 0 && j == 0
{
continue;
}
str = format!("{} {}", str, (self.matrix[i][j].clone()*rhs.matrix[j][i].clone()).0);
}
}
str
}
}
Добре е, че си писал с тестове, можеше да пробваш операции с unicode низове обаче (примерно кирилица)