feat: Add fizzbuzz.rs exercise

This commit is contained in:
Rust & Python 2021-09-29 21:29:30 +05:30
parent ad338ee789
commit 53b3036825

49
exercises/fizzbuzz.rs Normal file
View File

@ -0,0 +1,49 @@
// fizzbuzz challenge
// Make a simple code which fulfills the given tests
// Starting code is already given to you :)
/* Write a program that substitues the following:
* mulitples of 3 => "fizz"
* multiples of 5 => "buzz"
* mulitples of 3 and 5 => "fizzbuzz"
*/
// I AM NOT DONE
pub fn fizzbuzz(num: i32) -> String {
match (num % 3, num % 5) {
(0, 0) => "fizzbuzz".to_string(),
(0, _) => "fizz".to_string(),
(_, 0) => "buzz".to_string(),
(_, _) => num.to_string(),
}
}
// Don't change this part(the tests)
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fizz() {
assert_eq!(fizzbuzz(3), "fizz".to_string());
assert_eq!(fizzbuzz(6), "fizz".to_string());
assert_eq!(fizzbuzz(53), "53".to_string());
}
#[test]
fn buzz() {
assert_eq!(fizzbuzz(5), "buzz".to_string());
assert_eq!(fizzbuzz(10), "buzz".to_string());
assert_eq!(fizzbuzz(13), "13".to_string());
}
#[test]
fn fizz_and_buzz() {
assert_eq!(fizzbuzz(15), "fizzbuzz".to_string());
assert_eq!(fizzbuzz(45), "fizzbuzz".to_string());
assert_eq!(fizzbuzz(31), "31".to_string());
}
}