Решение на Matrix 4 от Кристиян Цветанов

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

Към профила на Кристиян Цветанов

Резултати

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

Код

use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub matrix: 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> {
Self {
matrix: vec![data[0].clone(), data[1].clone(), data[2].clone(), data[3].clone()],
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut v: Vec<Cell<T>> = Vec::new();
for elem in self.matrix.iter() {
v.push(Cell(elem.clone()));
}
v
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut v: Vec<Cell<T>> = Vec::new();
let arr = [0, 2, 1, 3];
for i in arr {
v.push(Cell(self.matrix[i].clone()));
}
v
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
Cell(self.0.to_string() + " " + &rhs.0)
} else {
Cell(rhs.0.chars().rev().collect::<String>() + " " + &self.0.abs().to_string())
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Cell<String> {
let mut s = String::new();
let mut i = 0;
if self.0 >= 0 {
while i < self.0 {
s.push_str(&rhs.0);
i = i + 1;
}
} else {
let rev = rhs.0.chars().rev().collect::<String>();
while i < self.0.abs() {
s.push_str(&rev);
i = i + 1;
}
}
Cell(s)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let v1: Vec<Cell<i32>> = self.by_row();
let v2: Vec<Cell<String>> = rhs.by_row();
let sum: Vec<String> = v1.into_iter()
.zip(v2)
.map(|(a, b)| a + b)
.map(|x| x.0)
.collect::<Vec<_>>();
let arr = [sum[0].clone(), sum[1].clone(), sum[2].clone(), sum[3].clone()];
Matrix::new(&arr)
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let v1: Vec<Cell<i32>> = self.by_row();
let v2: Vec<Cell<String>> = rhs.by_col();
let product: Vec<Cell<String>> = v1.into_iter()
.zip(v2)
.map(|(a, b)| a * b)
.collect::<Vec<_>>();
let mut s = String::new();
let mut i = 0;
while i < 3 {
s.push_str(&product[i].0);
s.push(' ');
i = i + 1;
}
s.push_str(&product[i].0);
s
}
}

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

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

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

Кристиян качи първо решение на 15.11.2021 17:51 (преди почти 4 години)