From 9512a94b6d5acd09ace96361373222ab9bcee921 Mon Sep 17 00:00:00 2001 From: Bauke Date: Wed, 13 Apr 2022 19:49:19 +0200 Subject: [PATCH] Solve fizz-buzz. --- source/fizz_buzz/mod.rs | 16 ++++++++++++++++ source/lib.rs | 1 + tests/fizz_buzz.rs | 15 +++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 source/fizz_buzz/mod.rs create mode 100644 tests/fizz_buzz.rs diff --git a/source/fizz_buzz/mod.rs b/source/fizz_buzz/mod.rs new file mode 100644 index 0000000..a6ee94b --- /dev/null +++ b/source/fizz_buzz/mod.rs @@ -0,0 +1,16 @@ +pub fn fizz_buzz(number: i32) -> Vec { + 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 +} diff --git a/source/lib.rs b/source/lib.rs index d1b3fad..f87d131 100644 --- a/source/lib.rs +++ b/source/lib.rs @@ -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; diff --git a/tests/fizz_buzz.rs b/tests/fizz_buzz.rs new file mode 100644 index 0000000..395d425 --- /dev/null +++ b/tests/fizz_buzz.rs @@ -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); +}