Решение на Matrix 4 от Александър Стоилов
Към профила на Александър Стоилов
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub x00: Cell<T>,
pub x01: Cell<T>,
pub x10: Cell<T>,
pub x11: 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> {
Matrix {
x00: Cell(data[0].clone()),
x01: Cell(data[1].clone()),
x10: Cell(data[2].clone()),
x11: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x01.clone());
data.push(self.x10.clone());
data.push(self.x11.clone());
data
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x10.clone());
data.push(self.x01.clone());
data.push(self.x11.clone());
data
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let output: Cell<String>;
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
output = Cell(format!("{} {}", _str_rev, _num_abs.to_string()));
} else {
output = Cell(format!("{} {}", _num.to_string(), _str));
}
output
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let mut output_as_string: String = String::from("");
let _num_abs = _num.abs();
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
output_as_string = format!("{}", _str_rev).repeat(_num_abs as usize);
} else if _num > 0 {
output_as_string = format!("{}", _str).repeat(_num_abs as usize);
}
let output: Cell<String> = Cell(output_as_string.clone());
output
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix {
x00: self.x00 + other.x00,
x01: self.x01 + other.x01,
x10: self.x10 + other.x10,
x11: self.x11 + other.x11,
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let res1 = match self.x00 * other.x00 {
Cell(x) => x
};
let res2 = match self.x01 * other.x10 {
Cell(x) => x
};
let res3 = match self.x10 * other.x01 {
Cell(x) => x
};
let res4 = match self.x11 * other.x11 {
Cell(x) => x
};
let output = format!("{} {} {} {}", res1, res2, res3, res4);
output
}
}
#[test]
fn test_random_kirilica_and_stuff_osnovnoto_si_go_testvah_otdelno_nali() {
assert_eq!(
Cell(45650412) + Cell(String::from("tralalalaабвг{}'/[]+?№И?П№")),
Cell(String::from("45650412 tralalalaабвг{}'/[]+?№И?П№"))
);
assert_eq!(
Cell(2) * Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Р")),
Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Рmd22у(мЧ–%+–$съ023))№УП?У!))Р"))
);
assert_eq!(
Cell(0) * Cell(String::from("md22у(мЧ–%+–$съ023))№Уоуеде-р№?!Чц;Ц§П?У!))Р")),
Cell(String::from(""))
);
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20220112-2706256-9uno81/solution) Finished test [unoptimized + debuginfo] target(s) in 7.48s 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
История (5 версии и 1 коментар)
Александър качи решение на 16.11.2021 22:38 (преди почти 4 години)
use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub x00: Cell<T>,
pub x01: Cell<T>,
pub x10: Cell<T>,
pub x11: 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> {
Matrix {
x00: Cell(data[0].clone()),
x01: Cell(data[1].clone()),
x10: Cell(data[2].clone()),
x11: Cell(data[3].clone()),
}
}
-
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x01.clone());
data.push(self.x10.clone());
data.push(self.x11.clone());
data
}
-
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x10.clone());
data.push(self.x01.clone());
data.push(self.x11.clone());
data
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
-
fn add(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
-
let _str = match _other {
Cell(x) => x
};
- // _num - num +/-
- // _str - string
-
let output: Cell<String>;
if _num < 0 {
- // -4 abvg => gvba 4
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
output = Cell(format!("{} {}", _str_rev, _num_abs.to_string()));
} else {
- //ok
output = Cell(format!("{} {}", _num.to_string(), _str));
}
- // let output = String::from(left, " ", right);
output
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
-
fn mul(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
-
let _str = match _other {
Cell(x) => x
};
let mut output_as_string: String = String::from("");
-
if _num < 0 {
- //reverse string, mult * abs(_num)
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
-
output_as_string = format!("{}", _str_rev).repeat(_num_abs as usize);
- }
- //4 * abv => abvabvabvabv
-
- else if _num > 0 {
- //string * _num
-
+ } else if _num > 0 {
let _num_abs = _num.abs();
output_as_string = format!("{}", _str).repeat(_num_abs as usize);
}
-
let output: Cell<String> = Cell(output_as_string.clone());
output
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix {
x00: self.x00 + other.x00,
x01: self.x01 + other.x01,
x10: self.x10 + other.x10,
x11: self.x11 + other.x11,
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let res1 = match self.x00 * other.x00 {
Cell(x) => x
};
-
let res2 = match self.x01 * other.x10 {
Cell(x) => x
};
let res3 = match self.x10 * other.x01 {
Cell(x) => x
};
let res4 = match self.x11 * other.x11 {
Cell(x) => x
};
-
let output = format!("{} {} {} {}", res1, res2, res3, res4);
output
}
}
Александър качи решение на 18.11.2021 00:28 (преди почти 4 години)
use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub x00: Cell<T>,
pub x01: Cell<T>,
pub x10: Cell<T>,
pub x11: 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> {
Matrix {
x00: Cell(data[0].clone()),
x01: Cell(data[1].clone()),
x10: Cell(data[2].clone()),
x11: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x01.clone());
data.push(self.x10.clone());
data.push(self.x11.clone());
data
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x10.clone());
data.push(self.x01.clone());
data.push(self.x11.clone());
data
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let output: Cell<String>;
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
output = Cell(format!("{} {}", _str_rev, _num_abs.to_string()));
} else {
output = Cell(format!("{} {}", _num.to_string(), _str));
}
output
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let mut output_as_string: String = String::from("");
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
output_as_string = format!("{}", _str_rev).repeat(_num_abs as usize);
} else if _num > 0 {
let _num_abs = _num.abs();
output_as_string = format!("{}", _str).repeat(_num_abs as usize);
}
let output: Cell<String> = Cell(output_as_string.clone());
output
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix {
x00: self.x00 + other.x00,
x01: self.x01 + other.x01,
x10: self.x10 + other.x10,
x11: self.x11 + other.x11,
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let res1 = match self.x00 * other.x00 {
Cell(x) => x
};
let res2 = match self.x01 * other.x10 {
Cell(x) => x
};
let res3 = match self.x10 * other.x01 {
Cell(x) => x
};
let res4 = match self.x11 * other.x11 {
Cell(x) => x
};
let output = format!("{} {} {} {}", res1, res2, res3, res4);
output
}
+}
+
+#[test]
+fn test_random_kirilica_and_stuff_osnovnoto_si_go_testvah_otdelno_nali() {
+ //кирилица + stuff
+ assert_eq!(
+ Cell(45650412) + Cell(String::from("tralalalaабвг{}'/[]+?№И?П№")),
+ Cell(String::from("45650412 tralalalaабвг{}'/[]+?№И?П№"))
+ );
+ assert_eq!(
+ Cell(2) * Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Р")),
+ Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Рmd22у(мЧ–%+–$съ023))№УП?У!))Р"))
+ );
+ assert_eq!(
+ Cell(0) * Cell(String::from("md22у(мЧ–%+–$съ023))№Уоуеде-р№?!Чц;Ц§П?У!))Р")),
+ Cell(String::from(""))
+ );
}
Александър качи решение на 18.11.2021 00:29 (преди почти 4 години)
use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub x00: Cell<T>,
pub x01: Cell<T>,
pub x10: Cell<T>,
pub x11: 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> {
Matrix {
x00: Cell(data[0].clone()),
x01: Cell(data[1].clone()),
x10: Cell(data[2].clone()),
x11: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x01.clone());
data.push(self.x10.clone());
data.push(self.x11.clone());
data
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x10.clone());
data.push(self.x01.clone());
data.push(self.x11.clone());
data
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let output: Cell<String>;
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
output = Cell(format!("{} {}", _str_rev, _num_abs.to_string()));
} else {
output = Cell(format!("{} {}", _num.to_string(), _str));
}
output
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let mut output_as_string: String = String::from("");
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
output_as_string = format!("{}", _str_rev).repeat(_num_abs as usize);
} else if _num > 0 {
let _num_abs = _num.abs();
output_as_string = format!("{}", _str).repeat(_num_abs as usize);
}
let output: Cell<String> = Cell(output_as_string.clone());
output
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix {
x00: self.x00 + other.x00,
x01: self.x01 + other.x01,
x10: self.x10 + other.x10,
x11: self.x11 + other.x11,
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let res1 = match self.x00 * other.x00 {
Cell(x) => x
};
let res2 = match self.x01 * other.x10 {
Cell(x) => x
};
let res3 = match self.x10 * other.x01 {
Cell(x) => x
};
let res4 = match self.x11 * other.x11 {
Cell(x) => x
};
let output = format!("{} {} {} {}", res1, res2, res3, res4);
output
}
}
#[test]
fn test_random_kirilica_and_stuff_osnovnoto_si_go_testvah_otdelno_nali() {
- //кирилица + stuff
assert_eq!(
Cell(45650412) + Cell(String::from("tralalalaабвг{}'/[]+?№И?П№")),
Cell(String::from("45650412 tralalalaабвг{}'/[]+?№И?П№"))
);
assert_eq!(
Cell(2) * Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Р")),
Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Рmd22у(мЧ–%+–$съ023))№УП?У!))Р"))
);
assert_eq!(
Cell(0) * Cell(String::from("md22у(мЧ–%+–$съ023))№Уоуеде-р№?!Чц;Ц§П?У!))Р")),
Cell(String::from(""))
);
}
Александър качи решение на 18.11.2021 00:33 (преди почти 4 години)
use std::ops::{Add, Mul};
#[derive(Debug)]
pub struct Matrix<T: Clone> {
pub x00: Cell<T>,
pub x01: Cell<T>,
pub x10: Cell<T>,
pub x11: 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> {
Matrix {
x00: Cell(data[0].clone()),
x01: Cell(data[1].clone()),
x10: Cell(data[2].clone()),
x11: Cell(data[3].clone()),
}
}
pub fn by_row(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x01.clone());
data.push(self.x10.clone());
data.push(self.x11.clone());
data
}
pub fn by_col(&self) -> Vec<Cell<T>> {
let mut data: Vec<Cell<T>> = Vec::new();
data.push(self.x00.clone());
data.push(self.x10.clone());
data.push(self.x01.clone());
data.push(self.x11.clone());
data
}
}
impl Add<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn add(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let output: Cell<String>;
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
let _num_abs = _num.abs();
output = Cell(format!("{} {}", _str_rev, _num_abs.to_string()));
} else {
output = Cell(format!("{} {}", _num.to_string(), _str));
}
output
}
}
impl Mul<Cell<String>> for Cell<i32> {
type Output = Cell<String>;
fn mul(self, _other: Cell<String>) -> Cell<String> {
let _num = match self {
Cell(x) => x
};
let _str = match _other {
Cell(x) => x
};
let mut output_as_string: String = String::from("");
+ let _num_abs = _num.abs();
if _num < 0 {
let mut _str_rev = _str.chars().rev().collect::<String>();
- let _num_abs = _num.abs();
output_as_string = format!("{}", _str_rev).repeat(_num_abs as usize);
} else if _num > 0 {
- let _num_abs = _num.abs();
output_as_string = format!("{}", _str).repeat(_num_abs as usize);
}
let output: Cell<String> = Cell(output_as_string.clone());
output
}
}
impl Add<Matrix<String>> for Matrix<i32> {
type Output = Matrix<String>;
fn add(self, other: Matrix<String>) -> Self::Output {
Matrix {
x00: self.x00 + other.x00,
x01: self.x01 + other.x01,
x10: self.x10 + other.x10,
x11: self.x11 + other.x11,
}
}
}
impl Mul<Matrix<String>> for Matrix<i32> {
type Output = String;
fn mul(self, other: Matrix<String>) -> String {
let res1 = match self.x00 * other.x00 {
Cell(x) => x
};
let res2 = match self.x01 * other.x10 {
Cell(x) => x
};
let res3 = match self.x10 * other.x01 {
Cell(x) => x
};
let res4 = match self.x11 * other.x11 {
Cell(x) => x
};
let output = format!("{} {} {} {}", res1, res2, res3, res4);
output
}
}
#[test]
fn test_random_kirilica_and_stuff_osnovnoto_si_go_testvah_otdelno_nali() {
assert_eq!(
Cell(45650412) + Cell(String::from("tralalalaабвг{}'/[]+?№И?П№")),
Cell(String::from("45650412 tralalalaабвг{}'/[]+?№И?П№"))
);
assert_eq!(
Cell(2) * Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Р")),
Cell(String::from("md22у(мЧ–%+–$съ023))№УП?У!))Рmd22у(мЧ–%+–$съ023))№УП?У!))Р"))
);
assert_eq!(
Cell(0) * Cell(String::from("md22у(мЧ–%+–$съ023))№Уоуеде-р№?!Чц;Ц§П?У!))Р")),
Cell(String::from(""))
);
}
Добре е, че си писал все някакви тестове, но малко постно :). Примерно, добре щеше да е да тестваш и с отрицателни числа, да провериш, че кирилицата ти actually се обръща правилно.
Откъм примери, няма нужда да са random-изглеждащи неща -- на компютъра не му пука дали си написал tralalalaабвг{}'/[]+?№И?П№
или abc абв
примерно. Само че второто се чете малко по-лесно и може да се напише и даже обърне лесно на ръка.