Решение на Matrix 4 от Ангел Марински

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

Към профила на Ангел Марински

Резултати

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

Код

use std::ops::{Add,Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
vec: Vec<Cell<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 matrix: Matrix<T> = Matrix{vec: Vec::new()};
for i in 0..4{
matrix.vec.push(Cell(data[i].clone()));
}
matrix
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.vec.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut vector:Vec<Cell<T>> = Vec::new();
vector.push(self.vec[0].clone());
vector.push(self.vec[2].clone());
vector.push(self.vec[1].clone());
vector.push(self.vec[3].clone());
vector
}
}
impl Add<Cell<String>> for Cell<i32>{
type Output = Cell<String>;
fn add(self,other:Cell<String>) -> Self::Output{
let number = self.0;
let string = other.0;
let mut result: Cell<String> = Cell(String::from(""));
if number >= 0 {
result = Cell(format!("{} {}",number.to_string(),string));
}
else{
result = Cell(format!("{} {}",string.chars().rev().collect::<String>(),number.abs().to_string()));
}
result
}
}
impl Mul<Cell<String>> for Cell<i32>{
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Self::Output{
let mut number = self.0;
let mut string = other.0;
if number == 0 {
return Cell(String::from(""));
}
else if number < 0 {
string = string.chars().rev().collect();
number = number.abs();
}
Cell(format!("{}", string.repeat(number as usize)))
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other:Matrix<String>) -> Self::Output{
let mut result: Matrix<String> = Matrix{vec: Vec::new()};
for i in 0..4 {
result.vec.push(self.vec[i].clone() + other.vec[i].clone());
}
result
}
}
//vec[0] = 00 vec[1] = 01
//vec[2] = 10 vec[3] = 11
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> Self::Output{
let mut string: String = String::new();
let space: String = String::from(" ");
string += &(self.vec[0].clone()*other.vec[0].clone()).0;
string += &space;
string += &(self.vec[1].clone()*other.vec[2].clone()).0;
string += &space;
string += &(self.vec[2].clone()*other.vec[1].clone()).0;
string += &space;
string += &(self.vec[3].clone()*other.vec[3].clone()).0;
string
}
}
/* tests
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_cell_multiply(){
assert_eq!((Cell(3) * Cell(String::from("Max"))).0, String::from("MaxMaxMax"));
assert_eq!((Cell(-3) * Cell(String::from("emoseerht"))).0, String::from("threesomethreesomethreesome"));
assert_eq!((Cell(0) * Cell(String::from("alibaba"))).0, String::from(""));
assert_eq!((Cell(-5) * Cell(String::from(" "))).0, String::from(" "));
}
#[test]
fn test_cell_add(){
assert_eq!((Cell(7000000) + Cell(String::from("e naselenieto na BG"))).0, String::from("7000000 e naselenieto na BG"));
assert_eq!((Cell(-700) + Cell(String::from("otonizak v atuban ohcvI"))).0, String::from("Ivcho nabuta v kazinoto 700"));
assert_eq!((Cell(0) + Cell(String::from("fuck given"))).0, String::from("0 fuck given"));
assert_eq!((Cell(-21) + Cell(String::from(" "))).0, String::from(" 21"));
assert_eq!((Cell(3) + ( Cell(6) + (Cell(0) + Cell(String::from("No scope"))))).0, String::from("3 6 0 No scope"));
}
#[test]
fn test_matrix(){
let matrix1 = Matrix::new(&[-1, 0, -2, 0]);
let matrix2 = Matrix::new(&[
String::from(" "),
String::from("test"),
String::from("legna"),
String::from(" "),
]);
let result = Matrix::new(&[
String::from(" 1"),
String::from("angel 2"),
String::from("0 test"),
String::from("0 "),
]);
assert_eq!((matrix1 + matrix2).by_col(), result.by_row());
let m1 = Matrix::new(&[0, -2, -1, 4]);
let m2 = Matrix::new(&[
String::from("longlonglonglonglonglongthing"),
String::from("krow"),
String::from("krow"),
String::from("letsgo"),
]);
assert_eq!((m1 * m2), String::from(" workwork work letsgoletsgoletsgoletsgo"));
}
#[test]
#[should_panic]
fn test_panic(){
let mut cell = Cell(151262523) * Cell(String::from(""));
assert_eq!(cell.0, "");
cell = Cell(13) * (Cell(-1) + cell);
assert_eq!(cell.0, "1 1 1 1 1 1 1 1 1 1 1 1 1");
}
*/

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-433w4u/solution)
warning: value assigned to `result` is never read
  --> src/lib.rs:44:17
   |
44 |         let mut result: Cell<String> = Cell(String::from(""));
   |                 ^^^^^^
   |
   = note: `#[warn(unused_assignments)]` on by default
   = help: maybe it is overwritten before being read?

warning: `solution` (lib) generated 1 warning
    Finished test [unoptimized + debuginfo] target(s) in 7.53s
     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 версия и 0 коментара)

Ангел качи първо решение на 17.11.2021 22:45 (преди почти 4 години)