Решение на FMI Buzz от Тервел Вълков

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

Към профила на Тервел Вълков

Резултати

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

Код

pub fn fizzbuzz(n: usize) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
for i in 1..=n {
if i % 15 == 0 { // if i % 3 == 0 && i % 5 == 0 {
result.push(String::from("Fizzbuzz"));
} else if i % 3 == 0 {
result.push(String::from("Fizz"));
} else if i % 5 == 0 {
result.push(String::from("Buzz"));
} else {
result.push(i.to_string());
}
}
return result;
}
pub fn fizzbuzz_map(n: usize) -> Vec<String> {
fn mapper(i: usize) -> String {
if i % 15 == 0 {
String::from("Fizzbuzz")
} else if i % 3 == 0 {
String::from("Fizz")
} else if i % 5 == 0 {
String::from("Buzz")
} else {
i.to_string()
}
}
let mut result: Vec<usize> = Vec::new();
for i in 1..=n {
result.push(i);
}
result.into_iter().map(mapper).collect()
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
if k1 <= 1 || k2 <= 1 {
panic!("Invalid coefficient!")
}
let mut result: Vec<String> = Vec::new();
let k1 = k1 as usize;
let k2 = k2 as usize;
let product = k1 * k2;
for i in 1..=n {
if i % product == 0 { // might not work when one divides the other (for example: 2 and 4), but the task description says numbers will be coprime
result.push(String::from("Fizzbuzz"));
} else if i % k1 == 0 {
result.push(String::from("Fizz"));
} else if i % k2 == 0 {
result.push(String::from("Buzz"));
} else {
result.push(i.to_string());
}
}
return result;
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
if self.k1 <= 1 || self.k2 <= 2 {
panic!("Invalid coefficient!")
}
let mut result: Vec<String> = Vec::new();
let k1 = self.k1 as usize;
let k2 = self.k2 as usize;
let product = k1 * k2;
for i in 1..=n {
if i % product == 0 {
result.push(self.labels[2].to_string());
} else if i % k1 == 0 {
result.push(self.labels[0].to_string());
} else if i % k2 == 0 {
result.push(self.labels[1].to_string());
} else {
result.push(i.to_string());
}
}
return result;
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Index out of bounds!")
}
self.labels[index] = value.to_string();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[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 mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
assert_eq!(fizzbuzzer.take(3), expected);
fizzbuzzer.change_label(0, &String::from("Fiz"));
}
//my tests
#[test]
fn test_edge_case_empty() {
assert!(fizzbuzz(0).is_empty());
assert!(custom_buzz(0, 3, 5).is_empty());
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert!(fizzbuzzer.take(0).is_empty());
}
#[test]
#[should_panic]
fn test_edge_case_invalid_k() {
custom_buzz(3, 0, 1);
}
#[test]
#[should_panic]
fn test_edge_case_invalid_index() {
let mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
fizzbuzzer.change_label(3, &String::from("Error"));
}
#[test]
fn test_identical_results() {
let x = fizzbuzz(100);
assert_eq!(x, custom_buzz(100, 3, 5));
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(x, fizzbuzzer.take(100));
}
#[test]
fn test_2_3() {
let expected = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"), 5.to_string(),
String::from("Fizzbuzz"), 7.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"),
11.to_string(), String::from("Fizzbuzz"), 13.to_string(), String::from("Fizz"),String::from("Buzz")];
assert_eq!(custom_buzz(15, 2, 3), expected);
let fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 3,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(15), expected);
}
#[test]
fn test_3_4() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz"), String::from("Buzz"), 5.to_string(),
String::from("Fizz"), 7.to_string(), String::from("Buzz"), String::from("Fizz"), 10.to_string(),
11.to_string(), String::from("Fizzbuzz"), 13.to_string(), 14.to_string(), String::from("Fizz")];
assert_eq!(custom_buzz(15, 3, 4), expected);
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 4,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(15), expected);
}
#[test]
fn test_5_7() {
let expected = vec![1.to_string(), 2.to_string(), 3.to_string(), 4.to_string(), String::from("Fizz"),
6.to_string(), String::from("Buzz"), 8.to_string(), 9.to_string(), String::from("Fizz"),
11.to_string(), 12.to_string(), 13.to_string(), String::from("Buzz"), String::from("Fizz"),
16.to_string(), 17.to_string(), 18.to_string(), 19.to_string(), String::from("Fizz"),
String::from("Buzz"), 22.to_string(), 23.to_string(), 24.to_string(), String::from("Fizz"),
26.to_string(), 27.to_string(), String::from("Buzz"), 29.to_string(), String::from("Fizz"),
31.to_string(), 32.to_string(), 33.to_string(), 34.to_string(), String::from("Fizzbuzz")];
assert_eq!(custom_buzz(35, 5, 7), expected);
let fizzbuzzer = FizzBuzzer {
k1: 5,
k2: 7,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(35), expected);
}
#[test]
fn test_change_labels() {
let mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.labels[0], String::from("Fizz"));
assert_eq!(fizzbuzzer.labels[1], String::from("Buzz"));
assert_eq!(fizzbuzzer.labels[2], String::from("Fizzbuzz"));
fizzbuzzer.change_label(0, &String::from("ПФОЗ"));
fizzbuzzer.change_label(1, &String::from("ПФОЕС"));
fizzbuzzer.change_label(2, &String::from("ПФОЗИЕС"));
assert_eq!(fizzbuzzer.labels[0], String::from("ПФОЗ"));
assert_eq!(fizzbuzzer.labels[1], String::from("ПФОЕС"));
assert_eq!(fizzbuzzer.labels[2], String::from("ПФОЗИЕС"));
}
#[test]
fn test_fmi() {
let expected = vec![1.to_string(), 2.to_string(), String::from("ПФОЗ"), 4.to_string(), String::from("ПФОЕС"),
String::from("ПФОЗ"), 7.to_string(), 8.to_string(), String::from("ПФОЗ"), String::from("ПФОЕС"),
11.to_string(), String::from("ПФОЗ"), 13.to_string(), 14.to_string(), String::from("ПФОЗИЕС"),
16.to_string(), 17.to_string(), String::from("ПФОЗ"), 19.to_string(), String::from("ПФОЕС")];
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("ПФОЗ"), String::from("ПФОЕС"), String::from("ПФОЗИЕС")],
};
assert_eq!(fizzbuzzer.take(20), expected);
}
}
/*
pub fn print_buzz(result: Vec<String>) {
for i in 0..result.len() {
println!("{}: {}", i + 1, result[i])
}
}
fn main() {
let mut fb = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
let x = fizzbuzz(100);
let y = fizzbuzz_map(100);
let z = custom_buzz(100, 3, 5);
let u = fb.take(100);
println!("{}", x == y);
println!("{}", x == z);
println!("{}", x == u);
println!("{}, {}, {}", fb.labels[0], fb.labels[1], fb.labels[2]);
fb.change_label(0, &String::from("ПФОЗ"));
fb.change_label(1, &String::from("ПФОЕС"));
fb.change_label(2, &String::from("ПФОЗИЕС"));
println!("{}, {}, {}", fb.labels[0], fb.labels[1], fb.labels[2]);
print_buzz(fb.take(15));
}
*/

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

Compiling solution v0.1.0 (/tmp/d20201028-2816268-18xj5am/solution)
    Finished test [unoptimized + debuginfo] target(s) in 2.93s
     Running target/debug/deps/solution-ebb42508826ef2b4

running 10 tests
test tests::test_2_3 ... ok
test tests::test_3_4 ... ok
test tests::test_5_7 ... ok
test tests::test_basic ... ok
test tests::test_change_labels ... ok
test tests::test_edge_case_empty ... ok
test tests::test_edge_case_invalid_index ... ok
test tests::test_edge_case_invalid_k ... ok
test tests::test_fmi ... ok
test tests::test_identical_results ... ok

test result: ok. 10 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

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

Тервел качи първо решение на 22.10.2020 16:51 (преди почти 5 години)

Тервел качи решение на 22.10.2020 16:55 (преди почти 5 години)

pub fn fizzbuzz(n: usize) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
for i in 1..=n {
if i % 15 == 0 { // if i % 3 == 0 && i % 5 == 0 {
result.push(String::from("Fizzbuzz"));
} else if i % 3 == 0 {
result.push(String::from("Fizz"));
} else if i % 5 == 0 {
result.push(String::from("Buzz"));
} else {
result.push(i.to_string());
}
}
return result;
}
pub fn fizzbuzz_map(n: usize) -> Vec<String> {
fn mapper(i: usize) -> String {
if i % 15 == 0 {
String::from("Fizzbuzz")
} else if i % 3 == 0 {
String::from("Fizz")
} else if i % 5 == 0 {
String::from("Buzz")
} else {
i.to_string()
}
}
let mut result: Vec<usize> = Vec::new();
for i in 1..=n {
result.push(i);
}
result.into_iter().map(mapper).collect()
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
if k1 <= 1 || k2 <= 1 {
panic!("Invalid coefficient!")
}
let mut result: Vec<String> = Vec::new();
let k1 = k1 as usize;
let k2 = k2 as usize;
for i in 1..=n {
if i % (k1 * k2) == 0 { // might not work for numbers which aren't coprime (for example: 2 and 4), but the task description says numbers will be coprime
result.push(String::from("Fizzbuzz"));
} else if i % k1 == 0 {
result.push(String::from("Fizz"));
} else if i % k2 == 0 {
result.push(String::from("Buzz"));
} else {
result.push(i.to_string());
}
}
return result;
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
if self.k1 <= 1 || self.k2 <= 2 {
panic!("Invalid coefficient!")
}
let mut result: Vec<String> = Vec::new();
let k1 = self.k1 as usize;
let k2 = self.k2 as usize;
for i in 1..=n {
if i % (k1 * k2) == 0 {
result.push(self.labels[2].to_string());
} else if i % k1 == 0 {
result.push(self.labels[0].to_string());
} else if i % k2 == 0 {
result.push(self.labels[1].to_string());
} else {
result.push(i.to_string());
}
}
return result;
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Index out of bounds!")
}
self.labels[index] = value.to_string();
}
}
pub fn print_buzz(result: Vec<String>) {
for i in 0..result.len() {
println!("{}: {}", i + 1, result[i])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[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 mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
assert_eq!(fizzbuzzer.take(3), expected);
fizzbuzzer.change_label(0, &String::from("Fiz"));
}
//my tests
#[test]
fn edge_case_empty() {
assert!(fizzbuzz(0).is_empty());
assert!(custom_buzz(0, 3, 5).is_empty());
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert!(fizzbuzzer.take(0).is_empty());
}
#[test]
#[should_panic]
fn edge_case_invalid_k() {
custom_buzz(3, 0, 1);
}
#[test]
+ #[should_panic]
+ fn edge_case_invalid_index() {
+ let mut fizzbuzzer = FizzBuzzer {
+ k1: 3,
+ k2: 5,
+ labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
+ };
+
+ fizzbuzzer.change_label(3, &String::from("Error"));
+ }
+
+ #[test]
fn identical_results() {
let x = fizzbuzz(100);
assert_eq!(x, custom_buzz(100, 3, 5));
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(x, fizzbuzzer.take(100));
}
#[test]
fn test_2_3() {
let expected = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"), 5.to_string(),
String::from("Fizzbuzz"), 7.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"),
11.to_string(), String::from("Fizzbuzz"), 13.to_string(), String::from("Fizz"),String::from("Buzz")];
assert_eq!(custom_buzz(15, 2, 3), expected);
let fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 3,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(15), expected);
}
#[test]
fn test_3_4() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz"), String::from("Buzz"), 5.to_string(),
String::from("Fizz"), 7.to_string(), String::from("Buzz"), String::from("Fizz"), 10.to_string(),
11.to_string(), String::from("Fizzbuzz"), 13.to_string(), 14.to_string(), String::from("Fizz")];
assert_eq!(custom_buzz(15, 3, 4), expected);
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 4,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(15), expected);
}
#[test]
fn test_5_7() {
let expected = vec![1.to_string(), 2.to_string(), 3.to_string(), 4.to_string(), String::from("Fizz"),
6.to_string(), String::from("Buzz"), 8.to_string(), 9.to_string(), String::from("Fizz"),
11.to_string(), 12.to_string(), 13.to_string(), String::from("Buzz"), String::from("Fizz"),
16.to_string(), 17.to_string(), 18.to_string(), 19.to_string(), String::from("Fizz"),
String::from("Buzz"), 22.to_string(), 23.to_string(), 24.to_string(), String::from("Fizz"),
26.to_string(), 27.to_string(), String::from("Buzz"), 29.to_string(), String::from("Fizz"),
31.to_string(), 32.to_string(), 33.to_string(), 34.to_string(), String::from("Fizzbuzz")];
assert_eq!(custom_buzz(35, 5, 7), expected);
let fizzbuzzer = FizzBuzzer {
k1: 5,
k2: 7,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(35), expected);
}
#[test]
fn change_labels() {
let mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.labels[0], String::from("Fizz"));
assert_eq!(fizzbuzzer.labels[1], String::from("Buzz"));
assert_eq!(fizzbuzzer.labels[2], String::from("Fizzbuzz"));
fizzbuzzer.change_label(0, &String::from("ПФОЗ"));
fizzbuzzer.change_label(1, &String::from("ПФОЕС"));
fizzbuzzer.change_label(2, &String::from("ПФОЗИЕС"));
assert_eq!(fizzbuzzer.labels[0], String::from("ПФОЗ"));
assert_eq!(fizzbuzzer.labels[1], String::from("ПФОЕС"));
assert_eq!(fizzbuzzer.labels[2], String::from("ПФОЗИЕС"));
}
#[test]
fn fmi() {
let expected = vec![1.to_string(), 2.to_string(), String::from("ПФОЗ"), 4.to_string(), String::from("ПФОЕС"),
String::from("ПФОЗ"), 7.to_string(), 8.to_string(), String::from("ПФОЗ"), String::from("ПФОЕС"),
11.to_string(), String::from("ПФОЗ"), 13.to_string(), 14.to_string(), String::from("ПФОЗИЕС"),
16.to_string(), 17.to_string(), String::from("ПФОЗ"), 19.to_string(), String::from("ПФОЕС")];
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("ПФОЗ"), String::from("ПФОЕС"), String::from("ПФОЗИЕС")],
};
assert_eq!(fizzbuzzer.take(20), expected);
}
}
/*fn main() {
let mut fb = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
let x = fizzbuzz(100);
let y = fizzbuzz_map(100);
let z = custom_buzz(100, 3, 5);
let u = fb.take(100);
println!("{}", x == y);
println!("{}", x == z);
println!("{}", x == u);
println!("{}, {}, {}", fb.labels[0], fb.labels[1], fb.labels[2]);
fb.change_label(0, &String::from("ПФОЗ"));
fb.change_label(1, &String::from("ПФОЕС"));
fb.change_label(2, &String::from("ПФОЗИЕС"));
println!("{}, {}, {}", fb.labels[0], fb.labels[1], fb.labels[2]);
print_buzz(fb.take(15));
}*/

Тервел качи решение на 23.10.2020 14:10 (преди почти 5 години)

pub fn fizzbuzz(n: usize) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
for i in 1..=n {
if i % 15 == 0 { // if i % 3 == 0 && i % 5 == 0 {
result.push(String::from("Fizzbuzz"));
} else if i % 3 == 0 {
result.push(String::from("Fizz"));
} else if i % 5 == 0 {
result.push(String::from("Buzz"));
} else {
result.push(i.to_string());
}
}
return result;
}
pub fn fizzbuzz_map(n: usize) -> Vec<String> {
fn mapper(i: usize) -> String {
if i % 15 == 0 {
String::from("Fizzbuzz")
} else if i % 3 == 0 {
String::from("Fizz")
} else if i % 5 == 0 {
String::from("Buzz")
} else {
i.to_string()
}
}
let mut result: Vec<usize> = Vec::new();
for i in 1..=n {
result.push(i);
}
result.into_iter().map(mapper).collect()
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
if k1 <= 1 || k2 <= 1 {
panic!("Invalid coefficient!")
}
let mut result: Vec<String> = Vec::new();
let k1 = k1 as usize;
let k2 = k2 as usize;
+ let product = k1 * k2;
for i in 1..=n {
- if i % (k1 * k2) == 0 { // might not work for numbers which aren't coprime (for example: 2 and 4), but the task description says numbers will be coprime
+ if i % product == 0 { // might not work when one divides the other (for example: 2 and 4), but the task description says numbers will be coprime
result.push(String::from("Fizzbuzz"));
} else if i % k1 == 0 {
result.push(String::from("Fizz"));
} else if i % k2 == 0 {
result.push(String::from("Buzz"));
} else {
result.push(i.to_string());
}
}
return result;
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
if self.k1 <= 1 || self.k2 <= 2 {
panic!("Invalid coefficient!")
}
let mut result: Vec<String> = Vec::new();
let k1 = self.k1 as usize;
let k2 = self.k2 as usize;
+ let product = k1 * k2;
for i in 1..=n {
- if i % (k1 * k2) == 0 {
+ if i % product == 0 {
result.push(self.labels[2].to_string());
} else if i % k1 == 0 {
result.push(self.labels[0].to_string());
} else if i % k2 == 0 {
result.push(self.labels[1].to_string());
} else {
result.push(i.to_string());
}
}
return result;
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Index out of bounds!")
}
self.labels[index] = value.to_string();
}
}
-pub fn print_buzz(result: Vec<String>) {
- for i in 0..result.len() {
- println!("{}: {}", i + 1, result[i])
- }
-}
-
-
#[cfg(test)]
mod tests {
use super::*;
#[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 mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
assert_eq!(fizzbuzzer.take(3), expected);
fizzbuzzer.change_label(0, &String::from("Fiz"));
}
//my tests
#[test]
- fn edge_case_empty() {
+ fn test_edge_case_empty() {
assert!(fizzbuzz(0).is_empty());
assert!(custom_buzz(0, 3, 5).is_empty());
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert!(fizzbuzzer.take(0).is_empty());
}
#[test]
#[should_panic]
- fn edge_case_invalid_k() {
+ fn test_edge_case_invalid_k() {
custom_buzz(3, 0, 1);
}
#[test]
#[should_panic]
- fn edge_case_invalid_index() {
+ fn test_edge_case_invalid_index() {
let mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
fizzbuzzer.change_label(3, &String::from("Error"));
}
#[test]
- fn identical_results() {
+ fn test_identical_results() {
let x = fizzbuzz(100);
assert_eq!(x, custom_buzz(100, 3, 5));
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(x, fizzbuzzer.take(100));
}
#[test]
fn test_2_3() {
let expected = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"), 5.to_string(),
String::from("Fizzbuzz"), 7.to_string(), String::from("Fizz"), String::from("Buzz"), String::from("Fizz"),
11.to_string(), String::from("Fizzbuzz"), 13.to_string(), String::from("Fizz"),String::from("Buzz")];
assert_eq!(custom_buzz(15, 2, 3), expected);
let fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 3,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(15), expected);
}
#[test]
fn test_3_4() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz"), String::from("Buzz"), 5.to_string(),
String::from("Fizz"), 7.to_string(), String::from("Buzz"), String::from("Fizz"), 10.to_string(),
11.to_string(), String::from("Fizzbuzz"), 13.to_string(), 14.to_string(), String::from("Fizz")];
assert_eq!(custom_buzz(15, 3, 4), expected);
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 4,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(15), expected);
}
#[test]
fn test_5_7() {
let expected = vec![1.to_string(), 2.to_string(), 3.to_string(), 4.to_string(), String::from("Fizz"),
6.to_string(), String::from("Buzz"), 8.to_string(), 9.to_string(), String::from("Fizz"),
11.to_string(), 12.to_string(), 13.to_string(), String::from("Buzz"), String::from("Fizz"),
16.to_string(), 17.to_string(), 18.to_string(), 19.to_string(), String::from("Fizz"),
String::from("Buzz"), 22.to_string(), 23.to_string(), 24.to_string(), String::from("Fizz"),
26.to_string(), 27.to_string(), String::from("Buzz"), 29.to_string(), String::from("Fizz"),
31.to_string(), 32.to_string(), 33.to_string(), 34.to_string(), String::from("Fizzbuzz")];
assert_eq!(custom_buzz(35, 5, 7), expected);
let fizzbuzzer = FizzBuzzer {
k1: 5,
k2: 7,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.take(35), expected);
}
#[test]
- fn change_labels() {
+ fn test_change_labels() {
let mut fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
assert_eq!(fizzbuzzer.labels[0], String::from("Fizz"));
assert_eq!(fizzbuzzer.labels[1], String::from("Buzz"));
assert_eq!(fizzbuzzer.labels[2], String::from("Fizzbuzz"));
fizzbuzzer.change_label(0, &String::from("ПФОЗ"));
fizzbuzzer.change_label(1, &String::from("ПФОЕС"));
fizzbuzzer.change_label(2, &String::from("ПФОЗИЕС"));
assert_eq!(fizzbuzzer.labels[0], String::from("ПФОЗ"));
assert_eq!(fizzbuzzer.labels[1], String::from("ПФОЕС"));
assert_eq!(fizzbuzzer.labels[2], String::from("ПФОЗИЕС"));
}
#[test]
- fn fmi() {
+ fn test_fmi() {
let expected = vec![1.to_string(), 2.to_string(), String::from("ПФОЗ"), 4.to_string(), String::from("ПФОЕС"),
String::from("ПФОЗ"), 7.to_string(), 8.to_string(), String::from("ПФОЗ"), String::from("ПФОЕС"),
11.to_string(), String::from("ПФОЗ"), 13.to_string(), 14.to_string(), String::from("ПФОЗИЕС"),
16.to_string(), 17.to_string(), String::from("ПФОЗ"), 19.to_string(), String::from("ПФОЕС")];
let fizzbuzzer = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("ПФОЗ"), String::from("ПФОЕС"), String::from("ПФОЗИЕС")],
};
assert_eq!(fizzbuzzer.take(20), expected);
}
}
-/*fn main() {
+
+/*
+pub fn print_buzz(result: Vec<String>) {
+ for i in 0..result.len() {
+ println!("{}: {}", i + 1, result[i])
+ }
+}
+
+fn main() {
let mut fb = FizzBuzzer {
k1: 3,
k2: 5,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
let x = fizzbuzz(100);
let y = fizzbuzz_map(100);
let z = custom_buzz(100, 3, 5);
let u = fb.take(100);
println!("{}", x == y);
println!("{}", x == z);
println!("{}", x == u);
println!("{}, {}, {}", fb.labels[0], fb.labels[1], fb.labels[2]);
fb.change_label(0, &String::from("ПФОЗ"));
fb.change_label(1, &String::from("ПФОЕС"));
fb.change_label(2, &String::from("ПФОЗИЕС"));
println!("{}, {}, {}", fb.labels[0], fb.labels[1], fb.labels[2]);
print_buzz(fb.take(15));
-}*/
+}
+*/

Добра работа с тестовете -- пробвал си няколко различни входа, и за коефициенти, и за етикети. За невалидното k имаш само един тест за само един случай, което е някаква лека дупка. Би могло да играе роля, ако accidentally допуснеш typo и напишеш if k1 <= 1 || k1 <= 1 { вместо if k1 <= 1 || k2 <= 1 {, примерно. Но пък това означава да напишеш N на брой теста за всички случаи.

Може да видиш в пълния тест (който споделих в discord) как съм решил този проблем. Също може да видиш как съм опростил малко кода за вектор, пълен със низове. И двете използват неща, които още не сме преподали, но ще ги разкажем на лекции в някакъв момент.