Решение на Matrix 4 от Иван Борисов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<T>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let data_cp = data.clone();
let mut coordinates: Vec<T> = Vec::new();
for coordinate in data_cp {
coordinates.push(coordinate);
}
Matrix { cells: coordinates ,}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut row: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
for coord in cells_copy {
row.push(Cell(coord.clone()));
}
row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut column: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
column.push(Cell(cells_copy[0].clone()));
column.push(Cell(cells_copy[2].clone()));
column.push(Cell(cells_copy[1].clone()));
column.push(Cell(cells_copy[3].clone()));
column
}
}
pub fn customConcat(number: i32, word: String) -> Cell<String> {
let mut answer: String = String::new();
for i in 0..number {
answer += &word;
}
let CellAnswer = Cell(answer);
CellAnswer
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut answer: String = String::new();
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let emptyCellString = Cell(String::from(""));
if number == 0 {
emptyCellString
} else if number > 0 {
customConcat(number, rhsString)
} else {
number *= -1;
let reverseString = rhsString.chars().rev().collect::<String>();
customConcat(number, reverseString)
}
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let mut answer: String = String::new();
if number >= 0 {
let stringNumber = number.to_string();
let mut stringCellInfo: String = String::new();
stringCellInfo = stringNumber + " " + &rhsString;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
} else {
number *= -1;
let stringNumber = number.to_string();
let reverseString = rhsString.chars().rev().collect::<String>();
let mut stringCellInfo: String = String::new();
stringCellInfo = reverseString + " " + &stringNumber;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let N: usize = 4;
let rowSelf = self.by_row();
let rowOther = rhs.by_row();
let mut result = Matrix::<String> { cells: Vec::new() };
for i in 0..N {
let elementOfResultCell: Cell<String> = rowSelf[i].clone() + rowOther[i].clone();
let mut elementOfResult = match elementOfResultCell {
Cell(stri) => stri,
};
result.cells.push(elementOfResult);
}
result
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let selfCol = self.by_row();
let rhsCol = rhs.by_col();
let mut result: String = String::new();
let N: usize = 4;
for i in 0..N {
let curCalculationCell: Cell<String> = selfCol[i].clone() * rhsCol[i].clone();
let curCalculation = match curCalculationCell {
Cell(stri) => stri,
};
result = result + &curCalculation + " ";
}
result.pop();
result
}
}
fn main() {}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1jgoq8r/solution)
warning: unused variable: `i`
--> src/lib.rs:54:9
|
54 | for i in 0..number {
| ^ help: if this is intentional, prefix it with an underscore: `_i`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `answer`
--> src/lib.rs:65:17
|
65 | let mut answer: String = String::new();
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_answer`
warning: unused variable: `answer`
--> src/lib.rs:96:17
|
96 | let mut answer: String = String::new();
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_answer`
warning: value assigned to `stringCellInfo` is never read
--> src/lib.rs:100:21
|
100 | let mut stringCellInfo: String = String::new();
| ^^^^^^^^^^^^^^
|
= note: `#[warn(unused_assignments)]` on by default
= help: maybe it is overwritten before being read?
warning: value assigned to `stringCellInfo` is never read
--> src/lib.rs:108:21
|
108 | let mut stringCellInfo: String = String::new();
| ^^^^^^^^^^^^^^
|
= help: maybe it is overwritten before being read?
warning: variable does not need to be mutable
--> src/lib.rs:65:13
|
65 | let mut answer: String = String::new();
| ----^^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
warning: variable does not need to be mutable
--> src/lib.rs:70:13
|
70 | let mut rhsString: String = match rhs {
| ----^^^^^^^^^
| |
| help: remove this `mut`
warning: variable does not need to be mutable
--> src/lib.rs:93:13
|
93 | let mut rhsString: String = match rhs {
| ----^^^^^^^^^
| |
| help: remove this `mut`
warning: variable does not need to be mutable
--> src/lib.rs:96:13
|
96 | let mut answer: String = String::new();
| ----^^^^^^
| |
| help: remove this `mut`
warning: variable does not need to be mutable
--> src/lib.rs:129:17
|
129 | let mut elementOfResult = match elementOfResultCell {
| ----^^^^^^^^^^^^^^^
| |
| help: remove this `mut`
warning: function is never used: `main`
--> src/lib.rs:157:4
|
157 | fn main() {}
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: function `customConcat` should have a snake case name
--> src/lib.rs:52:8
|
52 | pub fn customConcat(number: i32, word: String) -> Cell<String> {
| ^^^^^^^^^^^^ help: convert the identifier to snake case: `custom_concat`
|
= note: `#[warn(non_snake_case)]` on by default
warning: variable `CellAnswer` should have a snake case name
--> src/lib.rs:57:9
|
57 | let CellAnswer = Cell(answer);
| ^^^^^^^^^^ help: convert the identifier to snake case: `cell_answer`
warning: variable `rhsString` should have a snake case name
--> src/lib.rs:70:17
|
70 | let mut rhsString: String = match rhs {
| ^^^^^^^^^ help: convert the identifier to snake case: `rhs_string`
warning: variable `emptyCellString` should have a snake case name
--> src/lib.rs:73:13
|
73 | let emptyCellString = Cell(String::from(""));
| ^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `empty_cell_string`
warning: variable `reverseString` should have a snake case name
--> src/lib.rs:80:17
|
80 | let reverseString = rhsString.chars().rev().collect::<String>();
| ^^^^^^^^^^^^^ help: convert the identifier to snake case: `reverse_string`
warning: variable `rhsString` should have a snake case name
--> src/lib.rs:93:17
|
93 | let mut rhsString: String = match rhs {
| ^^^^^^^^^ help: convert the identifier to snake case: `rhs_string`
warning: variable `stringNumber` should have a snake case name
--> src/lib.rs:99:17
|
99 | let stringNumber = number.to_string();
| ^^^^^^^^^^^^ help: convert the identifier to snake case: `string_number`
warning: variable `stringCellInfo` should have a snake case name
--> src/lib.rs:100:21
|
100 | let mut stringCellInfo: String = String::new();
| ^^^^^^^^^^^^^^ help: convert the identifier to snake case: `string_cell_info`
warning: variable `CellAnswer` should have a snake case name
--> src/lib.rs:102:17
|
102 | let CellAnswer = Cell(stringCellInfo);
| ^^^^^^^^^^ help: convert the identifier to snake case: `cell_answer`
warning: variable `stringNumber` should have a snake case name
--> src/lib.rs:106:17
|
106 | let stringNumber = number.to_string();
| ^^^^^^^^^^^^ help: convert the identifier to snake case: `string_number`
warning: variable `reverseString` should have a snake case name
--> src/lib.rs:107:17
|
107 | let reverseString = rhsString.chars().rev().collect::<String>();
| ^^^^^^^^^^^^^ help: convert the identifier to snake case: `reverse_string`
warning: variable `stringCellInfo` should have a snake case name
--> src/lib.rs:108:21
|
108 | let mut stringCellInfo: String = String::new();
| ^^^^^^^^^^^^^^ help: convert the identifier to snake case: `string_cell_info`
warning: variable `CellAnswer` should have a snake case name
--> src/lib.rs:110:17
|
110 | let CellAnswer = Cell(stringCellInfo);
| ^^^^^^^^^^ help: convert the identifier to snake case: `cell_answer`
warning: variable `N` should have a snake case name
--> src/lib.rs:120:13
|
120 | let N: usize = 4;
| ^ help: convert the identifier to snake case: `n`
warning: variable `rowSelf` should have a snake case name
--> src/lib.rs:121:13
|
121 | let rowSelf = self.by_row();
| ^^^^^^^ help: convert the identifier to snake case: `row_self`
warning: variable `rowOther` should have a snake case name
--> src/lib.rs:122:13
|
122 | let rowOther = rhs.by_row();
| ^^^^^^^^ help: convert the identifier to snake case: `row_other`
warning: variable `elementOfResultCell` should have a snake case name
--> src/lib.rs:127:17
|
127 | let elementOfResultCell: Cell<String> = rowSelf[i].clone() + rowOther[i].clone();
| ^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `element_of_result_cell`
warning: variable `elementOfResult` should have a snake case name
--> src/lib.rs:129:21
|
129 | let mut elementOfResult = match elementOfResultCell {
| ^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `element_of_result`
warning: variable `selfCol` should have a snake case name
--> src/lib.rs:142:13
|
142 | let selfCol = self.by_row();
| ^^^^^^^ help: convert the identifier to snake case: `self_col`
warning: variable `rhsCol` should have a snake case name
--> src/lib.rs:143:13
|
143 | let rhsCol = rhs.by_col();
| ^^^^^^ help: convert the identifier to snake case: `rhs_col`
warning: variable `N` should have a snake case name
--> src/lib.rs:145:13
|
145 | let N: usize = 4;
| ^ help: convert the identifier to snake case: `n`
warning: variable `curCalculationCell` should have a snake case name
--> src/lib.rs:147:17
|
147 | let curCalculationCell: Cell<String> = selfCol[i].clone() * rhsCol[i].clone();
| ^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `cur_calculation_cell`
warning: variable `curCalculation` should have a snake case name
--> src/lib.rs:148:17
|
148 | let curCalculation = match curCalculationCell {
| ^^^^^^^^^^^^^^ help: convert the identifier to snake case: `cur_calculation`
warning: `solution` (lib) generated 34 warnings
Finished test [unoptimized + debuginfo] target(s) in 8.09s
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
История (5 версии и 0 коментара)
Иван качи решение на 16.11.2021 19:46 (преди около 4 години)
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
+
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<T>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let data_cp = data.clone();
let mut coordinates: Vec<T> = Vec::new();
for coordinate in data_cp {
coordinates.push(coordinate);
}
Matrix { cells: coordinates }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut row: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
for coord in cells_copy {
row.push(Cell(coord.clone()));
}
row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut column: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
column.push(Cell(cells_copy[0].clone()));
column.push(Cell(cells_copy[2].clone()));
column.push(Cell(cells_copy[1].clone()));
column.push(Cell(cells_copy[3].clone()));
column
}
}
pub fn customConcat(number: i32, word: String) -> Cell<String> {
let mut answer: String = String::new();
for i in 0..number {
answer += &word;
}
let CellAnswer = Cell(answer);
CellAnswer
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut answer: String = String::new();
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let emptyCellString = Cell(String::from(""));
if number == 0 {
emptyCellString
} else if number > 0 {
customConcat(number, rhsString)
} else {
number *= -1;
let reverseString = rhsString.chars().rev().collect::<String>();
customConcat(number, reverseString)
}
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let mut answer: String = String::new();
if number >= 0 {
let stringNumber = number.to_string();
let mut stringCellInfo: String = String::new();
stringCellInfo = stringNumber + " " + &rhsString;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
} else {
number *= -1;
let stringNumber = number.to_string();
let reverseString = rhsString.chars().rev().collect::<String>();
let mut stringCellInfo: String = String::new();
stringCellInfo = reverseString + " " + &stringNumber;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let N: usize = 4;
let rowSelf = self.by_row();
let rowOther = rhs.by_row();
let mut result = Matrix::<String> { cells: Vec::new() };
for i in 0..N {
let elementOfResultCell: Cell<String> = rowSelf[i].clone() + rowOther[i].clone();
let mut elementOfResult = match elementOfResultCell {
Cell(stri) => stri,
};
result.cells.push(elementOfResult);
}
result
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let selfCol = self.by_col();
let rhsCol = rhs.by_col();
let mut result: String = String::new();
let N: usize = 4;
for i in 0..N {
let curCalculationCell: Cell<String> = selfCol[i].clone() * rhsCol[i].clone();
let curCalculation = match curCalculationCell {
Cell(stri) => stri,
};
result = result + &curCalculation + " ";
}
result.pop();
result
}
}
fn main() {}
Иван качи решение на 16.11.2021 20:25 (преди около 4 години)
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<T>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let data_cp = data.clone();
let mut coordinates: Vec<T> = Vec::new();
for coordinate in data_cp {
coordinates.push(coordinate);
}
- Matrix { cells: coordinates }
+ Matrix { cells: coordinates ,}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut row: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
for coord in cells_copy {
row.push(Cell(coord.clone()));
}
row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut column: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
column.push(Cell(cells_copy[0].clone()));
column.push(Cell(cells_copy[2].clone()));
column.push(Cell(cells_copy[1].clone()));
column.push(Cell(cells_copy[3].clone()));
column
}
}
pub fn customConcat(number: i32, word: String) -> Cell<String> {
let mut answer: String = String::new();
for i in 0..number {
answer += &word;
}
let CellAnswer = Cell(answer);
CellAnswer
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut answer: String = String::new();
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let emptyCellString = Cell(String::from(""));
if number == 0 {
emptyCellString
} else if number > 0 {
customConcat(number, rhsString)
} else {
number *= -1;
let reverseString = rhsString.chars().rev().collect::<String>();
customConcat(number, reverseString)
}
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let mut answer: String = String::new();
if number >= 0 {
let stringNumber = number.to_string();
let mut stringCellInfo: String = String::new();
stringCellInfo = stringNumber + " " + &rhsString;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
} else {
number *= -1;
let stringNumber = number.to_string();
let reverseString = rhsString.chars().rev().collect::<String>();
let mut stringCellInfo: String = String::new();
stringCellInfo = reverseString + " " + &stringNumber;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let N: usize = 4;
let rowSelf = self.by_row();
let rowOther = rhs.by_row();
let mut result = Matrix::<String> { cells: Vec::new() };
for i in 0..N {
let elementOfResultCell: Cell<String> = rowSelf[i].clone() + rowOther[i].clone();
let mut elementOfResult = match elementOfResultCell {
Cell(stri) => stri,
};
result.cells.push(elementOfResult);
}
result
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let selfCol = self.by_col();
let rhsCol = rhs.by_col();
let mut result: String = String::new();
let N: usize = 4;
for i in 0..N {
let curCalculationCell: Cell<String> = selfCol[i].clone() * rhsCol[i].clone();
let curCalculation = match curCalculationCell {
Cell(stri) => stri,
};
result = result + &curCalculation + " ";
}
result.pop();
result
}
}
fn main() {}
Иван качи решение на 18.11.2021 16:11 (преди около 4 години)
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<T>,
}
+
+// За помощни цели:
+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>>>()
+}
+
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let data_cp = data.clone();
let mut coordinates: Vec<T> = Vec::new();
for coordinate in data_cp {
coordinates.push(coordinate);
}
Matrix { cells: coordinates ,}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut row: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
for coord in cells_copy {
row.push(Cell(coord.clone()));
}
row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut column: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
column.push(Cell(cells_copy[0].clone()));
column.push(Cell(cells_copy[2].clone()));
column.push(Cell(cells_copy[1].clone()));
column.push(Cell(cells_copy[3].clone()));
column
}
}
pub fn customConcat(number: i32, word: String) -> Cell<String> {
let mut answer: String = String::new();
for i in 0..number {
answer += &word;
}
let CellAnswer = Cell(answer);
CellAnswer
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut answer: String = String::new();
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let emptyCellString = Cell(String::from(""));
if number == 0 {
emptyCellString
} else if number > 0 {
customConcat(number, rhsString)
} else {
number *= -1;
let reverseString = rhsString.chars().rev().collect::<String>();
customConcat(number, reverseString)
}
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let mut answer: String = String::new();
if number >= 0 {
let stringNumber = number.to_string();
let mut stringCellInfo: String = String::new();
stringCellInfo = stringNumber + " " + &rhsString;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
} else {
number *= -1;
let stringNumber = number.to_string();
let reverseString = rhsString.chars().rev().collect::<String>();
let mut stringCellInfo: String = String::new();
stringCellInfo = reverseString + " " + &stringNumber;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let N: usize = 4;
let rowSelf = self.by_row();
let rowOther = rhs.by_row();
let mut result = Matrix::<String> { cells: Vec::new() };
for i in 0..N {
let elementOfResultCell: Cell<String> = rowSelf[i].clone() + rowOther[i].clone();
let mut elementOfResult = match elementOfResultCell {
Cell(stri) => stri,
};
result.cells.push(elementOfResult);
}
result
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
- let selfCol = self.by_col();
+ let selfCol = self.by_row();
let rhsCol = rhs.by_col();
let mut result: String = String::new();
let N: usize = 4;
for i in 0..N {
let curCalculationCell: Cell<String> = selfCol[i].clone() * rhsCol[i].clone();
let curCalculation = match curCalculationCell {
Cell(stri) => stri,
};
result = result + &curCalculation + " ";
}
result.pop();
result
}
}
fn main() {}
Иван качи решение на 18.11.2021 16:29 (преди около 4 години)
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<T>,
}
-// За помощни цели:
-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>>>()
-}
+
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let data_cp = data.clone();
let mut coordinates: Vec<T> = Vec::new();
for coordinate in data_cp {
coordinates.push(coordinate);
}
Matrix { cells: coordinates ,}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut row: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
for coord in cells_copy {
row.push(Cell(coord.clone()));
}
row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut column: Vec<Cell<T>> = Vec::new();
let cells_copy = self.cells.clone();
column.push(Cell(cells_copy[0].clone()));
column.push(Cell(cells_copy[2].clone()));
column.push(Cell(cells_copy[1].clone()));
column.push(Cell(cells_copy[3].clone()));
column
}
}
pub fn customConcat(number: i32, word: String) -> Cell<String> {
let mut answer: String = String::new();
for i in 0..number {
answer += &word;
}
let CellAnswer = Cell(answer);
CellAnswer
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut answer: String = String::new();
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let emptyCellString = Cell(String::from(""));
if number == 0 {
emptyCellString
} else if number > 0 {
customConcat(number, rhsString)
} else {
number *= -1;
let reverseString = rhsString.chars().rev().collect::<String>();
customConcat(number, reverseString)
}
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
let mut number: i32 = match self {
Cell(val) => val,
};
let mut rhsString: String = match rhs {
Cell(s) => s,
};
let mut answer: String = String::new();
if number >= 0 {
let stringNumber = number.to_string();
let mut stringCellInfo: String = String::new();
stringCellInfo = stringNumber + " " + &rhsString;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
} else {
number *= -1;
let stringNumber = number.to_string();
let reverseString = rhsString.chars().rev().collect::<String>();
let mut stringCellInfo: String = String::new();
stringCellInfo = reverseString + " " + &stringNumber;
let CellAnswer = Cell(stringCellInfo);
CellAnswer
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let N: usize = 4;
let rowSelf = self.by_row();
let rowOther = rhs.by_row();
let mut result = Matrix::<String> { cells: Vec::new() };
for i in 0..N {
let elementOfResultCell: Cell<String> = rowSelf[i].clone() + rowOther[i].clone();
let mut elementOfResult = match elementOfResultCell {
Cell(stri) => stri,
};
result.cells.push(elementOfResult);
}
result
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let selfCol = self.by_row();
let rhsCol = rhs.by_col();
let mut result: String = String::new();
let N: usize = 4;
for i in 0..N {
let curCalculationCell: Cell<String> = selfCol[i].clone() * rhsCol[i].clone();
let curCalculation = match curCalculationCell {
Cell(stri) => stri,
};
result = result + &curCalculation + " ";
}
result.pop();
result
}
}
fn main() {}
