Решение на FMI Buzz от Цветелина Стоянова

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

Към профила на Цветелина Стоянова

Резултати

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

Код

pub fn fizzbuzz(n: usize) -> Vec<String> {
return basic_buzz(n, 3, 5, "Fizz".to_string(), "Buzz".to_string(), "Fizzbuzz".to_string());
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
if n == 0 {
return Vec::new();
}
if k1 * k2 == 0 || k1 == 1 || k2 == 1 {
panic!("Not a valid input!");
}
return basic_buzz(n, usize::from(k1), usize::from(k2), "Fizz".to_string(), "Buzz".to_string(), "Fizzbuzz".to_string());
}
fn basic_buzz(n: usize, k1: usize, k2: usize, first_word: String, second_word: String, both_words: String) -> Vec<String> {
let mut words: Vec<String> = Vec::with_capacity(n + 1);
for i in 1..n + 1 {
let next_word = if i % (k1 * k2) == 0 {
String::from(both_words.to_string())
} else if i % k1 == 0 {
String::from(first_word.to_string())
} else if i % k2 == 0 {
String::from(second_word.to_string())
} else {
String::from(i.to_string())
};
words.push(next_word);
}
return words;
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
return basic_buzz(n, usize::from(self.k1), usize::from(self.k2), self.labels[0].to_string(), self.labels[1].to_string(), self.labels[2].to_string());
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Not a valid input!");
}
self.labels[index] = value.to_string();
}
}

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

Compiling solution v0.1.0 (/tmp/d20201028-2816268-g1jdph/solution)
    Finished test [unoptimized + debuginfo] target(s) in 2.06s
     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 ... FAILED
test solution_test::test_zeroes ... ok

failures:

---- solution_test::test_struct_invalid stdout ----
thread 'main' panicked at 'attempt to calculate the remainder with a divisor of zero', src/lib.rs:21:28
thread 'main' panicked at 'attempt to calculate the remainder with a divisor of zero', src/lib.rs:21:28
thread 'main' panicked at 'assertion failed: catch_unwind(|| { fizzbuzzer!(3, 1); }).is_err()', tests/solution_test.rs:102:5


failures:
    solution_test::test_struct_invalid

test result: FAILED. 9 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--test solution_test'

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

Цветелина качи първо решение на 28.10.2020 15:20 (преди почти 5 години)

Цветелина качи решение на 28.10.2020 15:27 (преди почти 5 години)

pub fn fizzbuzz(n: usize) -> Vec<String> {
- return basic_buzz(n, 3, 5, "Fizz".to_string(), "Buzz".to_string(), "FizzBuzz".to_string());
+ return basic_buzz(n, 3, 5, "Fizz".to_string(), "Buzz".to_string(), "Fizzbuzz".to_string());
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
if n == 0 {
return Vec::new();
}
if k1 * k2 == 0 || k1 == 1 || k2 == 1 {
panic!("Not a valid input!");
}
- return basic_buzz(n, usize::from(k1), usize::from(k2), "Fizz".to_string(), "Buzz".to_string(), "FizzBuzz".to_string());
+ return basic_buzz(n, usize::from(k1), usize::from(k2), "Fizz".to_string(), "Buzz".to_string(), "Fizzbuzz".to_string());
}
fn basic_buzz(n: usize, k1: usize, k2: usize, first_word: String, second_word: String, both_words: String) -> Vec<String> {
let mut words: Vec<String> = Vec::with_capacity(n + 1);
for i in 1..n + 1 {
let next_word = if i % (k1 * k2) == 0 {
String::from(both_words.to_string())
} else if i % k1 == 0 {
String::from(first_word.to_string())
} else if i % k2 == 0 {
String::from(second_word.to_string())
} else {
String::from(i.to_string())
};
words.push(next_word);
}
return words;
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
return basic_buzz(n, usize::from(self.k1), usize::from(self.k2), self.labels[0].to_string(), self.labels[1].to_string(), self.labels[2].to_string());
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("Not a valid input!");
}
self.labels[index] = value.to_string();
}
}