Решение на Matrix 4 от Радостина Йошева

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

Към профила на Радостина Йошева

Резултати

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

Код

use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
data: 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 result = data.to_vec().iter().cloned().map(|x| Cell{0: x}).collect();
Matrix{data: result}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.data.clone()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut mut_vec = self.data.clone();
mut_vec.swap(1, 2);
mut_vec
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
let mut result = Vec::new();
for elem in 0..4 {
result.push(self.data[elem].clone() + other.data[elem].clone());
}
Matrix{data: result}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let mut result_matrix = Vec::new();
let matrix1 = self.by_row();
let matrix2 = other.by_col();
for elem in 0..4 {
result_matrix.push(matrix1[elem].clone() * matrix2[elem].clone());
}
let transformed: Vec<String> = result_matrix.iter().cloned().map(|x| x.0).collect();
transformed.join(" ")
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
if self.0 < 0 {
let reversed: String = other.0.chars().rev().collect();
let abs_value: i32 = i32::abs(self.0);
Cell{0: format!("{} {}", reversed, abs_value)}
} else {
Cell{0: format!("{} {}", self.0, other.0)}
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
if self.0 < 0 {
let reversed: String = other.0.chars().rev().collect();
let num_repetitions: usize = i32::abs(self.0) as usize;
Cell{0: reversed.repeat(num_repetitions)}
} else {
Cell{0: other.0.repeat(self.0 as usize)}
}
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-eegdzy/solution)
    Finished test [unoptimized + debuginfo] target(s) in 8.49s
     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 00:24 (преди почти 4 години)