Решение на Matrix 4 от Юлиан Стоев

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

Към профила на Юлиан Стоев

Резултати

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

Код

#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub cell11: Cell<T>,
pub cell12: Cell<T>,
pub cell21: Cell<T>,
pub cell22: Cell<T>,
}
impl std::ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
// Borrow or clone here? - borrow because operator + just borrows the rhs
Cell(self.0.to_string() + " " + &other.0)
} else {
Cell((&other.0).clone().chars().rev().collect::<String>() + " "
+ &self.0.abs().to_string())
}
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let mut result = String::from("");
let temp: String;
if self.0 > 0 {
temp = other.0.clone(); }
else {
temp = other.0.clone().chars().rev().collect::<String>(); }
for _ in 0..self.0.abs() {
result += &temp;
}
Cell(result)
}
}
// Too much boiler plate? - maybe rewrite Matrix to have vector of 4 cells
impl std::ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
let empty_string = String::from("");
let mut result = Matrix::new(&[empty_string.clone(), empty_string.clone(),
empty_string.clone(), empty_string.clone()]);
result.cell11 = self.cell11 + other.cell11;
result.cell12 = self.cell12 + other.cell12;
result.cell21 = self.cell21 + other.cell21;
result.cell22 = self.cell22 + other.cell22;
result
}
}
impl std::ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
// Hybrid design or do i fuck up the ownership?
(self.cell11 * other.cell11).0.clone() + " "
+ &(self.cell12 * other.cell21).0 + " "
+ &(self.cell21 * other.cell12).0 + " "
+ &(self.cell22 * other.cell22).0
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
cell11: Cell(data[0].clone()),
cell12: Cell(data[1].clone()),
cell21: Cell(data[2].clone()),
cell22: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell12.clone());
result.push(self.cell21.clone());
result.push(self.cell22.clone());
result
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell21.clone());
result.push(self.cell12.clone());
result.push(self.cell22.clone());
result
}
}
#[cfg(test)]
mod tests{
use super::*;
#[test]
fn test_basic_addition() {
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
}
#[test]
fn test_basic_multiplication() {
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
}
}

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

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

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

Юлиан качи първо решение на 18.11.2021 06:24 (преди почти 4 години)

Юлиан качи решение на 18.11.2021 09:10 (преди почти 4 години)

+#[derive(Debug, Clone, PartialEq)]
+pub struct Cell<T>(pub T);
+
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub cell11: Cell<T>,
pub cell12: Cell<T>,
pub cell21: Cell<T>,
pub cell22: Cell<T>,
}
-#[derive(Debug, Clone, PartialEq)]
-pub struct Cell<T>(pub T);
+impl std::ops::Add<Cell<String>> for Cell<i32> {
+ type Output = Cell<String>;
+ fn add(self, other: Cell<String>) -> Cell<String> {
+ if self.0 >= 0 {
+ // Borrow or clone here? - borrow because operator + just borrows the rhs
+ Cell(self.0.to_string() + " " + &other.0)
+ } else {
+ Cell((&other.0).clone().chars().rev().collect::<String>() + " "
+ + &self.0.abs().to_string())
+ }
+ }
+}
+impl std::ops::Mul<Cell<String>> for Cell<i32> {
+ type Output = Cell<String>;
+ fn mul(self, other: Cell<String>) -> Cell<String> {
+ let mut result = String::from("");
+ let temp: String;
+ if self.0 > 0 {
+ temp = other.0.clone(); }
+ else {
+ temp = other.0.clone().chars().rev().collect::<String>(); }
+ for _ in 0..self.0.abs() {
+ result += &temp;
+ }
+ Cell(result)
+ }
+}
+
+
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
cell11: Cell(data[0].clone()),
cell12: Cell(data[1].clone()),
cell21: Cell(data[2].clone()),
cell22: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell12.clone());
result.push(self.cell21.clone());
result.push(self.cell22.clone());
result
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell21.clone());
result.push(self.cell12.clone());
result.push(self.cell22.clone());
result
+ }
+}
+
+#[cfg(test)]
+mod tests{
+ use super::*;
+
+ #[test]
+ fn test_basic_addition() {
+ assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
+ assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
+ assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
+ }
+
+ #[test]
+ fn test_basic_multiplication() {
+ assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
+ assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
+ assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
}
}

Писах в discord, но just in case, нека да пусна едно съобщение и тук: в момента решението, което си пуснал, не се компилира, защото няма дефиниции на събиране и умножение на матрици. Предполагам че просто го правиш на части, that's fine, просто ако решиш, че нямаш време да го довършиш, сложи "празни" имплементации с todo!() за да се компилира, иначе няма как да получиш каквито и да е точки за това което вече имаш.

Юлиан качи решение на 18.11.2021 15:47 (преди почти 4 години)

#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub cell11: Cell<T>,
pub cell12: Cell<T>,
pub cell21: Cell<T>,
pub cell22: Cell<T>,
}
impl std::ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
// Borrow or clone here? - borrow because operator + just borrows the rhs
Cell(self.0.to_string() + " " + &other.0)
} else {
Cell((&other.0).clone().chars().rev().collect::<String>() + " "
- + &self.0.abs().to_string())
+ + &self.0.abs().to_string())
}
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let mut result = String::from("");
let temp: String;
if self.0 > 0 {
temp = other.0.clone(); }
else {
temp = other.0.clone().chars().rev().collect::<String>(); }
for _ in 0..self.0.abs() {
result += &temp;
}
Cell(result)
+ }
+}
+
+// Too much boiler plate? - rewrite Matrix?, .0, .1 in loop and what am i missing
+impl std::ops::Add<Matrix<String>> for Matrix<i32> {
+ type Output = Matrix<String>;
+ fn add(self, other: Matrix<String>) -> Matrix<String> {
+ let empty_string = String::from("");
+ let mut result = Matrix::new(&[empty_string.clone(), empty_string.clone(),
+ empty_string.clone(), empty_string.clone()]);
+ result.cell11 = self.cell11 + other.cell11;
+ result.cell12 = self.cell12 + other.cell12;
+ result.cell21 = self.cell21 + other.cell21;
+ result.cell22 = self.cell22 + other.cell22;
+ result
+ }
+}
+
+impl std::ops::Mul<Matrix<String>> for Matrix<i32> {
+ type Output = String;
+ fn mul(self, other: Matrix<String>) -> String {
+ // Hybrid design or do i fuck up the ownership?
+ (self.cell11 * other.cell11).0.clone() + " "
+ + &(self.cell12 * other.cell21).0 + " "
+ + &(self.cell21 * other.cell12).0 + " "
+ + &(self.cell22 * other.cell22).0
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
cell11: Cell(data[0].clone()),
cell12: Cell(data[1].clone()),
cell21: Cell(data[2].clone()),
cell22: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell12.clone());
result.push(self.cell21.clone());
result.push(self.cell22.clone());
result
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell21.clone());
result.push(self.cell12.clone());
result.push(self.cell22.clone());
result
}
}
#[cfg(test)]
mod tests{
use super::*;
#[test]
fn test_basic_addition() {
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
}
#[test]
fn test_basic_multiplication() {
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
}
}

Юлиан качи решение на 18.11.2021 16:53 (преди почти 4 години)

#[derive(Debug, Clone, PartialEq)]
pub struct Cell<T>(pub T);
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub cell11: Cell<T>,
pub cell12: Cell<T>,
pub cell21: Cell<T>,
pub cell22: Cell<T>,
}
impl std::ops::Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, other: Cell<String>) -> Cell<String> {
if self.0 >= 0 {
// Borrow or clone here? - borrow because operator + just borrows the rhs
Cell(self.0.to_string() + " " + &other.0)
} else {
Cell((&other.0).clone().chars().rev().collect::<String>() + " "
+ &self.0.abs().to_string())
}
}
}
impl std::ops::Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, other: Cell<String>) -> Cell<String> {
let mut result = String::from("");
let temp: String;
if self.0 > 0 {
temp = other.0.clone(); }
else {
temp = other.0.clone().chars().rev().collect::<String>(); }
for _ in 0..self.0.abs() {
result += &temp;
}
Cell(result)
}
}
-// Too much boiler plate? - rewrite Matrix?, .0, .1 in loop and what am i missing
+// Too much boiler plate? - maybe rewrite Matrix to have vector of 4 cells
impl std::ops::Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Matrix<String> {
let empty_string = String::from("");
let mut result = Matrix::new(&[empty_string.clone(), empty_string.clone(),
empty_string.clone(), empty_string.clone()]);
result.cell11 = self.cell11 + other.cell11;
result.cell12 = self.cell12 + other.cell12;
result.cell21 = self.cell21 + other.cell21;
result.cell22 = self.cell22 + other.cell22;
result
}
}
impl std::ops::Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
// Hybrid design or do i fuck up the ownership?
(self.cell11 * other.cell11).0.clone() + " "
+ &(self.cell12 * other.cell21).0 + " "
+ &(self.cell21 * other.cell12).0 + " "
+ &(self.cell22 * other.cell22).0
}
}
impl<T: Clone> Matrix<T> {
pub fn new(data: &[T; 4]) -> Matrix<T> {
Matrix {
cell11: Cell(data[0].clone()),
cell12: Cell(data[1].clone()),
cell21: Cell(data[2].clone()),
cell22: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell12.clone());
result.push(self.cell21.clone());
result.push(self.cell22.clone());
result
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut result: Vec<Cell<T>> = Vec::new();
result.push(self.cell11.clone());
result.push(self.cell21.clone());
result.push(self.cell12.clone());
result.push(self.cell22.clone());
result
}
}
#[cfg(test)]
mod tests{
use super::*;
#[test]
fn test_basic_addition() {
assert_eq!(Cell(22) + Cell(String::from("years ago")), Cell(String::from("22 years ago")));
assert_eq!(Cell(0) + Cell(String::from("expectation")), Cell(String::from("0 expectation")));
assert_eq!(Cell(-4) + Cell(String::from("xirtam")), Cell(String::from("matrix 4")));
}
#[test]
fn test_basic_multiplication() {
assert_eq!(Cell(3) * Cell(String::from("woah!")), Cell(String::from("woah!woah!woah!")));
assert_eq!(Cell(0) * Cell(String::from("woah?")), Cell(String::from("")));
assert_eq!(Cell(-3) * Cell(String::from(",regdab")), Cell(String::from("badger,badger,badger,")));
}
-}
+}