Solve fizz-buzz.
This commit is contained in:
parent
0a065d259b
commit
9512a94b6d
|
@ -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
|
||||
}
|
|
@ -3,6 +3,7 @@ pub mod contains_duplicate;
|
|||
pub mod contains_duplicate_ii;
|
||||
pub mod excel_sheet_column_number;
|
||||
pub mod excel_sheet_column_title;
|
||||
pub mod fizz_buzz;
|
||||
pub mod implement_strstr;
|
||||
pub mod integer_to_roman;
|
||||
pub mod isomorphic_strings;
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue