Решение на FMI Buzz от Илиян Руйков
Резултати
- 15 точки от тестове
- 0 бонус точки
- 15 точки общо
- 10 успешни тест(а)
- 0 неуспешни тест(а)
Код
pub fn fizzbuzz(n: usize) -> Vec<String> {
let fizz = String::from("Fizz");
let buzz = String::from("Buzz");
let fizz_buzz = String::from("Fizzbuzz");
let mut output = Vec::<String>::new();
for i in 1..(n + 1) {
if i % 3 == 0 && i % 5 == 0 {
output.push(fizz_buzz.clone());
} else if i % 3 == 0 {
output.push(fizz.clone());
} else if i % 5 == 0 {
output.push(buzz.clone());
} else {
output.push(i.to_string());
}
}
output
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
if k1 == 0 || k1 == 1 || k2 == 0 || k2 == 1 {
panic!("Mr.Programmer I don't feel so good");
}
let fizz = String::from("Fizz");
let buzz = String::from("Buzz");
let fizz_buzz = String::from("Fizzbuzz");
let mut output = Vec::<String>::new();
for i in 1..(n + 1) {
if i % k1 as usize == 0 && i % k2 as usize == 0{
output.push(fizz_buzz.clone());
} else if i % k1 as usize == 0 {
output.push(fizz.clone());
} else if i % k2 as usize == 0 {
output.push(buzz.clone());
} else {
output.push(i.to_string());
}
}
output
}
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 == 0 || self.k1 == 1 || self.k2 == 0 || self.k2 == 1 {
panic!("Mr.Programmer I don't feel so good");
}
let first_label = String::from(self.labels[0].clone());
let second_label = String::from(self.labels[1].clone());
let third_label = String::from(self.labels[2].clone());
let mut output = Vec::<String>::new();
for i in 1..(n + 1) {
if i % self.k1 as usize == 0 && i % self.k2 as usize == 0 {
output.push(third_label.clone());
} else if i % self.k1 as usize == 0 {
output.push(first_label.clone());
} else if i % self.k2 as usize == 0 {
output.push(second_label.clone());
} else {
output.push(i.to_string());
}
}
output
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index == 0 {
self.labels[0] = value.clone();
} else if index == 1 {
self.labels[1] = value.clone();
} else if index == 2 {
self.labels[2] = value.clone();
} else {
panic!("Mr.Programmer I don't feel so good");
}
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20201028-2816268-n1pg6u/solution) Finished test [unoptimized + debuginfo] target(s) in 2.08s Running target/debug/deps/solution-ebb42508826ef2b4 running 0 tests test result: ok. 0 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