Решение на Matrix 4 от Аристотелис Папанис

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

Към профила на Аристотелис Папанис

Резултати

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

Код

#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cells: Vec<Cell<T>>,
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let mut vec: Vec<Cell<T>> = Vec::new();
for i in data {
vec.push(Cell(i.clone()));
}
Matrix::<T> { cells: vec.clone() }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.cells.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut vec = self.cells.clone();
vec.swap(1, 2);
vec
}
}
impl std::ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
if self.0 < 0 {
return Cell(
rhs.0.clone().chars().rev().collect::<String>()
+ " "
+ &self.0.clone().abs().to_string(),
);
}
Cell(self.0.to_string() + " " + &rhs.0.clone())
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut counter: i32 = self.0.clone();
let mut result: String = String::from("");
if self.0 == 0 {
return Cell(String::from(""));
}
if self.0 > 0 {
while counter > 0 {
counter = counter - 1;
result = result + &rhs.0.clone();
}
return Cell(result);
}
while counter < 0 {
counter = counter + 1;
result = result + &rhs.0.clone().chars().rev().collect::<String>();
}
return Cell(result);
}
}
impl std::ops::Add<Matrix<String>> for Matrix<i32>{
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let mut iter1 = self.cells.iter();
let mut iter2 = rhs.cells.iter();
let mut res:Vec<Cell<String>> = Vec::new();
while let (Some(Cell(num)), Some(Cell(niz))) = (iter1.next(), iter2.next()){
res.push(Cell(num.clone()) + Cell(niz.clone()));
}
Matrix {
cells: (res)
}
}
}
impl std::ops::Mul<Matrix<String>> for Matrix<i32>{
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let (row,col) = (self.by_row(),rhs.by_col());
let mut iter1 = row.iter();
let mut iter2 = col.iter();
let mut res:Vec<Cell<String>> = Vec::new();
while let (Some(num_cell), Some(str_cell)) = (iter1.next(), iter2.next()){
res.push(num_cell.clone() * str_cell.clone());
}
let mut res :String = res.iter()
.map(|x| x.0.clone() + " ")
.collect::<String>();
res.pop();
res
}
}
impl PartialEq for Matrix<String> {
fn eq(&self, other: &Self) -> bool {
let mut iter1 = self.cells.iter();
let mut iter2 = other.cells.iter();
let mut result:bool = true;
while let (Some (Cell(str1)),Some (Cell(str2))) = (iter1.next(),iter2.next()){
if str1 != str2{
result = false;
return result;
}
}
result
}
}
#[cfg(test)]
mod tests {
use crate::{Cell, Matrix};
#[test]
fn test_matrix_by_row() {
let mat = Matrix::new(&[1, 2, 3, 4]);
assert_eq!(mat.by_row(), vec![Cell(1), Cell(2), Cell(3), Cell(4)]);
}
#[test]
fn test_matrix_by_col() {
let mat = Matrix::new(&[1, 2, 3, 4]);
assert_eq!(mat.by_col(), vec![Cell(1), Cell(3), Cell(2), Cell(4)]);
}
#[test]
fn test_cell_i32_add_string() {
let cell0: Cell<i32> = Cell(-4);
let cell1: Cell<i32> = Cell(6);
let cell2: Cell<String> = Cell("Desserts".to_string());
assert_eq!(
cell1.clone() + cell2.clone(),
Cell("6 Desserts".to_string())
);
assert_eq!(cell0 + cell2, Cell("stresseD 4".to_string()));
}
#[test]
fn test_cell_mul() {
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(""))
);
}
#[test]
fn test_cell_mul_negative() {
assert_eq!(
Cell(-3) * Cell(String::from(",regdab")),
Cell(String::from("badger,badger,badger,"))
);
assert_eq!(
Cell(-1) * Cell(String::from("mere peace")),
Cell(String::from("ecaep erem"))
);
}
#[test]
fn test_cell_add() {
assert_eq!(Cell(22) + Cell(String::from("hello friend")), Cell(String::from("22 hello friend")));
}
#[test]
fn test_cell_negative() {
assert_eq!(Cell(-57) + Cell(String::from("doG")), Cell(String::from("God 57")));
}
#[test]
fn test_cell_mul_positive() {
let cell_num: Cell<i32> = Cell(3);
let cell_str: Cell<String> = Cell(String::from("Rf t"));
assert_eq!(cell_num * cell_str, Cell(String::from("Rf tRf tRf t")))
}
#[test]
fn test_cell_mul_zero() {
let cell_num: Cell<i32> = Cell(0);
let cell_str: Cell<String> = Cell(String::from("SchwerePunkt"));
assert_eq!(cell_num * cell_str, Cell(String::from("")))
}
#[test]
fn test_matrix_add() {
let matrix_num: Matrix<i32> = Matrix::new(&[4, 3, 2, 1]);
let matrix_str: Matrix<String> = Matrix::new(&[
String::from("one"),
String::from("two"),
String::from("three"),
String::from("four"),
]);
assert_eq!(
matrix_num + matrix_str,
Matrix::new(&[
String::from("4 one"),
String::from("3 two"),
String::from("2 three"),
String::from("1 four")
])
);
let matrix_num_negative: Matrix<i32> = Matrix::new(&[-4, -3, -2, -1]);
let matrix_str_negative: Matrix<String> = Matrix::new(&[
String::from("one"),
String::from("two"),
String::from("three"),
String::from("four"),
]);
assert_eq!(
matrix_num_negative + matrix_str_negative,
Matrix::new(&[
String::from("eno 4"),
String::from("owt 3"),
String::from("eerht 2"),
String::from("ruof 1")
])
);
}
#[test]
fn test_matrix_positive_mul() {
let matrix_num: Matrix<i32> = Matrix::new(&[1, 2, 3, 1]);
let matrix_str: Matrix<String> = Matrix::new(&[
String::from("one"),
String::from("two"),
String::from("three"),
String::from("you get it"),
]);
assert_eq!(
matrix_num * matrix_str,
"one threethree twotwotwo you get it"
);
}
#[test]
fn test_matrix_mul_zeroes() {
let matrix_num: Matrix<i32> = Matrix::new(&[0, 0, 0, 0]);
let matrix_str: Matrix<String> = Matrix::new(&[
String::from("one"),
String::from("two"),
String::from("three"),
String::from("you get it"),
]);
assert_eq!(
matrix_num * matrix_str,
" " //Три спейса
);
}
#[test]
fn test_matrix_negative_mul() {
let matrix_num: Matrix<i32> = Matrix::new(&[-1, 0, -2, -1]);
let matrix_str: Matrix<String> = Matrix::new(&[
String::from("one"),
String::from("two"),
String::from("three"),
String::from("you get it"),
]);
assert_eq!(
matrix_num * matrix_str,
"eno owtowt ti teg uoy"
);
}
fn string_cell_vec(s1: &str, s2: &str, s3: &str, s4: &str) -> Vec<Cell<String>> {
[s1, s2, s3, s4].into_iter().map(String::from).map(Cell).collect::<Vec<Cell<String>>>()
}
#[test]
fn test_basic() {
assert_eq!((Cell(4) + Cell(String::from("badger"))).0, String::from("4 badger"));
assert_eq!((Cell(2) * Cell(String::from("mushroom"))).0, String::from("mushroommushroom"));
let matrix1 = Matrix::new(&[1, 2, 3, 4]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1.by_row()[0], Cell(1));
assert_eq!(matrix1.by_col()[0], Cell(1));
assert_eq!(
(matrix1 + matrix2).by_row(),
string_cell_vec("1 one", "2 two", "3 three", "4 four")
);
let matrix1 = Matrix::new(&[1, 1, 1, 1]);
let matrix2 = Matrix::new(&[
String::from("one"), String::from("two"),
String::from("three"), String::from("four")
]);
assert_eq!(matrix1 * matrix2, String::from("one three two four"));
}
}

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

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

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

Аристотелис качи първо решение на 14.11.2021 17:11 (преди почти 4 години)