Решение на Matrix 4 от Даниел Величков
Към профила на Даниел Величков
Резултати
- 16 точки от тестове
- 0 бонус точки
- 16 точки общо
- 12 успешни тест(а)
- 3 неуспешни тест(а)
Код
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
values: Vec<Cell<T>>
}
#[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>) -> Cell<String>{
if self.0 < 0 {
Cell((-self.0.clone()).to_string() + " " + &other.0.clone().chars().rev().collect::<String>())
} else {
Cell(self.0.clone().to_string() + " " + &other.0.clone())
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String>{
if self.0 < 0 {
let mut x = String::new();
let other_as_string = &other.0.clone().chars().rev().collect::<String>();
let mut i = 0;
while i < -self.0.clone() {
x = x + other_as_string;
i = i + 1;
}
Cell(x)
} else {
let mut x = String::new();
let other_as_string = &other.0.clone();
let mut i = 0;
while i < self.0.clone() {
x = x + other_as_string;
i = i + 1;
}
Cell(x)
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String>{
Matrix {
values: vec!(self.values[0].clone() + other.values[0].clone(),
self.values[1].clone() + other.values[1].clone(),
self.values[2].clone() + other.values[2].clone(),
self.values[3].clone() + other.values[3].clone(),)
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String{
let mut output = String::new();
let vec_left = vec!(self.values[0].clone(),self.values[1].clone(), self.values[2].clone(), self.values[3].clone());
let vec_right = vec!(other.values[0].clone(),other.values[1].clone(), other.values[2].clone(), other.values[3].clone());
output = output + &(vec_left[0].clone() * vec_right[0].clone()).0;
output = output + " " + &(vec_left[1].clone() * vec_right[2].clone()).0;
output = output + " " + &(vec_left[2].clone() * vec_right[1].clone()).0;
output = output + " " + &(vec_left[3].clone() * vec_right[3].clone()).0;
output
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
values: vec!(Cell(data[0].clone()), Cell(data[1].clone()), Cell(data[2].clone()), Cell(data[3].clone()))
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.values.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
vec!(self.values[0].clone(), self.values[2].clone(), self.values[1].clone(), self.values[3].clone())
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-sqeg42/solution)
Finished test [unoptimized + debuginfo] target(s) in 8.03s
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'
