Решение на FMI Buzz от Биляна Йорданова

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

Към профила на Биляна Йорданова

Резултати

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

Код

pub fn fizzbuzz(n: usize) -> Vec<String>
{
let mut vec = Vec::<String>::new();
for i in 1..n+1
{
let fizz : bool = i % 3 == 0;
let buzz : bool = i % 5 == 0;
if fizz && buzz
{
vec.push(String::from("Fizzbuzz"));
}
else if fizz
{
vec.push(String::from("Fizz"));
}
else if buzz
{
vec.push(String::from("Buzz"));
}
else
{
vec.push(i.to_string());
}
}
vec
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String>
{
if k1 == 0 || k1 == 1 || k2 == 0 || k2 == 1
{
panic!("Invalid divisors");
}
let mut vec = Vec::<String>::new();
for i in 1..n+1
{
let fizz : bool = (i as u8) % k1 == 0;
let buzz : bool = (i as u8) % k2 == 0;
if fizz && buzz
{
vec.push(String::from("Fizzbuzz"));
}
else if fizz
{
vec.push(String::from("Fizz"));
}
else if buzz
{
vec.push(String::from("Buzz"));
}
else
{
vec.push(i.to_string());
}
}
vec
}
pub struct FizzBuzzer
{
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer
{
pub fn take(&self, n: usize) -> Vec<String>
{
let n1 = self.k1;
let n2 = self.k2;
let label0 = &self.labels[0];
let label1 = &self.labels[1];
let label2 = &self.labels[2];
if n1 == 0 || n1 == 1 || n2 == 0 || n2 == 1
{
panic!("Invalid divisors");
}
let mut vec = Vec::<String>::new();
for i in 1..n+1
{
let fizz : bool = (i as u8) % n1 == 0;
let buzz : bool = (i as u8) % n2 == 0;
if fizz && buzz
{
vec.push(label2.to_string());
}
else if fizz
{
vec.push(label0.to_string());
}
else if buzz
{
vec.push(label1.to_string());
}
else
{
vec.push(i.to_string());
}
}
vec
}
pub fn change_label(&mut self, index: usize, value: &String)
{
if index > 2
{
panic!("Invalid index of label");
}
self.labels[index] = value.clone();
}
}

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

Compiling solution v0.1.0 (/tmp/d20201028-2816268-zogiww/solution)
    Finished test [unoptimized + debuginfo] target(s) in 2.04s
     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 версии и 4 коментара)

Биляна качи първо решение на 25.10.2020 15:05 (преди 9 месеца)

Биляна качи решение на 26.10.2020 14:40 (преди 9 месеца)

pub fn fizzbuzz(n: usize) -> Vec<String>
{
let mut vec = Vec::<String>::new();
for i in 1..n+1
{
let fizz : bool = i % 3 == 0;
let buzz : bool = i % 5 == 0;
if fizz && buzz
{
- vec.push(String::from("FizzBuzz"));
+ vec.push(String::from("Fizzbuzz"));
}
else if fizz
{
vec.push(String::from("Fizz"));
}
else if buzz
{
vec.push(String::from("Buzz"));
}
else
{
vec.push(i.to_string());
}
}
vec
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String>
{
if k1 == 0 || k1 == 1 || k2 == 0 || k2 == 1
{
panic!("Invalid divisors");
}
let mut vec = Vec::<String>::new();
for i in 1..n+1
{
let fizz : bool = (i as u8) % k1 == 0;
let buzz : bool = (i as u8) % k2 == 0;
if fizz && buzz
{
- vec.push(String::from("FizzBuzz"));
+ vec.push(String::from("Fizzbuzz"));
}
else if fizz
{
vec.push(String::from("Fizz"));
}
else if buzz
{
vec.push(String::from("Buzz"));
}
else
{
vec.push(i.to_string());
}
}
vec
}
pub struct FizzBuzzer
{
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer
{
pub fn take(&self, n: usize) -> Vec<String>
{
let n1 = self.k1;
let n2 = self.k2;
let label0 = &self.labels[0];
let label1 = &self.labels[1];
let label2 = &self.labels[2];
if n1 == 0 || n1 == 1 || n2 == 0 || n2 == 1
{
panic!("Invalid divisors");
}
let mut vec = Vec::<String>::new();
for i in 1..n+1
{
let fizz : bool = (i as u8) % n1 == 0;
let buzz : bool = (i as u8) % n2 == 0;
if fizz && buzz
{
vec.push(label2.to_string());
}
else if fizz
{
vec.push(label0.to_string());
}
else if buzz
{
vec.push(label1.to_string());
}
else
{
vec.push(i.to_string());
}
}
vec
}
pub fn change_label(&mut self, index: usize, value: &String)
{
if index > 2
{
panic!("Invalid index of label");
}
self.labels[index] = value.clone();
}
}

Тестовете винаги са добра идея :). В конкретния случай, просто е въпрос на недочитане на дребен детайл от домашното, така че има шанс и в тестовете да го беше сложила така -- разликата е тънка, особено предвид че структурата се казва FizzBuzzer с camelCase. Разбираемо е донякъде, където го видим, ще го изтъкваме.