Решение на Matrix 4 от Никола Кралев
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug)]
pub struct Matrix<T: Clone> {
// Каквито данни ви вършат работа
rows: Vec<Vec<T>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut result = Matrix::<T> {
rows: Vec::with_capacity(2),
};
result.rows.push(Vec::with_capacity(2));
result.rows.push(Vec::with_capacity(2));
result.rows[0].push(data[0].clone());
result.rows[0].push(data[1].clone());
result.rows[1].push(data[2].clone());
result.rows[1].push(data[3].clone());
return result;
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result = Vec::<Cell<T>>::with_capacity(4);
for row in &self.rows {
for item in row {
result.push(Cell(item.clone()));
}
}
return result;
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result = Vec::<Cell<T>>::with_capacity(4);
result.push(Cell(self.rows[0][0].clone()));
result.push(Cell(self.rows[1][0].clone()));
result.push(Cell(self.rows[0][1].clone()));
result.push(Cell(self.rows[1][1].clone()));
return result;
}
}
//not realy needed, but, eh, whatever
impl<T: Clone + std::ops::Add<Output = T>> std::ops::Add<Matrix<T>> for Matrix<T> {
type Output = Matrix<T>;
fn add(self, other: Self::Output) -> Self::Output {
return Matrix::new(&[
self.rows[0][0].clone() + other.rows[0][0].clone(),
self.rows[0][1].clone() + other.rows[0][1].clone(),
self.rows[1][0].clone() + other.rows[1][0].clone(),
self.rows[1][1].clone() + other.rows[1][1].clone(),
]);
}
}
//same
impl<T: Clone + std::ops::Mul<Output = T> + std::ops::Add<Output = T>> std::ops::Mul<Matrix<T>>
for Matrix<T>
{
type Output = Matrix<T>;
fn mul(self, other: Self::Output) -> Self::Output {
return Matrix::new(&[
self.rows[0][0].clone() * other.rows[0][0].clone()
+ self.rows[0][1].clone() * other.rows[1][0].clone(),
self.rows[0][0].clone() * other.rows[0][1].clone()
+ self.rows[0][1].clone() * other.rows[1][1].clone(),
self.rows[1][0].clone() * other.rows[0][0].clone()
+ self.rows[1][1].clone() * other.rows[1][0].clone(),
self.rows[1][0].clone() * other.rows[0][1].clone()
+ self.rows[1][1].clone() * other.rows[1][1].clone(),
]);
}
}
//this can break sometimes for utf-8
//https://stackoverflow.com/questions/27996430/reversing-a-string-in-rust
fn reverse(data: String) -> String {
return data.chars().rev().collect::<String>();
}
impl std::ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
return Cell(self.0.to_string() + " " + other.0.as_str());
}
//make sure that i32.min() converted to a positive does not overflow
let mut num = self.0 as i64;
num *= -1_i64;
let reversed_str = reverse(other.0);
return Cell(reversed_str + " " + num.to_string().as_str());
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
if self.0 == 0 {
return Cell("".to_string());
}
if self.0 > 0 {
let mut result = String::from("");
for _i in 0..self.0 {
result += other.0.as_str();
}
return Cell(result);
} else {
//make sure that i32.min() converted to a positive does not overflow
let mut num = self.0 as i64;
num *= -1_i64;
let reversed_str = reverse(other.0);
let mut result = String::from("");
for _i in 0..num {
result += reversed_str.as_str();
}
return Cell(result);
}
}
}
impl std::ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Self::Output) -> Self::Output {
return Matrix::new(&[
(Cell(self.rows[0][0].clone()) + Cell(other.rows[0][0].clone())).0,
(Cell(self.rows[0][1].clone()) + Cell(other.rows[0][1].clone())).0,
(Cell(self.rows[1][0].clone()) + Cell(other.rows[1][0].clone())).0,
(Cell(self.rows[1][1].clone()) + Cell(other.rows[1][1].clone())).0,
]);
}
}
impl std::ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output {
return (Cell(self.rows[0][0].clone()) * Cell(other.rows[0][0].clone())).0
+ " "
+ (Cell(self.rows[0][1].clone()) * Cell(other.rows[1][0].clone()))
.0
.as_str()
+ " "
+ (Cell(self.rows[1][0].clone()) * Cell(other.rows[0][1].clone()))
.0
.as_str()
+ " "
+ (Cell(self.rows[1][1].clone()) * Cell(other.rows[1][1].clone()))
.0
.as_str();
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-1s4hzl0/solution) Finished test [unoptimized + debuginfo] target(s) in 8.29s 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
История (2 версии и 0 коментара)
Никола качи решение на 17.11.2021 00:25 (преди почти 4 години)
#[derive(Debug)]
pub struct Matrix<T: Clone> {
// Каквито данни ви вършат работа
rows: Vec<Vec<T>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut result = Matrix::<T> {
rows: Vec::with_capacity(2),
};
result.rows.push(Vec::with_capacity(2));
result.rows.push(Vec::with_capacity(2));
result.rows[0].push(data[0].clone());
result.rows[0].push(data[1].clone());
result.rows[1].push(data[2].clone());
result.rows[1].push(data[3].clone());
return result;
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result = Vec::<Cell<T>>::with_capacity(4);
for row in &self.rows {
for item in row {
result.push(Cell(item.clone()));
}
}
return result;
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result = Vec::<Cell<T>>::with_capacity(4);
result.push(Cell(self.rows[0][0].clone()));
result.push(Cell(self.rows[1][0].clone()));
result.push(Cell(self.rows[0][1].clone()));
result.push(Cell(self.rows[1][1].clone()));
return result;
}
}
+//not realy needed, but, eh, whatever
impl<T: Clone + std::ops::Add<Output = T>> std::ops::Add<Matrix<T>> for Matrix<T> {
type Output = Matrix<T>;
fn add(self, other: Self::Output) -> Self::Output {
return Matrix::new(&[
self.rows[0][0].clone() + other.rows[0][0].clone(),
self.rows[0][1].clone() + other.rows[0][1].clone(),
self.rows[1][0].clone() + other.rows[1][0].clone(),
self.rows[1][1].clone() + other.rows[1][1].clone(),
]);
}
}
+//same
impl<T: Clone + std::ops::Mul<Output = T> + std::ops::Add<Output = T>> std::ops::Mul<Matrix<T>>
for Matrix<T>
{
type Output = Matrix<T>;
fn mul(self, other: Self::Output) -> Self::Output {
return Matrix::new(&[
self.rows[0][0].clone() * other.rows[0][0].clone()
+ self.rows[0][1].clone() * other.rows[1][0].clone(),
self.rows[0][0].clone() * other.rows[0][1].clone()
+ self.rows[0][1].clone() * other.rows[1][1].clone(),
self.rows[1][0].clone() * other.rows[0][0].clone()
+ self.rows[1][1].clone() * other.rows[1][0].clone(),
self.rows[1][0].clone() * other.rows[0][1].clone()
+ self.rows[1][1].clone() * other.rows[1][1].clone(),
]);
}
}
//this can break sometimes for utf-8
//https://stackoverflow.com/questions/27996430/reversing-a-string-in-rust
fn reverse(data: String) -> String {
return data.chars().rev().collect::<String>();
}
impl std::ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
return Cell(self.0.to_string() + " " + other.0.as_str());
}
//make sure that i32.min() converted to a positive does not overflow
let mut num = self.0 as i64;
num *= -1_i64;
let reversed_str = reverse(other.0);
return Cell(reversed_str + " " + num.to_string().as_str());
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
if self.0 == 0 {
return Cell("".to_string());
}
if self.0 > 0 {
let mut result = String::from("");
for _i in 0..self.0 {
result += other.0.as_str();
}
return Cell(result);
} else {
//make sure that i32.min() converted to a positive does not overflow
let mut num = self.0 as i64;
num *= -1_i64;
let reversed_str = reverse(other.0);
let mut result = String::from("");
for _i in 0..num {
result += reversed_str.as_str();
}
return Cell(result);
}
}
}
impl std::ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Self::Output) -> Self::Output {
return Matrix::new(&[
(Cell(self.rows[0][0].clone()) + Cell(other.rows[0][0].clone())).0,
(Cell(self.rows[0][1].clone()) + Cell(other.rows[0][1].clone())).0,
(Cell(self.rows[1][0].clone()) + Cell(other.rows[1][0].clone())).0,
(Cell(self.rows[1][1].clone()) + Cell(other.rows[1][1].clone())).0,
]);
}
}
impl std::ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output {
- return
- (Cell(self.rows[0][0].clone()) * Cell(other.rows[0][0].clone())).0 +
- " " +
- (Cell(self.rows[0][1].clone()) * Cell(other.rows[1][0].clone())).0.as_str() +
- " " +
- (Cell(self.rows[1][0].clone()) * Cell(other.rows[0][1].clone())).0.as_str() +
- " " +
- (Cell(self.rows[1][1].clone()) * Cell(other.rows[1][1].clone())).0.as_str();
+ return (Cell(self.rows[0][0].clone()) * Cell(other.rows[0][0].clone())).0
+ + " "
+ + (Cell(self.rows[0][1].clone()) * Cell(other.rows[1][0].clone()))
+ .0
+ .as_str()
+ + " "
+ + (Cell(self.rows[1][0].clone()) * Cell(other.rows[0][1].clone()))
+ .0
+ .as_str()
+ + " "
+ + (Cell(self.rows[1][1].clone()) * Cell(other.rows[1][1].clone()))
+ .0
+ .as_str();
}
}