Решение на FMI Buzz от Теодор Тошков
Резултати
- 15 точки от тестове
- 1 бонус точка
- 16 точки общо
- 10 успешни тест(а)
- 0 неуспешни тест(а)
Код
pub fn fizzbuzz(n: usize) -> Vec<String> {
custom_buzz(n, 3, 5)
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
FizzBuzzer {
k1,
k2,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz"),
],
}
.take(n)
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
// This should probably be an invariant
if self.k1 <= 1 || self.k2 <= 1 {
panic!("The coefficients cannot be 0 or 1");
}
(1..=n).map(|num| self.transform(num)).collect()
}
pub fn change_label(&mut self, index: usize, value: &String) {
self.labels[index] = value.clone();
}
// Eww ...
fn transform(&self, num: usize) -> String {
if num % (self.k1 as usize * self.k2 as usize) == 0 {
self.labels[2].clone()
} else if num % self.k1 as usize == 0 {
self.labels[0].clone()
} else if num % self.k2 as usize == 0 {
self.labels[1].clone()
} else {
num.to_string()
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[should_panic]
fn test_panic_1() {
custom_buzz(10, 1, 3);
}
#[test]
#[should_panic]
fn test_panic_0() {
custom_buzz(10, 2, 0);
}
#[test]
#[should_panic]
fn test_panic_change_label() {
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz"),
],
};
fb.change_label(3, &String::from("panic"));
}
#[test]
fn test_label_change() {
let expected = vec![
1.to_string(),
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizz"),
5.to_string(),
String::from("Fizzbuzz"),
];
let new_label = String::from("monkaW");
let expected_after_change = {
let mut expected_after_change = expected.clone();
expected_after_change[expected.len() - 1] = new_label.clone();
expected_after_change
};
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz"),
],
};
assert_eq!(fb.take(6), expected);
fb.change_label(2, &new_label);
assert_eq!(fb.take(6), expected_after_change);
}
#[test]
fn test_basic() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz")];
assert_eq!(fizzbuzz(3), expected);
assert_eq!(custom_buzz(3, 3, 5), expected);
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz"),
],
};
assert_eq!(fizzbuzzer.take(3), expected);
}
#[test]
fn test_empty() {
assert_eq!(fizzbuzz(0), Vec::<String>::new());
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20201028-2816268-1o91pgh/solution) Finished test [unoptimized + debuginfo] target(s) in 2.83s Running target/debug/deps/solution-ebb42508826ef2b4 running 6 tests test test::test_basic ... ok test test::test_empty ... ok test test::test_label_change ... ok test test::test_panic_0 ... ok test test::test_panic_1 ... ok test test::test_panic_change_label ... ok test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Running target/debug/deps/solution_test-9e954a53ed808c89 running 10 tests test solution_test::test_change_label_basic ... ok test solution_test::test_change_label_invalid ... ok test solution_test::test_classic1 ... ok test solution_test::test_classic2 ... ok test solution_test::test_coefficients1 ... ok test solution_test::test_coefficients2 ... ok test solution_test::test_coefficients_invalid ... ok test solution_test::test_struct_basic ... ok test solution_test::test_struct_invalid ... ok test solution_test::test_zeroes ... ok test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests solution running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
История (6 версии и 0 коментара)
Теодор качи решение на 21.10.2020 20:45 (преди почти 5 години)
pub fn fizzbuzz(n: usize) -> Vec<String> {
FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")]
- }.get(n)
+ }.take(n)
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
FizzBuzzer {
k1: k1,
k2: k2,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")]
- }.get(n)
+ }.take(n)
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
- pub fn get(&self, n: usize) -> Vec<String> {
+ pub fn take(&self, n: usize) -> Vec<String> {
// This should probably be an invariant
if self.k1 <= 1 || self.k2 <= 1 {
panic!("The coefficients cannot be 0 or 1");
}
(1..=n).map(|num| self.transform(num))
.collect()
}
pub fn change_label(&mut self, index: usize, value: &String) {
self.labels[index] = value.clone();
}
// Eww ...
fn transform(&self, num: usize) -> String {
if num % (self.k1 as usize * self.k2 as usize) == 0 {
self.labels[2].clone()
} else if num % self.k1 as usize == 0 {
self.labels[0].clone()
} else if num % self.k2 as usize == 0 {
self.labels[1].clone()
} else {
num.to_string()
}
}
}
Теодор качи решение на 21.10.2020 21:20 (преди почти 5 години)
pub fn fizzbuzz(n: usize) -> Vec<String> {
FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")]
}.take(n)
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
FizzBuzzer {
k1: k1,
k2: k2,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")]
}.take(n)
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
// This should probably be an invariant
if self.k1 <= 1 || self.k2 <= 1 {
panic!("The coefficients cannot be 0 or 1");
}
(1..=n).map(|num| self.transform(num))
.collect()
}
pub fn change_label(&mut self, index: usize, value: &String) {
self.labels[index] = value.clone();
}
// Eww ...
fn transform(&self, num: usize) -> String {
if num % (self.k1 as usize * self.k2 as usize) == 0 {
self.labels[2].clone()
} else if num % self.k1 as usize == 0 {
self.labels[0].clone()
} else if num % self.k2 as usize == 0 {
self.labels[1].clone()
} else {
num.to_string()
}
}
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ #[should_panic]
+ fn test_panic_1() {
+ custom_buzz(10, 1, 3);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_panic_0() {
+ custom_buzz(10, 2, 0);
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_panic_change_label() {
+ let mut fb = FizzBuzzer {
+ k1: 2,
+ k2: 3,
+ labels: [
+ String::from("Fizz"),
+ String::from("Buzz"),
+ String::from("Fizzbuzz")
+ ],
+ };
+
+ fb.change_label(3, &String::from("panic"));
+ }
+
+ #[test]
+ fn test_label_change() {
+ let expected = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"), 5.to_string(), String::from("Fizzbuzz")];
+ let expected_after_change = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"), 5.to_string(), String::from("monkaW")];
+
+ let mut fb = FizzBuzzer {
+ k1: 2,
+ k2: 3,
+ labels: [
+ String::from("Fizz"),
+ String::from("Buzz"),
+ String::from("Fizzbuzz")
+ ],
+ };
+
+ assert_eq!(fb.take(6), expected);
+
+ fb.change_label(2, &String::from("monkaW"));
+ assert_eq!(fb.take(6), expected_after_change);
+ }
+
+ #[test]
+ fn test_basic() {
+ let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz")];
+
+ assert_eq!(fizzbuzz(3), expected);
+ assert_eq!(custom_buzz(3, 3, 5), expected);
+
+ let fizzbuzzer = FizzBuzzer {
+ k1: 3,
+ k2: 5,
+ labels: [
+ String::from("Fizz"),
+ String::from("Buzz"),
+ String::from("Fizzbuzz")
+ ],
+ };
+ assert_eq!(fizzbuzzer.take(3), expected);
+ }
}
Теодор качи решение на 21.10.2020 22:26 (преди почти 5 години)
pub fn fizzbuzz(n: usize) -> Vec<String> {
- FizzBuzzer {
- k1: 3,
- k2: 5,
- labels: [
- String::from("Fizz"),
- String::from("Buzz"),
- String::from("Fizzbuzz")]
- }.take(n)
+ custom_buzz(n, 3, 5)
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
FizzBuzzer {
k1: k1,
k2: k2,
labels: [
String::from("Fizz"),
String::from("Buzz"),
- String::from("Fizzbuzz")]
+ String::from("Fizzbuzz")
+ ]
}.take(n)
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
// This should probably be an invariant
if self.k1 <= 1 || self.k2 <= 1 {
panic!("The coefficients cannot be 0 or 1");
}
(1..=n).map(|num| self.transform(num))
.collect()
}
pub fn change_label(&mut self, index: usize, value: &String) {
self.labels[index] = value.clone();
}
// Eww ...
fn transform(&self, num: usize) -> String {
if num % (self.k1 as usize * self.k2 as usize) == 0 {
self.labels[2].clone()
} else if num % self.k1 as usize == 0 {
self.labels[0].clone()
} else if num % self.k2 as usize == 0 {
self.labels[1].clone()
} else {
num.to_string()
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[should_panic]
fn test_panic_1() {
custom_buzz(10, 1, 3);
}
#[test]
#[should_panic]
fn test_panic_0() {
custom_buzz(10, 2, 0);
}
#[test]
#[should_panic]
fn test_panic_change_label() {
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
fb.change_label(3, &String::from("panic"));
}
#[test]
fn test_label_change() {
- let expected = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"), 5.to_string(), String::from("Fizzbuzz")];
- let expected_after_change = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"), 5.to_string(), String::from("monkaW")];
+ let expected = vec![
+ 1.to_string(),
+ String::from("Fizz"),
+ String::from("Buzz"),
+ String::from("Fizz"),
+ 5.to_string(),
+ String::from("Fizzbuzz")
+ ];
+
+ let new_label = String::from("monkaW");
+
+ let expected_after_change = {
+ let mut expected_after_change = expected.clone();
+ expected_after_change[expected.len() - 1] = new_label.clone();
+ expected_after_change
+ };
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
assert_eq!(fb.take(6), expected);
- fb.change_label(2, &String::from("monkaW"));
+ fb.change_label(2, &new_label);
assert_eq!(fb.take(6), expected_after_change);
}
#[test]
fn test_basic() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz")];
assert_eq!(fizzbuzz(3), expected);
assert_eq!(custom_buzz(3, 3, 5), expected);
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
assert_eq!(fizzbuzzer.take(3), expected);
}
}
Теодор качи решение на 21.10.2020 22:35 (преди почти 5 години)
pub fn fizzbuzz(n: usize) -> Vec<String> {
custom_buzz(n, 3, 5)
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
FizzBuzzer {
k1: k1,
k2: k2,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
]
}.take(n)
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
// This should probably be an invariant
if self.k1 <= 1 || self.k2 <= 1 {
panic!("The coefficients cannot be 0 or 1");
}
(1..=n).map(|num| self.transform(num))
.collect()
}
pub fn change_label(&mut self, index: usize, value: &String) {
self.labels[index] = value.clone();
}
// Eww ...
fn transform(&self, num: usize) -> String {
if num % (self.k1 as usize * self.k2 as usize) == 0 {
self.labels[2].clone()
} else if num % self.k1 as usize == 0 {
self.labels[0].clone()
} else if num % self.k2 as usize == 0 {
self.labels[1].clone()
} else {
num.to_string()
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[should_panic]
fn test_panic_1() {
custom_buzz(10, 1, 3);
}
#[test]
#[should_panic]
fn test_panic_0() {
custom_buzz(10, 2, 0);
}
#[test]
#[should_panic]
fn test_panic_change_label() {
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
fb.change_label(3, &String::from("panic"));
}
#[test]
fn test_label_change() {
let expected = vec![
1.to_string(),
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizz"),
5.to_string(),
String::from("Fizzbuzz")
];
let new_label = String::from("monkaW");
let expected_after_change = {
let mut expected_after_change = expected.clone();
expected_after_change[expected.len() - 1] = new_label.clone();
expected_after_change
};
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
assert_eq!(fb.take(6), expected);
fb.change_label(2, &new_label);
assert_eq!(fb.take(6), expected_after_change);
}
#[test]
fn test_basic() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz")];
assert_eq!(fizzbuzz(3), expected);
assert_eq!(custom_buzz(3, 3, 5), expected);
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
assert_eq!(fizzbuzzer.take(3), expected);
}
+
+ #[test]
+ fn test_empty() {
+ assert_eq!(fizzbuzz(0), Vec::<String>::new());
+ }
}
Теодор качи решение на 22.10.2020 20:15 (преди почти 5 години)
pub fn fizzbuzz(n: usize) -> Vec<String> {
custom_buzz(n, 3, 5)
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
FizzBuzzer {
- k1: k1,
- k2: k2,
+ k1,
+ k2,
labels: [
String::from("Fizz"),
String::from("Buzz"),
- String::from("Fizzbuzz")
- ]
- }.take(n)
+ String::from("Fizzbuzz"),
+ ],
+ }
+ .take(n)
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
// This should probably be an invariant
if self.k1 <= 1 || self.k2 <= 1 {
panic!("The coefficients cannot be 0 or 1");
}
- (1..=n).map(|num| self.transform(num))
- .collect()
+ (1..=n).map(|num| self.transform(num)).collect()
}
pub fn change_label(&mut self, index: usize, value: &String) {
self.labels[index] = value.clone();
}
-
+
// Eww ...
fn transform(&self, num: usize) -> String {
if num % (self.k1 as usize * self.k2 as usize) == 0 {
self.labels[2].clone()
} else if num % self.k1 as usize == 0 {
self.labels[0].clone()
} else if num % self.k2 as usize == 0 {
self.labels[1].clone()
} else {
num.to_string()
}
}
}
#[cfg(test)]
mod test {
use super::*;
-
+
#[test]
#[should_panic]
fn test_panic_1() {
custom_buzz(10, 1, 3);
}
-
+
#[test]
#[should_panic]
fn test_panic_0() {
custom_buzz(10, 2, 0);
}
-
+
#[test]
#[should_panic]
fn test_panic_change_label() {
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
- String::from("Fizzbuzz")
+ String::from("Fizzbuzz"),
],
};
-
+
fb.change_label(3, &String::from("panic"));
}
-
+
#[test]
fn test_label_change() {
let expected = vec![
1.to_string(),
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizz"),
5.to_string(),
- String::from("Fizzbuzz")
+ String::from("Fizzbuzz"),
];
-
+
let new_label = String::from("monkaW");
-
+
let expected_after_change = {
let mut expected_after_change = expected.clone();
expected_after_change[expected.len() - 1] = new_label.clone();
expected_after_change
};
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [
String::from("Fizz"),
String::from("Buzz"),
- String::from("Fizzbuzz")
+ String::from("Fizzbuzz"),
],
};
-
+
assert_eq!(fb.take(6), expected);
-
+
fb.change_label(2, &new_label);
assert_eq!(fb.take(6), expected_after_change);
}
-
+
#[test]
fn test_basic() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz")];
assert_eq!(fizzbuzz(3), expected);
assert_eq!(custom_buzz(3, 3, 5), expected);
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
- String::from("Fizzbuzz")
+ String::from("Fizzbuzz"),
],
};
assert_eq!(fizzbuzzer.take(3), expected);
}
-
+
#[test]
fn test_empty() {
assert_eq!(fizzbuzz(0), Vec::<String>::new());
}
-}
+}