Решение на FMI Buzz от Иван Лучев

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

Към профила на Иван Лучев

Резултати

  • 15 точки от тестове
  • 0 бонус точки
  • 15 точки общо
  • 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 < 2 {
panic!("k1 must be > 1");
}
if self.k2 < 2 {
panic!("k2 must be > 1");
}
let k1 = self.k1 as usize;
let k2 = self.k2 as usize;
let mut result = Vec::<String>::new();
for i in 1..=n {
match i {
_ if i % k1 == 0 && i % k2 != 0 => result.push(self.labels[0].clone()),
_ if i % k1 != 0 && i % k2 == 0 => result.push(self.labels[1].clone()),
_ if i % k1 == 0 && i % k2 == 0 => result.push(self.labels[2].clone()),
_ => result.push(i.to_string()),
}
}
result
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("index out of range");
}
self.labels[index] = value.clone();
}
}
pub fn fizzbuzz(n: usize) -> Vec<String> {
custom_buzz(n, 3, 5)
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
let fizzbuzzer = FizzBuzzer {
k1: k1,
k2: k2,
labels: [
String::from("Fizz"),
String::from("Buzz"),
String::from("Fizzbuzz")
],
};
fizzbuzzer.take(n)
}

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

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

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

Иван качи първо решение на 22.10.2020 15:42 (преди 9 месеца)

Иван качи решение на 22.10.2020 16:46 (преди 9 месеца)

-pub fn fizzbuzz(n: usize) -> Vec<String> {
- let mut result = Vec::<String>::new();
- for i in 1..=n {
- match i {
- _ if i % 3 == 0 && i % 5 != 0 => result.push(String::from("Fizz")),
- _ if i % 3 != 0 && i % 5 == 0 => result.push(String::from("Buzz")),
- _ if i % 3 == 0 && i % 5 == 0 => result.push(String::from("Fizzbuzz")),
- _ => result.push(i.to_string()),
- }
- }
- result
-}
-
-pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
- if k1 < 2 {
- panic!("k1 must be > 1");
- }
- if k2 < 2 {
- panic!("k2 must be > 1");
- }
-
- let k1 = k1 as usize;
- let k2 = k2 as usize;
-
- let mut result = Vec::<String>::new();
- for i in 1..=n {
- match i {
- _ if i % k1 == 0 && i % k2 != 0 => result.push(String::from("Fizz")),
- _ if i % k1 != 0 && i % k2 == 0 => result.push(String::from("Buzz")),
- _ if i % k1 == 0 && i % k2 == 0 => result.push(String::from("Fizzbuzz")),
- _ => result.push(i.to_string()),
- }
- }
- result
-}
-
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 < 2 {
panic!("k1 must be > 1");
}
if self.k2 < 2 {
panic!("k2 must be > 1");
}
let k1 = self.k1 as usize;
let k2 = self.k2 as usize;
let mut result = Vec::<String>::new();
for i in 1..=n {
match i {
_ if i % k1 == 0 && i % k2 != 0 => result.push(self.labels[0].clone()),
_ if i % k1 != 0 && i % k2 == 0 => result.push(self.labels[1].clone()),
_ if i % k1 == 0 && i % k2 == 0 => result.push(self.labels[2].clone()),
_ => result.push(i.to_string()),
}
}
result
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index > 2 {
panic!("index out of range");
}
self.labels[index] = value.clone();
}
+}
+
+pub fn fizzbuzz(n: usize) -> Vec<String> {
+ custom_buzz(n, 3, 5)
+}
+
+pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
+ let fizzbuzzer = FizzBuzzer {
+ k1: k1,
+ k2: k2,
+ labels: [
+ String::from("Fizz"),
+ String::from("Buzz"),
+ String::from("Fizzbuzz")
+ ],
+ };
+
+ fizzbuzzer.take(n)
}