Решение на Matrix 4 от Александър Каратов
Към профила на Александър Каратов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
fn main()
{
let xs: [i32; 4] = [1, 2, 3, 1];
let ss: [String; 4] = ["one".to_string(), "two".to_string(), "three".to_string(), "four".to_string()];
//matrix.new(&xs);
let matrix1 = Matrix::<i32>::new(&xs);
let matrix2 = Matrix::<String>::new(&ss);
println!("{:?}" , matrix1);
println!("{:?}", matrix1.by_row());
println!("{:?}", matrix1.by_col());
println!("{:?}", Cell(1) + Cell("123".to_string()));
println!("{:?}", Cell(-4) + Cell("xirtam".to_string()));
println!("{:?}", Cell(3) * Cell("woah!".to_string()));
println!("{:?}", Cell(-3) * Cell(",regdab".to_string()));
println!("{:?}", Cell(0) * Cell("woah?".to_string()));
//println!("{:?}", matrix1 + matrix2 );
println!("{:?}", matrix1 * matrix2);
}
#[derive(Debug)]
pub struct Matrix<T: Clone> {
cell_1_1: Cell<T>,
cell_1_2: Cell<T>,
cell_2_1: Cell<T>,
cell_2_2: Cell<T>,
}
use std::ops::Add;
use std::ops::Mul;
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
use std::string::ToString;
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
let strcell: String = other.clone().0.to_string();
let numcell: i32 = self.clone().0;
let mut temp : String;
if numcell >= 0 {
temp = numcell.to_string();
temp.push_str(" ");
temp.push_str(&strcell);
}
else
{
temp = strcell.chars().rev().collect();
temp.push_str(" ");
temp.push_str(&(numcell.abs()).to_string());
}
return Cell(temp);
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let strcell: String = other.clone().0.to_string();
let numcell: i32 = self.clone().0;
let mut buff : String;
if numcell > 0 {
buff = "".to_string();
for i in 0..numcell {
buff.push_str(&strcell);
if i == numcell - 1
{
break;
}
}
}
else if numcell < 0
{
buff = "".to_string();
let temp: String = strcell.chars().rev().collect();
for i in 0..numcell.abs() {
buff.push_str(&temp);
if i == numcell - 1
{
break;
}
}
}
else
{
buff = "".to_string();
}
return Cell(buff);
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T>
{
Matrix {cell_1_1 : Cell(data[0].clone()), cell_1_2 : Cell(data[1].clone()), cell_2_1 : Cell(data[2].clone()), cell_2_2 : Cell(data[3].clone())}
}
pub fn by_row(&self) -> Vec<Cell<T>>
{
vec![self.cell_1_1.clone(), self.cell_1_2.clone(), self.cell_2_1.clone(), self.cell_2_2.clone()]
}
pub fn by_col(&self) -> Vec<Cell<T>>
{
vec![self.cell_1_1.clone(), self.cell_2_1.clone(), self.cell_1_2.clone(), self.cell_2_2.clone()]
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
Matrix{cell_1_1: self.cell_1_1 + other.cell_1_1,
cell_1_2: self.cell_1_2 + other.cell_1_2,
cell_2_1: self.cell_2_1 + other.cell_2_1,
cell_2_2: self.cell_2_2 + other.cell_2_2 }
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String
{
let mut buff : String = (self.cell_1_1 * other.cell_1_1).0;
buff.push_str(" ");
buff.push_str(&(self.cell_1_2 * other.cell_2_1).0);
buff.push_str(" ");
buff.push_str(&(self.cell_2_1 * other.cell_1_2).0);
buff.push_str(" ");
buff.push_str(&(self.cell_2_2 * other.cell_2_2).0);
return buff;
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-plxzcf/solution) warning: function is never used: `main` --> src/lib.rs:1:4 | 1 | fn main() | ^^^^ | = note: `#[warn(dead_code)]` on by default warning: `solution` (lib) generated 1 warning Finished test [unoptimized + debuginfo] target(s) in 7.58s 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