Решение на Matrix 4 от Иван Стефанов

Обратно към всички решения

Към профила на Иван Стефанов

Резултати

  • 17 точки от тестове
  • 0 бонус точки
  • 17 точки общо
  • 13 успешни тест(а)
  • 2 неуспешни тест(а)

Код

use std::ops::Add;
use std::ops::Mul;
#[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> {
let output : String = if self.0 < 0
{
let mut reversed : String = other.0.clone();
reversed = reversed.chars().rev().collect::<String>();
reversed + " " + &self.0.abs().to_string()
}
else
{
self.0.to_string() + " " + &other.0
};
Cell::<String>(output)
}
}
impl Add<Cell<String>> for Cell<String> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
let output : String = self.0 + " " + &other.0;
Cell::<String>(output)
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let mut init : String = String::from("");
let output : String = if self.0 < 0
{
let mut reversed : String = other.0.clone();
reversed = reversed.chars().rev().collect::<String>();
let itr : i32 = self.0.abs();
for _i in 0..itr {
init.push_str(&reversed);
}
init
}
else if self.0 == 0
{
init
}
else
{
let itr : i32 = self.0;
for _i in 0..itr {
init.push_str(&other.0);
}
init
};
Cell::<String>(output)
}
}
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub matrix : Vec<Vec<Cell<T>>>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut output : Vec<Vec<Cell<T>>> = Vec::new();
let mut row1 : Vec<Cell<T>> = Vec::new();
let mut row2 : Vec<Cell<T>> = Vec::new();
row1.push(Cell(data[0].clone()));
row1.push(Cell(data[1].clone()));
row2.push(Cell(data[2].clone()));
row2.push(Cell(data[3].clone()));
output.push(row1);
output.push(row2);
Matrix{matrix: output}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut output : Vec<Cell<T>> = Vec::new();
output.push(self.matrix[0][0].clone());
output.push(self.matrix[0][1].clone());
output.push(self.matrix[1][0].clone());
output.push(self.matrix[1][1].clone());
output
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut output : Vec<Cell<T>> = Vec::new();
output.push(self.matrix[0][0].clone());
output.push(self.matrix[1][0].clone());
output.push(self.matrix[0][1].clone());
output.push(self.matrix[1][1].clone());
output
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
let mut output : Vec<Vec<Cell<String>>> = Vec::new();
let mut row1 : Vec<Cell<String>> = Vec::new();
let mut row2 : Vec<Cell<String>> = Vec::new();
let el1 : Cell<String> = self.matrix[0][0].clone() + other.matrix[0][0].clone();
let el2 : Cell<String> = self.matrix[0][1].clone() + other.matrix[0][1].clone();
let el3 : Cell<String> = self.matrix[1][0].clone() + other.matrix[1][0].clone();
let el4 : Cell<String> = self.matrix[1][1].clone() + other.matrix[1][1].clone();
row1.push(el1);
row1.push(el2);
row2.push(el3);
row2.push(el4);
output.push(row1);
output.push(row2);
Matrix{matrix: output}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let output : Cell<String> = self.matrix[0][0].clone() * other.matrix[0][0].clone() +
self.matrix[0][1].clone() * other.matrix[0][1].clone() +
self.matrix[1][0].clone() * other.matrix[1][0].clone() +
self.matrix[1][1].clone() * other.matrix[1][1].clone();
output.0
}
}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20220112-2706256-anxh5o/solution)
    Finished test [unoptimized + debuginfo] target(s) in 8.23s
     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 ... FAILED
test solution_test::test_multiplying_matrices_2 ... FAILED

failures:

---- solution_test::test_multiplying_matrices_1 stdout ----
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"one twotwo threethreethree you get it"`,
 right: `"one threethree twotwotwo you get it"`', tests/solution_test.rs:139:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- solution_test::test_multiplying_matrices_2 stdout ----
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"едно       иритеч иритеч"`,
 right: `"едно  евдевдевд  иритеч иритеч"`', tests/solution_test.rs:153:5


failures:
    solution_test::test_multiplying_matrices_1
    solution_test::test_multiplying_matrices_2

test result: FAILED. 13 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--test solution_test'

История (2 версии и 0 коментара)

Иван качи първо решение на 18.11.2021 16:25 (преди почти 4 години)

Иван качи решение на 18.11.2021 16:27 (преди почти 4 години)

use std::ops::Add;
use std::ops::Mul;
#[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> {
let output : String = if self.0 < 0
{
let mut reversed : String = other.0.clone();
reversed = reversed.chars().rev().collect::<String>();
reversed + " " + &self.0.abs().to_string()
}
else
{
self.0.to_string() + " " + &other.0
};
Cell::<String>(output)
}
}
impl Add<Cell<String>> for Cell<String> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
let output : String = self.0 + " " + &other.0;
Cell::<String>(output)
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let mut init : String = String::from("");
let output : String = if self.0 < 0
{
let mut reversed : String = other.0.clone();
reversed = reversed.chars().rev().collect::<String>();
let itr : i32 = self.0.abs();
for _i in 0..itr {
init.push_str(&reversed);
}
init
}
else if self.0 == 0
{
init
}
else
{
- let itr : i32 = self.0.abs();
+ let itr : i32 = self.0;
for _i in 0..itr {
init.push_str(&other.0);
}
init
};
Cell::<String>(output)
}
}
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub matrix : Vec<Vec<Cell<T>>>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut output : Vec<Vec<Cell<T>>> = Vec::new();
let mut row1 : Vec<Cell<T>> = Vec::new();
let mut row2 : Vec<Cell<T>> = Vec::new();
row1.push(Cell(data[0].clone()));
row1.push(Cell(data[1].clone()));
row2.push(Cell(data[2].clone()));
row2.push(Cell(data[3].clone()));
output.push(row1);
output.push(row2);
Matrix{matrix: output}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut output : Vec<Cell<T>> = Vec::new();
output.push(self.matrix[0][0].clone());
output.push(self.matrix[0][1].clone());
output.push(self.matrix[1][0].clone());
output.push(self.matrix[1][1].clone());
output
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut output : Vec<Cell<T>> = Vec::new();
output.push(self.matrix[0][0].clone());
output.push(self.matrix[1][0].clone());
output.push(self.matrix[0][1].clone());
output.push(self.matrix[1][1].clone());
output
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
let mut output : Vec<Vec<Cell<String>>> = Vec::new();
let mut row1 : Vec<Cell<String>> = Vec::new();
let mut row2 : Vec<Cell<String>> = Vec::new();
let el1 : Cell<String> = self.matrix[0][0].clone() + other.matrix[0][0].clone();
let el2 : Cell<String> = self.matrix[0][1].clone() + other.matrix[0][1].clone();
let el3 : Cell<String> = self.matrix[1][0].clone() + other.matrix[1][0].clone();
let el4 : Cell<String> = self.matrix[1][1].clone() + other.matrix[1][1].clone();
row1.push(el1);
row1.push(el2);
row2.push(el3);
row2.push(el4);
output.push(row1);
output.push(row2);
Matrix{matrix: output}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let output : Cell<String> = self.matrix[0][0].clone() * other.matrix[0][0].clone() +
self.matrix[0][1].clone() * other.matrix[0][1].clone() +
self.matrix[1][0].clone() * other.matrix[1][0].clone() +
self.matrix[1][1].clone() * other.matrix[1][1].clone();
output.0
}
}