Решение на Matrix 4 от Константин Константинов
Към профила на Константин Константинов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::{ops::{Add, Mul}, usize};
#[derive(Debug, Clone)]
pub struct Matrix<T: Clone> {
matrix : [[Cell<T> ; 2] ; 2]
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix{
matrix:[[Cell(data[0].clone()),Cell(data[1].clone())],
[Cell(data[2].clone()),Cell(data[3].clone())]]
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let row = self.matrix[0].clone();
let row2 = self.matrix[1].clone();
[row,row2].concat().to_vec()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
[self.matrix[0][0].clone(),self.matrix[1][0].clone(),self.matrix[0][1].clone(),self.matrix[1][1].clone()].to_vec()
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Self::Output {
if self.0 >= 0
{
Cell(format!("{} {}", self.0 , rhs.0))
}else {
Cell(format!("{} {}", rhs.0.chars().rev().collect::<String>(), self.0.abs()))
}
}
}
impl Mul<Cell<String>> for Cell<i32>
{
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
if self.0>=0
{
Cell(format!("{}", rhs.0.repeat(self.0 as usize)))
}else {
Cell(format!("{}", rhs.0.chars().rev().collect::<String>().repeat(self.0.abs() as usize)))
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Self::Output {
Matrix::new(&[ (self.matrix[0][0].clone() + rhs.matrix[0][0].clone()).0,
(self.matrix[0][1].clone() + rhs.matrix[0][1].clone()).0,
(self.matrix[1][0].clone() + rhs.matrix[1][0].clone()).0,
(self.matrix[1][1].clone() + rhs.matrix[1][1].clone()).0
])
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
format!("{} {} {} {}", (self.by_row()[0].clone() * rhs.by_col()[0].clone()).0,
(self.by_row()[1].clone() * rhs.by_col()[1].clone()).0,
(self.by_row()[2].clone() * rhs.by_col()[2].clone()).0,
(self.by_row()[3].clone() * rhs.by_col()[3].clone()).0
)
}
}
#[test]
fn test_cell_addition()
{
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
}
#[test]
fn test_cell_multiplication()
{
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!( Cell(-5) * Cell(String::from(" !delzoobmaB")),
Cell(String::from("Bamboozled! Bamboozled! Bamboozled! Bamboozled! Bamboozled! ")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
}
#[test]
fn test_matrix_multiplication()
{
assert_eq!( Matrix::new(&[1,2,3,4]) * Matrix::new(&[String::from("one"),
String::from("two"), String::from("three"),String::from("four")]),"one threethree twotwotwo fourfourfourfour");
assert_eq!( Matrix::new(&[-1,-2,-3,-4]) * Matrix::new(&[String::from("eno"),
String::from("owt"), String::from("eerht"),String::from("ruof")]),"one threethree twotwotwo fourfourfourfour");
assert_eq!( Matrix::new(&[0,-2,4,-4]) * Matrix::new(&[String::from("disappear"),
String::from("four"), String::from("owt"),String::from("ruof")])," twotwo fourfourfourfour fourfourfourfour");
}
#[test]
fn test_matrix_addition()
{
let matrix_int = Matrix::new(&[1, -2, -3, 4]);
let matrix_str = Matrix::new(&[
String::from("one"), String::from("owt"),
String::from("eerht"), String::from("four")
]);
let matrix_check = Matrix::new(&[
String::from("1 one"), String::from("two 2"),
String::from("three 3"), String::from("4 four")
]);
assert_eq!((matrix_int.clone() + matrix_str.clone()).by_row(), matrix_check.by_row());
assert_eq!((matrix_int.clone() + matrix_str.clone()).by_col(), matrix_check.by_col());
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-123yukx/solution) Finished test [unoptimized + debuginfo] target(s) in 7.84s 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 версии и 1 коментар)
Константин качи решение на 18.11.2021 16:20 (преди почти 4 години)
use std::{ops::{Add, Mul}, usize};
#[derive(Debug, Clone)]
pub struct Matrix<T: Clone> {
matrix : [[Cell<T> ; 2] ; 2]
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix{
matrix:[[Cell(data[0].clone()),Cell(data[1].clone())],
[Cell(data[2].clone()),Cell(data[3].clone())]]
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let row = self.matrix[0].clone();
let row2 = self.matrix[1].clone();
[row,row2].concat().to_vec()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
[self.matrix[0][0].clone(),self.matrix[1][0].clone(),self.matrix[0][1].clone(),self.matrix[1][1].clone()].to_vec()
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Self::Output {
if self.0 >= 0
{
Cell(format!("{} {}", self.0 , rhs.0))
}else {
Cell(format!("{} {}", rhs.0.chars().rev().collect::<String>(), self.0.abs()))
}
}
}
impl Mul<Cell<String>> for Cell<i32>
{
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
if self.0>=0
{
Cell(format!("{}", rhs.0.repeat(self.0 as usize)))
}else {
Cell(format!("{}", rhs.0.chars().rev().collect::<String>().repeat(self.0.abs() as usize)))
}
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Self::Output {
Matrix::new(&[ (self.matrix[0][0].clone() + rhs.matrix[0][0].clone()).0,
(self.matrix[0][1].clone() + rhs.matrix[0][1].clone()).0,
(self.matrix[1][0].clone() + rhs.matrix[1][0].clone()).0,
(self.matrix[1][1].clone() + rhs.matrix[1][1].clone()).0
])
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
format!("{} {} {} {}", (self.by_row()[0].clone() * rhs.by_col()[0].clone()).0,
(self.by_row()[1].clone() * rhs.by_col()[1].clone()).0,
(self.by_row()[2].clone() * rhs.by_col()[2].clone()).0,
(self.by_row()[3].clone() * rhs.by_col()[3].clone()).0
)
}
+}
+
+
+#[test]
+fn test_cell_addition()
+{
+ assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
+ assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
+ assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
+}
+
+#[test]
+fn test_cell_multiplication()
+{
+ assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
+ assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
+ assert_eq!( Cell(-5) * Cell(String::from(" !delzoobmaB")),
+ Cell(String::from("Bamboozled! Bamboozled! Bamboozled! Bamboozled! Bamboozled! ")));
+ assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
+}
+
+#[test]
+fn test_matrix_multiplication()
+{
+ assert_eq!( Matrix::new(&[1,2,3,4]) * Matrix::new(&[String::from("one"),
+ String::from("two"), String::from("three"),String::from("four")]),"one threethree twotwotwo fourfourfourfour");
+
+ assert_eq!( Matrix::new(&[-1,-2,-3,-4]) * Matrix::new(&[String::from("eno"),
+ String::from("owt"), String::from("eerht"),String::from("ruof")]),"one threethree twotwotwo fourfourfourfour");
+
+ assert_eq!( Matrix::new(&[0,-2,4,-4]) * Matrix::new(&[String::from("disappear"),
+ String::from("four"), String::from("owt"),String::from("ruof")])," twotwo fourfourfourfour fourfourfourfour");
+}
+
+#[test]
+fn test_matrix_addition()
+{
+ let matrix_int = Matrix::new(&[1, -2, -3, 4]);
+ let matrix_str = Matrix::new(&[
+ String::from("one"), String::from("owt"),
+ String::from("eerht"), String::from("four")
+ ]);
+ let matrix_check = Matrix::new(&[
+ String::from("1 one"), String::from("two 2"),
+ String::from("three 3"), String::from("4 four")
+ ]);
+ assert_eq!((matrix_int.clone() + matrix_str.clone()).by_row(), matrix_check.by_row());
+ assert_eq!((matrix_int.clone() + matrix_str.clone()).by_col(), matrix_check.by_col());
}
Не си тествал с unicode.