Решение на FMI Buzz от Пламен Николов

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

Към профила на Пламен Николов

Резултати

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

Код

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 <= 1 {
panic!("k1 and k2 must be different from 0 and 1");
}
let mut result: Vec<String> = Vec::new();
for i in 1..n + 1 {
if i % (self.k1 as usize) == 0 && i % (self.k2 as usize) == 0 {
result.push(self.labels[2].clone());
} else if i % (self.k1 as usize) == 0 {
result.push(self.labels[0].clone());
} else if i % (self.k2 as usize) == 0 {
result.push(self.labels[1].clone());
} else {
result.push(i.to_string());
}
}
return result;
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Index should be between 0 and 2 inclusive.");
}
self.labels[index] = value.clone();
}
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
let fizzbuzzer = FizzBuzzer {
k1,
k2,
labels: [String::from("Fizz"), String::from("Buzz"), String::from("Fizzbuzz")],
};
return fizzbuzzer.take(n);
}
pub fn fizzbuzz(n: usize) -> Vec<String> {
return custom_buzz(n , 3, 5);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fizzbuzz_15() {
let expected = vec![1.to_string(), 2.to_string(), String::from("Fizz"),
4.to_string(), String::from("Buzz"), String::from("Fizz"),
7.to_string(), 8.to_string(), String::from("Fizz"),
String::from("Buzz"), 11.to_string(), String::from("Fizz"),
13.to_string(), 14.to_string(), String::from("Fizzbuzz")];
assert_eq!(fizzbuzz(15), expected);
}
#[test]
fn test_fizzbuzz_n0() {
let expected: Vec<String> = vec![];
assert_eq!(fizzbuzz(0), expected);
}
#[test]
fn test_custom_buzz_8_3_2() {
let expected = vec![1.to_string(), String::from("Buzz"), String::from("Fizz"),
String::from("Buzz"), 5.to_string(), String::from("Fizzbuzz"),
7.to_string(), String::from("Buzz")];
assert_eq!(custom_buzz(8, 3, 2), expected);
}
#[test]
fn test_custom_buzz_n0() {
let expected: Vec<String> = vec![];
assert_eq!(custom_buzz(0, 3, 2), expected);
}
#[test]
#[should_panic]
fn test_custom_buzz_invalid_k1() {
custom_buzz(0, 0, 2);
}
#[test]
#[should_panic]
fn test_custom_buzz_invalid_k2() {
custom_buzz(0, 2, 1);
}
#[test]
fn test_fizz_buzzer_take_with_9_2_3_and_labels_abra_kadabra_alakazam() {
let fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 3,
labels: [String::from("abra"), String::from("kadabra"), String::from("alakazam")],
};
let expected = vec![1.to_string(), String::from("abra"), String::from("kadabra"),
String::from("abra"), 5.to_string(), String::from("alakazam"),
7.to_string(), String::from("abra"), String::from("kadabra")];
assert_eq!(fizzbuzzer.take(9), expected);
}
#[test]
fn test_fizz_buzzer_take_with_n0() {
let fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 3,
labels: [String::from("abra"), String::from("kadabra"), String::from("alakazam")],
};
let expected: Vec<String> = vec![];
assert_eq!(fizzbuzzer.take(0), expected);
}
#[test]
#[should_panic]
fn test_fizz_buzzer_take_with_invalid_k1() {
let fizzbuzzer = FizzBuzzer {
k1: 0,
k2: 3,
labels: [String::from("abra"), String::from("kadabra"), String::from("alakazam")],
};
fizzbuzzer.take(0);
}
#[test]
#[should_panic]
fn test_fizz_buzzer_take_with_invalid_k2() {
let fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 1,
labels: [String::from("abra"), String::from("kadabra"), String::from("alakazam")],
};
fizzbuzzer.take(0);
}
#[test]
fn test_fizz_buzzer_change_label() {
let mut fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 1,
labels: [String::from("abra"), String::from("kadabra"), String::from("alakazam")],
};
fizzbuzzer.change_label(0, &String::from("pichu"));
fizzbuzzer.change_label(1, &String::from("pikachu"));
fizzbuzzer.change_label(2, &String::from("raichu"));
let expected = [String::from("pichu"), String::from("pikachu"), String::from("raichu")];
assert_eq!(fizzbuzzer.labels, expected);
}
#[test]
#[should_panic]
fn test_fizz_buzzer_change_label_with_invalid_index() {
let mut fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 1,
labels: [String::from("abra"), String::from("kadabra"), String::from("alakazam")],
};
fizzbuzzer.change_label(3, &String::from("mega alakazam"));
}
#[test]
fn test_fizz_buzzer_take_after_label_change() {
let mut fizzbuzzer = FizzBuzzer {
k1: 2,
k2: 3,
labels: [String::from("abra"), String::from("kadabra"), String::from("alakazam")],
};
fizzbuzzer.change_label(2, &String::from("mega alakazam"));
let expected = vec![1.to_string(), String::from("abra"), String::from("kadabra"),
String::from("abra"), 5.to_string(), String::from("mega alakazam"),
7.to_string(), String::from("abra"), String::from("kadabra")];
assert_eq!(fizzbuzzer.take(9), expected);
}
}

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

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

running 13 tests
test tests::test_custom_buzz_8_3_2 ... ok
test tests::test_custom_buzz_invalid_k1 ... ok
test tests::test_custom_buzz_invalid_k2 ... ok
test tests::test_custom_buzz_n0 ... ok
test tests::test_fizz_buzzer_change_label ... ok
test tests::test_fizz_buzzer_change_label_with_invalid_index ... ok
test tests::test_fizz_buzzer_take_after_label_change ... ok
test tests::test_fizz_buzzer_take_with_9_2_3_and_labels_abra_kadabra_alakazam ... ok
test tests::test_fizz_buzzer_take_with_invalid_k1 ... ok
test tests::test_fizz_buzzer_take_with_invalid_k2 ... ok
test tests::test_fizz_buzzer_take_with_n0 ... ok
test tests::test_fizzbuzz_15 ... ok
test tests::test_fizzbuzz_n0 ... ok

test result: ok. 13 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 коментар)

Пламен качи първо решение на 22.10.2020 19:27 (преди 9 месеца)

Добър комплект от тестове -- може да хвърлиш и едно око на нашия пълен тест за идеи как би могъл да съкратиш малко писането, макар че нещата там още не сме ги преподали -- макроси и catch_unwind (но ще стигнем и до тях).