Решение на Matrix 4 от Алекс Петров
Резултати
- 16 точки от тестове
- 0 бонус точки
- 16 точки общо
- 12 успешни тест(а)
- 3 неуспешни тест(а)
Код
use std::ops;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
firstRow: Vec<Cell<T>>,
secondRow: Vec<Cell<T>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _rhs : Cell<String>) -> Cell<String>{
let mut result: String = self.0.to_string().clone().to_owned();
if self.0 >= 0 {
result.push_str(" ");
result.push_str(&_rhs.0.clone().to_owned());
}else{
result.push_str(" ");
result.push_str(&_rhs.0.clone().chars().rev().collect::<String>().to_owned());
}
Cell(result)
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _rhs : Cell<String>) -> Cell<String>{
let mut result: String = String::new();
if self.0 >= 0 {
for i in 0 .. self.0 {
result.push_str(&_rhs.0.clone().to_owned());
}
}else{
for i in 0 .. -1*self.0 {
result.push_str(&_rhs.0.clone().chars().rev().collect::<String>().to_owned());
}
}
Cell(result)
}
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, _rhs : Matrix<String>) -> Matrix<String>{
let mut firstRow : Vec<Cell<String>> = Vec::new();
firstRow.push(self.firstRow[0].clone() + _rhs.firstRow[0].clone());
firstRow.push(self.firstRow[1].clone() + _rhs.firstRow[1].clone());
let mut secondRow: Vec<Cell<String>> = Vec::new();
secondRow.push(self.secondRow[0].clone() + _rhs.secondRow[0].clone());
secondRow.push(self.secondRow[1].clone() + _rhs.secondRow[1].clone());
Matrix{
firstRow,
secondRow
}
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, _rhs : Matrix<String>) -> String{
let mut result = String::new();
result.push_str(&(self.firstRow[0].clone() * _rhs.firstRow[0].clone()).0.clone());
result.push_str(&String::from(" "));
result.push_str(&(self.firstRow[1].clone() * _rhs.secondRow[0].clone()).0.clone());
result.push_str(&String::from(" "));
result.push_str(&(self.secondRow[0].clone() * _rhs.firstRow[1].clone()).0.clone());
result.push_str(&String::from(" "));
result.push_str(&(self.secondRow[1].clone() * _rhs.secondRow[1].clone()).0.clone());
result
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut firstRow = Vec::new();
for current in &data[0 .. 2] {
firstRow.push(Cell(current.clone()));
}
let mut secondRow = Vec::new();
for current in &data[2 .. 4] {
secondRow.push(Cell(current.clone()));
}
let result : Matrix<T> = Matrix{
firstRow,
secondRow
};
return result;
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result = Vec::new();
result.extend(self.firstRow.iter().cloned());
result.extend(self.secondRow.iter().cloned());
return result;
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result = Vec::new();
result.push(self.firstRow[0].clone());
result.push(self.secondRow[0].clone());
result.push(self.firstRow[1].clone());
result.push(self.secondRow[1].clone());
return result;
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-ufmdw7/solution) warning: unused variable: `i` --> src/lib.rs:32:17 | 32 | for i in 0 .. self.0 { | ^ help: if this is intentional, prefix it with an underscore: `_i` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `i` --> src/lib.rs:36:17 | 36 | for i in 0 .. -1*self.0 { | ^ help: if this is intentional, prefix it with an underscore: `_i` warning: structure field `firstRow` should have a snake case name --> src/lib.rs:5:5 | 5 | firstRow: Vec<Cell<T>>, | ^^^^^^^^ help: convert the identifier to snake case: `first_row` | = note: `#[warn(non_snake_case)]` on by default warning: structure field `secondRow` should have a snake case name --> src/lib.rs:6:5 | 6 | secondRow: Vec<Cell<T>>, | ^^^^^^^^^ help: convert the identifier to snake case: `second_row` warning: variable `firstRow` should have a snake case name --> src/lib.rs:48:17 | 48 | let mut firstRow : Vec<Cell<String>> = Vec::new(); | ^^^^^^^^ help: convert the identifier to snake case: `first_row` warning: variable `secondRow` should have a snake case name --> src/lib.rs:52:17 | 52 | let mut secondRow: Vec<Cell<String>> = Vec::new(); | ^^^^^^^^^ help: convert the identifier to snake case: `second_row` warning: variable `firstRow` should have a snake case name --> src/lib.rs:84:17 | 84 | let mut firstRow = Vec::new(); | ^^^^^^^^ help: convert the identifier to snake case: `first_row` warning: variable `secondRow` should have a snake case name --> src/lib.rs:88:17 | 88 | let mut secondRow = Vec::new(); | ^^^^^^^^^ help: convert the identifier to snake case: `second_row` warning: `solution` (lib) generated 8 warnings Finished test [unoptimized + debuginfo] target(s) in 7.67s Running tests/solution_test.rs (target/debug/deps/solution_test-4c880d3f0adaac34) running 15 tests test solution_test::test_adding_int_and_string_negative ... FAILED test solution_test::test_adding_int_and_string_positive ... ok test solution_test::test_adding_int_and_string_unicode ... FAILED 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 ... FAILED 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 failures: ---- solution_test::test_adding_int_and_string_negative stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `"-2 regdab"`, right: `"regdab 2"`', tests/solution_test.rs:55:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ---- solution_test::test_adding_int_and_string_unicode stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `"-3 апо"`, right: `"апо 3"`', tests/solution_test.rs:64:5 ---- solution_test::test_adding_matrices_2 stdout ---- thread 'main' panicked at 'assertion failed: `(left == right)` left: `[Cell("1 едно"), Cell("0 две"), Cell("-3 "), Cell("-37 иритеч")]`, right: `[Cell("1 едно"), Cell("0 две"), Cell(" 3"), Cell(" иритеч 37")]`', tests/solution_test.rs:125:5 failures: solution_test::test_adding_int_and_string_negative solution_test::test_adding_int_and_string_unicode solution_test::test_adding_matrices_2 test result: FAILED. 12 passed; 3 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s error: test failed, to rerun pass '--test solution_test'