Решение на Matrix 4 от Георги Чобанов

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

Към профила на Георги Чобанов

Резултати

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

Код

use std::ops::Add;
use std::ops::Mul;
#[derive(Debug)]
pub struct Matrix<T: Clone> {
elements: Vec<Cell<T>>
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
//add and mul for Cell
//add Cell
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String> ) -> Cell<String> {
if self.0 >= 0 {
Cell(String::from(format!("{} {}",self.0, rhs.0)))
} else {
let str_reveresed: String = rhs.0.clone();
Cell(String::from(format!("{} {}", str_reveresed.chars().rev().collect::<String>(), -1*self.0)))
}
}
}
//mul Cell
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String> ) -> Cell<String> {
let str_clone: String = if self.0 < 0 {
rhs.0.clone().chars().rev().collect::<String>()
} else {
rhs.0.clone()
};
let times: i32 = if self.0 < 0 {
self.0*-1
} else {
self.0
};
let mut result: String = String::from("");
let mut i = 0;
while i < times {
result.push_str(&str_clone.clone());
i += 1;
}
Cell(String::from(result))
}
}
//Matrix
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
let data_clone = data.clone();
let mut elements_to_put: Vec<Cell<T>> = Vec::new();
for di in data_clone {
let to_put = Cell(di);
elements_to_put.push(to_put);
}
Matrix { elements: elements_to_put }
}
//by_row
pub fn by_row(&self) -> Vec<Cell<T>> {
let to_return: Vec<Cell<T>> = self.elements.clone();
to_return
}
//by_col
pub fn by_col(&self) -> Vec<Cell<T>> {
let elements_clone: Vec<Cell<T>> = self.elements.clone();
let mut col1: Vec<Cell<T>> = Vec::new();
let mut col2: Vec<Cell<T>> = Vec::new();
let mut i = 0;
for el in elements_clone {
if i%2 == 0 {
col1.push(el);
} else {
col2.push(el);
}
i += 1;
}
let mut to_return: Vec<Cell<T>> = Vec::new();
for el in col1 {
to_return.push(el);
}
for el in col2 {
to_return.push(el);
}
to_return
}
}
// add matrix
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Matrix<String> {
let self_matrix: Vec<Cell<i32>> = self.elements.clone();
let other_matrix: Vec<Cell<String>> = rhs.elements.clone();
let mut result: Vec<Cell<String>> = Vec::new();
let mut i = 0;
while i < 4 {
result.push(self_matrix[i].clone() + other_matrix[i].clone());
i += 1;
}
Matrix::new(&[result[0].0.clone(),result[1].0.clone(),result[2].0.clone(),result[3].0.clone()])
}
}
// mul matrix haha poluchih nervna kriza tova moje da ne e efektivno za koda
// no e efektivno za psihikata mi *ravecrab* *ravecarb* *ravecrab*
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> String {
let self_matrix: Vec<Cell<i32>> = self.elements.clone();
let other_matrix: Vec<Cell<String>> = rhs.elements.clone();
let r0_c0: Cell<String> = self_matrix[0].clone() * other_matrix[0].clone();
let r1_c2: Cell<String> = self_matrix[1].clone() * other_matrix[2].clone();
let r2_c1: Cell<String> = self_matrix[2].clone() * other_matrix[1].clone();
let r3_c3: Cell<String> = self_matrix[3].clone() * other_matrix[3].clone();
String::from(format!("{} {} {} {}", r0_c0.0.clone(), r1_c2.0.clone(), r2_c1.0.clone(), r3_c3.0.clone()))
}

Работи, няма лошо :). Мисля че няма нужда да клонираш елементите на първите два реда. Anyway, може да разгледаш решенията на колегите за още вдъхновение, като приключи срока и психиката ти се е поуспокоила малко hopefully :)

}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-fiaw4e/solution)
    Finished test [unoptimized + debuginfo] target(s) in 8.02s
     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 версия и 1 коментар)

Георги качи първо решение на 17.11.2021 19:46 (преди почти 4 години)