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

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

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

Резултати

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

Код

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
#[test]
fn tests() {
assert_eq!(crate::custom_buzz(0,5,17),Vec::<String>::new());
//crate::custom_buzz(5,0,3);
//crate::custom_buzz(5,3,0);
println!("{:?}",crate::custom_buzz(20,2,3));
let mut fizzbuzzer = crate::FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("-3"),
String::from("-5"),
String::from("-15")
],
};
println!("{:?}",fizzbuzzer.take(15));
fizzbuzzer.change_label(0,&String::from("Fizz"));
println!("{:?}",fizzbuzzer.take(15));
fizzbuzzer.change_label(0,&String::from("Buzz"));
println!("{:?}",fizzbuzzer.take(15));
//fizzbuzzer.change_label(3,&String::from("Buzz"));
}
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
if (k1<=1)||(k2<=1) {
panic!("Super big problem!");
}
let mut ans=Vec::<String>::new();
for i in 1..=n {
let curr : u8 = i as u8;
if (curr%k1==0)&&(curr%k2==0) {
ans.push(String::from("Fizzbuzz"));
}
else if curr%k1==0 {
ans.push(String::from("Fizz"));
}
else if curr%k2==0 {
ans.push(String::from("Buzz"));
}
else {
ans.push(curr.to_string());
}
}
ans
}
pub fn fizzbuzz(n: usize) -> Vec<String> {
custom_buzz(n,3,5)
}
pub struct FizzBuzzer {
pub k1: u8,
pub k2: u8,
pub labels: [String; 3],
}
impl FizzBuzzer {
pub fn take(&self, n: usize) -> Vec<String> {
let k1 = self.k1;
let k2 = self.k2;
if (k1<=1)||(k2<=1) {
panic!("Super big problem!");
}
let mut ans=Vec::<String>::new();
for i in 1..=n {
let curr : u8 = i as u8;
if (curr%k1==0)&&(curr%k2==0) {
ans.push(self.labels[2].clone());
}
else if curr%k1==0 {
ans.push(self.labels[0].clone());
}
else if curr%k2==0 {
ans.push(self.labels[1].clone());
}
else {
ans.push(curr.to_string());
}
}
ans
}
pub fn change_label(&mut self, index: usize, value: &String) {
if index>2 {
panic!("Terrible index!");
}
self.labels[index]=value.clone();
}
}

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

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

running 2 tests
test tests::it_works ... ok
test tests::tests ... ok

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

Илиян качи първо решение на 24.10.2020 17:10 (преди 9 месеца)

Илиян качи решение на 24.10.2020 17:15 (преди 9 месеца)

#[cfg(test)]
mod tests {
- #[test]
- fn it_works() {
- assert_eq!(2 + 2, 4);
- }
#[test]
+ fn it_works() {
+ assert_eq!(2 + 2, 4);
+ }
+ #[test]
fn tests() {
assert_eq!(crate::custom_buzz(0,5,17),Vec::<String>::new());
//crate::custom_buzz(5,0,3);
//crate::custom_buzz(5,3,0);
println!("{:?}",crate::custom_buzz(20,2,3));
let mut fizzbuzzer = crate::FizzBuzzer {
k1: 3,
k2: 5,
labels: [
String::from("-3"),
String::from("-5"),
String::from("-15")
],
};
println!("{:?}",fizzbuzzer.take(15));
fizzbuzzer.change_label(0,&String::from("Fizz"));
println!("{:?}",fizzbuzzer.take(15));
fizzbuzzer.change_label(0,&String::from("Buzz"));
println!("{:?}",fizzbuzzer.take(15));
//fizzbuzzer.change_label(3,&String::from("Buzz"));
}
}
pub fn custom_buzz(n: usize, k1: u8, k2: u8) -> Vec<String> {
- if (k1<=1)||(k2<=1) {
+ if (k1<=1)||(k2<=1) {
panic!("Super big problem!");
}
let mut ans=Vec::<String>::new();
for i in 1..=n {
let curr : u8 = i as u8;
if (curr%k1==0)&&(curr%k2==0) {
ans.push(String::from("Fizzbuzz"));
}
else if curr%k1==0 {
ans.push(String::from("Fizz"));
}
else if curr%k2==0 {
ans.push(String::from("Buzz"));
}
else {
ans.push(curr.to_string());
}
}
ans
}
pub fn fizzbuzz(n: usize) -> Vec<String> {
- custom_buzz(n,3,5)
+ custom_buzz(n,3,5)
}
pub struct FizzBuzzer {
- pub k1: u8,
- pub k2: u8,
- pub labels: [String; 3],
+ pub k1: u8,
+ pub k2: u8,
+ pub labels: [String; 3],
}
impl FizzBuzzer {
-
+
pub fn take(&self, n: usize) -> Vec<String> {
let k1 = self.k1;
let k2 = self.k2;
if (k1<=1)||(k2<=1) {
panic!("Super big problem!");
}
let mut ans=Vec::<String>::new();
for i in 1..=n {
let curr : u8 = i as u8;
if (curr%k1==0)&&(curr%k2==0) {
ans.push(self.labels[2].clone());
}
else if curr%k1==0 {
ans.push(self.labels[0].clone());
}
else if curr%k2==0 {
ans.push(self.labels[1].clone());
}
else {
ans.push(curr.to_string());
}
}
ans
- }
+ }
- pub fn change_label(&mut self, index: usize, value: &String) {
- if index>2 {
+ pub fn change_label(&mut self, index: usize, value: &String) {
+ if index>2 {
panic!("Terrible index!");
}
self.labels[index]=value.clone();
- }
+ }
}