Решение на Matrix 4 от Андрей

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

Към профила на Андрей

Резултати

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

Код

use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
data: [T; 4],
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix { data: data.to_owned() }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.data.iter().cloned().map(Cell).collect()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
[&self.data[0], &self.data[2], &self.data[1], &self.data[3]].
into_iter().cloned().map(Cell).collect()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Self::Output {
if self.0 < 0 {
let string = rhs.0.chars().rev().collect::<String>();
let number = self.0.abs();
Cell(format!("{} {}", string, number))
} else {
let number = self.0;
let string = rhs.0;
Cell(format!("{} {}", number, string))
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
let mut number = self.0;
let mut string = rhs.0;
if self.0 < 0 {
string = string.chars().rev().collect();
number = number.abs();
}
let mut output = String::new();
for _ in 0..number {
output.push_str(&string);
}
Cell(output)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Self::Output {
let left_rows = self.by_row().into_iter();
let right_rows = rhs.by_row().into_iter();
let sums: Vec<String> = left_rows.zip(right_rows).map(|(l, r)| (l + r).0).collect();
Matrix::new(&sums.try_into().unwrap())
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
let left_rows = self.by_row().into_iter();
let right_cols = rhs.by_col().into_iter();
let products: Vec<String> = left_rows.zip(right_cols).map(|(l, r)| (l * r).0).collect();
products.join(" ")
}
}

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

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

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

Андрей качи първо решение на 06.11.2021 17:21 (преди почти 4 години)

Андрей качи решение на 07.11.2021 13:08 (преди почти 4 години)

use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
- data: Vec<T>,
+ data: [T; 4],
}
impl<T: Clone> Matrix<T> {
- pub fn new(data: &[T]) -> Matrix<T> {
- if data.len() != 4 {
- panic!("Expected 4 elements, got: {}", data.len());
- }
- Matrix { data: data.to_vec() }
+ pub fn new(data: &[T; 4]) -> Matrix<T> {
+ Matrix { data: data.to_owned() }
}
pub fn by_row(&self) -> Vec<Cell<T>> {
self.data.iter().cloned().map(Cell).collect()
}
pub fn by_col(&self) -> Vec<Cell<T>> {
- [&self.data[0], &self.data[2], &self.data[1], &self.data[3]].into_iter().cloned().map(Cell).collect()
+ [&self.data[0], &self.data[2], &self.data[1], &self.data[3]].
+ into_iter().cloned().map(Cell).collect()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, rhs: Cell<String>) -> Self::Output {
if self.0 < 0 {
let string = rhs.0.chars().rev().collect::<String>();
let number = self.0.abs();
Cell(format!("{} {}", string, number))
} else {
let number = self.0;
let string = rhs.0;
Cell(format!("{} {}", number, string))
}
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, rhs: Cell<String>) -> Self::Output {
let mut number = self.0;
let mut string = rhs.0;
if self.0 < 0 {
string = string.chars().rev().collect();
number = number.abs();
}
let mut output = String::new();
for _ in 0..number {
output.push_str(&string);
}
Cell(output)
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, rhs: Matrix<String>) -> Self::Output {
let left_rows = self.by_row().into_iter();
let right_rows = rhs.by_row().into_iter();
let sums: Vec<String> = left_rows.zip(right_rows).map(|(l, r)| (l + r).0).collect();
- Matrix::new(&sums)
+ Matrix::new(&sums.try_into().unwrap())
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, rhs: Matrix<String>) -> Self::Output {
let left_rows = self.by_row().into_iter();
let right_cols = rhs.by_col().into_iter();
let products: Vec<String> = left_rows.zip(right_cols).map(|(l, r)| (l * r).0).collect();
products.join(" ")
}
-}
+}