Решение на Matrix 4 от Кристиан Николов

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

Към профила на Кристиан Николов

Резултати

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

Код

use std::{ops, process::Output};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
rows: u32,
cols: u32,
matrix: 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 all_elements = Vec::new();
let mut temp_row = Vec::new();
let mut index = 0;
for value in data {
if index == 2 {
all_elements.push(temp_row.clone());
temp_row.clear();
}
temp_row.push(Cell(value.clone()));
index += 1;
}
all_elements.push(temp_row);
Matrix {
rows: 2,
cols: 2,
matrix: all_elements
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut elements = Vec::new();
for row in self.matrix.iter() {
for element in row.iter() {
elements.push(element.clone());
}
}
elements
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut elements = Vec::new();
for col in 0..self.cols {
for row in 0..self.rows {
elements.push(self.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone());
}
}
elements
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _rhs: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
return Cell(format!("{}{}{}", self.0, " ", _rhs.0));
}
Cell(format!("{}{}{}", _rhs.0.chars().rev().collect::<String>(), " ", self.0.abs()))
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _rhs: Cell<String>) -> Cell<String> {
let mut result: String = String::new();
let mut value: String = String::from(_rhs.0);
if (self.0 < 0) {
value = value.chars().rev().collect();
}
for index in 0..self.0.abs() {
result.push_str(&value);
}
Cell(result)
}
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, _rhs: Matrix<String>) -> Matrix<String> {
let mut new_matrix_values: [String; 4] = Default::default();
let mut index: usize = 0;
for row in 0..self.rows {
for col in 0..self.cols {
new_matrix_values[index] = (self.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone()
+ _rhs.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone()).0;
index += 1;
}
}
Matrix::new(&new_matrix_values)
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, _rhs: Matrix<String>) -> String {
let mut result: String = String::new();
let mut value: String = String::new();
for row in 0..self.rows {
for col in 0..self.cols {
value = (self.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone()
* _rhs.matrix.get(col as usize).unwrap().get(row as usize).unwrap().clone()).0;
result.push_str(&value);
result.push(' ');
}
}
result.pop();
result
}
}

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

Compiling solution v0.1.0 (/tmp/d20220112-2706256-9gisd6/solution)
warning: unused import: `process::Output`
 --> src/lib.rs:1:16
  |
1 | use std::{ops, process::Output};
  |                ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unnecessary parentheses around `if` condition
  --> src/lib.rs:70:12
   |
70 |         if (self.0 < 0) {
   |            ^^^^^^^^^^^^ help: remove these parentheses
   |
   = note: `#[warn(unused_parens)]` on by default

warning: unused variable: `index`
  --> src/lib.rs:73:13
   |
73 |         for index in 0..self.0.abs() {
   |             ^^^^^ help: if this is intentional, prefix it with an underscore: `_index`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: value assigned to `value` is never read
   --> src/lib.rs:100:17
    |
100 |         let mut value: String = String::new();
    |                 ^^^^^
    |
    = note: `#[warn(unused_assignments)]` on by default
    = help: maybe it is overwritten before being read?

warning: `solution` (lib) generated 4 warnings
    Finished test [unoptimized + debuginfo] target(s) in 8.12s
     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

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

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

Кристиан качи решение на 18.11.2021 12:27 (преди почти 4 години)

use std::{ops, process::Output};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
rows: u32,
cols: u32,
matrix: 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 all_elements = Vec::new();
let mut temp_row = Vec::new();
let mut index = 0;
for value in data {
if index == 2 {
all_elements.push(temp_row.clone());
temp_row.clear();
}
temp_row.push(Cell(value.clone()));
index += 1;
}
all_elements.push(temp_row);
Matrix {
rows: 2,
cols: 2,
matrix: all_elements
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut elements = Vec::new();
for row in self.matrix.iter() {
for element in row.iter() {
elements.push(element.clone());
}
}
elements
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut elements = Vec::new();
for col in 0..self.cols {
for row in 0..self.rows {
elements.push(self.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone());
}
}
elements
}
}
impl ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _rhs: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
return Cell(format!("{}{}{}", self.0, " ", _rhs.0));
}
- Cell(format!("{}{}{}", _rhs.0, " ", self.0.abs()))
+ Cell(format!("{}{}{}", _rhs.0.chars().rev().collect::<String>(), " ", self.0.abs()))
}
}
impl ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _rhs: Cell<String>) -> Cell<String> {
let mut result: String = String::new();
let mut value: String = String::from(_rhs.0);
if (self.0 < 0) {
value = value.chars().rev().collect();
}
for index in 0..self.0.abs() {
result.push_str(&value);
}
Cell(result)
}
}
impl ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, _rhs: Matrix<String>) -> Matrix<String> {
let mut new_matrix_values: [String; 4] = Default::default();
let mut index: usize = 0;
for row in 0..self.rows {
for col in 0..self.cols {
new_matrix_values[index] = (self.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone()
+ _rhs.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone()).0;
index += 1;
}
}
Matrix::new(&new_matrix_values)
}
}
impl ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, _rhs: Matrix<String>) -> String {
let mut result: String = String::new();
let mut value: String = String::new();
for row in 0..self.rows {
for col in 0..self.cols {
value = (self.matrix.get(row as usize).unwrap().get(col as usize).unwrap().clone()
* _rhs.matrix.get(col as usize).unwrap().get(row as usize).unwrap().clone()).0;
result.push_str(&value);
result.push(' ');
}
}
result.pop();
result
}
}