Решение на FMI Buzz от Борис Ангелов

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

Към профила на Борис Ангелов

Резултати

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

Код

pub fn fizzbuzz(n: usize) -> Vec<String> {
return custom_labeled_buzz(n, 3, 5, &[String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")]);
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
return custom_labeled_buzz(n, k1, k2, &[String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")]);
}
fn custom_labeled_buzz(n: usize, k1: u8, k2: u8, labels: &[String; 3]) -> Vec<String> {
if k1 * k2 == 0 {
panic!("Divisors cannot be 0.");
}
if k1 == 1 || k2 == 1 {
panic!("Divisors cannot be 1.");
}
let mut res: Vec<String> = Vec::new();
let del1 = usize::from(k1);
let del2 = usize::from(k2);
for i in 1..n+1 {
if i % del1 == 0 {
if i % del2 == 0 {
res.push(labels[2].clone());
} else {
res.push(labels[0].clone());
}
} else {
if i % del2 == 0 {
res.push(labels[1].clone());
} else {
res.push(i.to_string());
}
}
}
return res;
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
return custom_labeled_buzz(n, self.k1, self.k2, &self.labels);
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Index out of bounds [0, 2]");
}
self.labels[index] = value.clone();
}
}

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

Compiling solution v0.1.0 (/tmp/d20201028-2816268-1q5r88x/solution)
    Finished test [unoptimized + debuginfo] target(s) in 2.02s
     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

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

Борис качи първо решение на 25.10.2020 09:26 (преди почти 5 години)