Решение на FMI Buzz от Ася Русанова

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

Към профила на Ася Русанова

Резултати

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

Код

pub fn fizzbuzz(n: usize) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
if n == 0 {
return result;
}
for x in 1..n+1 {
if x % 3 == 0 && x % 5 != 0 {
result.push(String::from("Fizz"));
} else if x % 3 != 0 && x % 5 == 0 {
result.push(String::from("Buzz"));
} else if x % 3 == 0 && x % 5 == 0 {
result.push(String::from("Fizzbuzz"));
} else {
result.push(x.to_string());
}
}
return result;
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
if n == 0 {
return result;
}
if k1 == 0 || k1 == 1 || k2 == 0 || k2 == 1 {
panic!("The divisors should be different than 0 or 1!");
}
for x in 1..n+1 {
if (x as u8) % k1 == 0 && (x as u8) % k2 != 0 {
result.push(String::from("Fizz"));
} else if (x as u8) % k1 != 0 && (x as u8) % k2 == 0 {
result.push(String::from("Buzz"));
} else if (x as u8) % k1 == 0 && (x as u8) % k2 == 0 {
result.push(String::from("Fizzbuzz"));
} else {
result.push(x.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> {
let mut result: Vec<String> = Vec::new();
if n == 0 {
return result;
}
if self.k1 == 0 || self.k1 == 1 || self.k2 == 0 || self.k2 == 1 {
panic!("The divisors should be different than 0 or 1!");
}
for x in 1..n+1 {
if (x as u8) % self.k1 == 0 && (x as u8) % self.k2 != 0 {
result.push(self.labels[0].clone());
} else if (x as u8) % self.k1 != 0 && (x as u8) % self.k2 == 0 {
result.push(self.labels[1].clone());
} else if (x as u8) % self.k1 == 0 && (x as u8) % self.k2 == 0 {
result.push(self.labels[2].clone());
} else {
result.push(x.to_string());
}
}
return result;
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Index out of scope.");
}
self.labels[index] = value.to_string();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_custom_and_take() {
let expected_1 = vec![1.to_string(), String::from("Fizz"), String::from("Buzz"),
String::from("Fizz"), 5.to_string(), String::from("Fizzbuzz")];
let expected_2: Vec<String> = Vec::new();
let mut fb = FizzBuzzer {
k1: 2,
k2: 3,
labels: [String::from("Hello"), String::from("Buzz"), String::from("Fizzbuzz")],
};
fb.change_label(0, &String::from("Fizz"));
assert_eq!(custom_buzz(6,2,3), expected_1);
assert_eq!(fb.take(6), expected_1);
assert_eq!(custom_buzz(0, 1, 4), expected_2);
}

Опитала си се да squeeze-неш няколко теста в един, интересен опит :D.

Не е лошо да извикаш няколко различни функции с един assertion, но expected_1 е default-ната поредица. Реално никъде не тестваш успешна поредица, която да не е Fizz/Buzz/Fizzbuzz. Структурата Fizzbuzzer може спокойно да игнорира label-ите си и да използва винаги стандартните 3 и тестовете ще минат, мисля. Теста по-долу, в който ползваш Avada Kedavra, (нарочно) panic-ва, така че и там нямаш успешно генериране :).

Все е нещо -- все пак, като пишеш тестове, опитай се да правиш проверки за различни комплекти от входове и изходи, за да си сигурна, че кода наистина работи върху тях.

#[test]
#[should_panic]
fn test_panic_k1_k2() {
custom_buzz(10, 1, 2);
}
#[test]
#[should_panic]
fn test_panic_label_change() {
let mut fb = FizzBuzzer {
k1: 4,
k2: 6,
labels: [String::from("Avada"), String::from("Kedavra"), String::from("Avada Kedavra")],
};
fb.change_label(5, &String::from("Kedabra"));
}
}

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

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

running 3 tests
test tests::test_custom_and_take ... ok
test tests::test_panic_k1_k2 ... ok
test tests::test_panic_label_change ... ok

test result: ok. 3 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 версия и 1 коментар)

Ася качи първо решение на 27.10.2020 18:46 (преди почти 5 години)