1
Fork 0

Solve fizz-buzz.

This commit is contained in:
Bauke 2022-04-13 19:49:19 +02:00
parent 0a065d259b
commit 9512a94b6d
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 32 additions and 0 deletions

16
source/fizz_buzz/mod.rs Normal file
View File

@ -0,0 +1,16 @@
pub fn fizz_buzz(number: i32) -> Vec<String> {
let mut result = vec![];
for index in 1..=number {
let string = match (index % 3 == 0, index % 5 == 0) {
(true, true) => "FizzBuzz".to_string(),
(true, false) => "Fizz".to_string(),
(false, true) => "Buzz".to_string(),
(false, false) => index.to_string(),
};
result.push(string);
}
result
}

View File

@ -3,6 +3,7 @@ pub mod contains_duplicate;
pub mod contains_duplicate_ii; pub mod contains_duplicate_ii;
pub mod excel_sheet_column_number; pub mod excel_sheet_column_number;
pub mod excel_sheet_column_title; pub mod excel_sheet_column_title;
pub mod fizz_buzz;
pub mod implement_strstr; pub mod implement_strstr;
pub mod integer_to_roman; pub mod integer_to_roman;
pub mod isomorphic_strings; pub mod isomorphic_strings;

15
tests/fizz_buzz.rs Normal file
View File

@ -0,0 +1,15 @@
use leetcode::fizz_buzz::fizz_buzz;
use test_case::test_case;
const EXAMPLE_3: &[&str] = &[
"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11",
"Fizz", "13", "14", "FizzBuzz",
];
#[test_case(3, &["1", "2", "Fizz"]; "example 1")]
#[test_case(5, &["1", "2", "Fizz", "4", "Buzz"]; "example 2")]
#[test_case(15, EXAMPLE_3; "example 3")]
fn test_fizz_buzz(input: i32, expected: &[&str]) {
assert_eq!(fizz_buzz(input), expected);
}