Решение на Matrix 4 от Георги Попов

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

Към профила на Георги Попов

Резултати

  • 20 точки от тестове
  • 0 бонус точки
  • 20 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub a: Cell<T>,
pub b: Cell<T>,
pub c: Cell<T>,
pub d: Cell<T>,
pub arr: Vec<T>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let result: Matrix<String> = Matrix::new(&[(self.a.clone() + rhs.a.clone()).0, (self.b.clone() + rhs.b.clone()).0, (self.c.clone() + rhs.c.clone()).0, (self.d.clone() + rhs.d.clone()).0]);
result
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let mult_a: Vec<Cell<i32>> = self.by_row();
let mult_b: Vec<Cell<String>> = rhs.by_col();
let mut result: String = String::new();
let mut index: usize = 0;
while index < 3 {
result.push_str(&(mult_a[index].clone() * mult_b[index].clone()).0);
result.push_str(" ");
index+=1;
}
result.push_str(&(mult_a[3].clone() * mult_b[3].clone()).0);
result
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
let mut result = Cell::<String> {
0: String::from(""),
};
if self.0 >= 0 {
let mut concat = self.0.to_string();
concat.push_str(" ");
concat.push_str(&rhs.0);
result.0 = concat;
} else {
let number_to_string: String = (self.0 * -1).to_string();
let mut copy: String = rhs.0.clone();
let mut reversed_copy: String = String::new();
while let Some(letter) = copy.pop() {
reversed_copy.push(letter)
}
reversed_copy.push_str(" ");
reversed_copy.push_str(&number_to_string);
result.0 = reversed_copy;
}
result
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut result_cell = Cell::<String> {
0: String::from(""),
};
let mut repetitions_left: i32;
let mut string_to_repeat: String = String::new();
let mut result_string: String = String::new();
if self.0 >= 0 {
string_to_repeat = rhs.0.clone();
repetitions_left = self.0.clone();
} else {
let mut copy: String = rhs.0.clone();
while let Some(letter) = copy.pop() {
string_to_repeat.push(letter)
}
repetitions_left = (self.0 * -1).clone()
}
while repetitions_left > 0 {
result_string.push_str(&string_to_repeat);
repetitions_left-=1;
}
result_cell.0 = result_string;
result_cell
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut matrix = Matrix::<T> {
a: Cell::<T> {
0: data[0].clone(),
},
b: Cell::<T> {
0: data[1].clone(),
},
c: Cell::<T> {
0: data[2].clone(),
},
d: Cell::<T> {
0: data[3].clone(),
},
arr: Vec::new()
};
for elem in data {
matrix.arr.push(elem.clone())
}
matrix
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut by_row = Vec::new();
by_row.push(self.a.clone());
by_row.push(self.b.clone());
by_row.push(self.c.clone());
by_row.push(self.d.clone());
by_row
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut by_col = Vec::new();
by_col.push(self.a.clone());
by_col.push(self.c.clone());
by_col.push(self.b.clone());
by_col.push(self.d.clone());
by_col
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-1ii6vas/solution)
    Finished test [unoptimized + debuginfo] target(s) in 7.40s
     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

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

Георги качи първо решение на 13.11.2021 20:04 (преди почти 4 години)