Move History

Rooted by: FizzBuzz Prime
Fork Selected
  • Code
    fn fizzbuzz(n: u32) -> String {
        if is_prime(n) {
            "Prime".to_owned()
        } else if n % 15 == 0 {
            "FizzBuzz".to_owned()
        } else if n % 3 == 0 {
            "Fizz".to_owned()
        } else if n % 5 == 0 {
            "Buzz".to_owned()
        } else {
            n.to_string()
        }
    }
    
    // inefficient but this is just a simple demonstration
    fn is_prime(n: u32) -> bool {
        n > 1 && (2..n).all(|d| n % d != 0)
    }
    Test Cases
    #[test]
    fn test_prime() {
        let prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
        for n in prime_numbers {
            assert_eq!(fizzbuzz(n), "Prime");
        }
    }
    
    #[test]
    fn test_fizz() {
        let fizz_numbers = [6, 9, 12, 18, 21, 24, 27, 33, 36, 39, 42, 48, 51, 54, 57, 63, 66, 69, 72, 78, 81, 84, 87, 93, 96, 99];
        for n in fizz_numbers {
            assert_eq!(fizzbuzz(n), "Fizz");
        }
    }
    
    #[test]
    fn test_buzz() {
        let buzz_numbers = [10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95, 100];
        for n in buzz_numbers {
            assert_eq!(fizzbuzz(n), "Buzz");
        }
    }
    
    #[test]
    fn test_fizzbuzz() {
        let fizzbuzz_numbers = [15, 30, 45, 60, 75, 90];
        for n in fizzbuzz_numbers {
            assert_eq!(fizzbuzz(n), "FizzBuzz");
        }
    }
                
    #[test]
    fn test_all() {
        let sample_list = [
            "1", "Prime", "Prime", "4", "Prime", "Fizz", "Prime", "8", "Fizz", "Buzz", "Prime", "Fizz", "Prime", "14", "FizzBuzz",
            "16", "Prime", "Fizz", "Prime", "Buzz", "Fizz", "22", "Prime", "Fizz", "Buzz", "26", "Fizz", "28", "Prime", "FizzBuzz",
            "Prime", "32", "Fizz", "34", "Buzz", "Fizz", "Prime", "38", "Fizz", "Buzz", "Prime", "Fizz", "Prime", "44", "FizzBuzz",
            "46", "Prime", "Fizz", "49", "Buzz", "Fizz", "52", "Prime", "Fizz", "Buzz", "56", "Fizz", "58", "Prime", "FizzBuzz",
            "Prime", "62", "Fizz", "64", "Buzz", "Fizz", "Prime", "68", "Fizz", "Buzz", "Prime", "Fizz", "Prime", "74", "FizzBuzz", 
            "76", "77", "Fizz", "Prime", "Buzz", "Fizz", "82", "Prime", "Fizz", "Buzz", "86", "Fizz", "88", "Prime", "FizzBuzz", 
            "91", "92", "Fizz", "94", "Buzz", "Fizz", "Prime", "98", "Fizz", "Buzz"
        ];
        
        for (sample, expected) in (1..=100).zip(sample_list) {
            assert_eq!(fizzbuzz(sample), expected);
        }
    }
        
  • Code
    • class IsPrimeNumber:
    • """Returns True if n is a prime number, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • class Fizz:
    • """Returns True if n is divisible by 3, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • class Buzz:
    • """Returns True if n is divisible by 5, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • class FizzBuzz:
    • """Returns True if n is divisible by 3 and 5, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • class CodeWarKata776:
    • """Executes the Fizz, Bizz, FizzBuzz Prime sequence."""
    • def __init__(self, n):
    • self.n = n
    • def calculate_prime(self):
    • pass
    • def calculate_fizz(self):
    • pass
    • def calculate_buzz(self):
    • pass
    • def calculate_fizzbuzz(self):
    • pass
    • def execute(self):
    • pass
    • fn fizzbuzz(n: u32) -> String {
    • if is_prime(n) {
    • "Prime".to_owned()
    • } else if n % 15 == 0 {
    • "FizzBuzz".to_owned()
    • } else if n % 3 == 0 {
    • "Fizz".to_owned()
    • } else if n % 5 == 0 {
    • "Buzz".to_owned()
    • } else {
    • n.to_string()
    • }
    • }
    • // inefficient but this is just a simple demonstration
    • fn is_prime(n: u32) -> bool {
    • n > 1 && (2..n).all(|d| n % d != 0)
    • }
    Test Cases
    • import codewars_test as test
    • from solution import CodeWarKata776
    • #[test]
    • fn test_prime() {
    • let prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
    • for n in prime_numbers {
    • assert_eq!(fizzbuzz(n), "Prime");
    • }
    • }
    • @test.describe("Test CodeWarKata776")
    • def test_group():
    • @test.it("test 1: Test calculate_prime()")
    • def test_case():
    • prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    • for n in prime_numbers:
    • test.assert_equals(CodeWarKata776(n).calculate_prime(), True)
    • @test.it("test 2: Test calculate_fizz()")
    • def test_case():
    • fizz_numbers = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81,
    • 84, 87, 87, 90, 93, 96, 99]
    • for n in fizz_numbers:
    • test.assert_equals(CodeWarKata776(n).calculate_fizz(), True)
    • @test.it("test 3: Test calculate_buzz()")
    • def test_case():
    • buzz_numbers = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
    • for n in buzz_numbers:
    • test.assert_equals(CodeWarKata776(n).calculate_buzz(), True)
    • #[test]
    • fn test_fizz() {
    • let fizz_numbers = [6, 9, 12, 18, 21, 24, 27, 33, 36, 39, 42, 48, 51, 54, 57, 63, 66, 69, 72, 78, 81, 84, 87, 93, 96, 99];
    • for n in fizz_numbers {
    • assert_eq!(fizzbuzz(n), "Fizz");
    • }
    • }
    • #[test]
    • fn test_buzz() {
    • let buzz_numbers = [10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95, 100];
    • for n in buzz_numbers {
    • assert_eq!(fizzbuzz(n), "Buzz");
    • }
    • }
    • @test.it("test 4: Test calculate_fizzbuzz()")
    • def test_case():
    • fizz_buzz_numbers = [15, 30, 45, 60, 75, 90]
    • for n in fizz_buzz_numbers:
    • test.assert_equals(CodeWarKata776(n).calculate_fizzbuzz(), True)
    • #[test]
    • fn test_fizzbuzz() {
    • let fizzbuzz_numbers = [15, 30, 45, 60, 75, 90];
    • for n in fizzbuzz_numbers {
    • assert_eq!(fizzbuzz(n), "FizzBuzz");
    • }
    • }
    • #[test]
    • fn test_all() {
    • let sample_list = [
    • "1", "Prime", "Prime", "4", "Prime", "Fizz", "Prime", "8", "Fizz", "Buzz", "Prime", "Fizz", "Prime", "14", "FizzBuzz",
    • "16", "Prime", "Fizz", "Prime", "Buzz", "Fizz", "22", "Prime", "Fizz", "Buzz", "26", "Fizz", "28", "Prime", "FizzBuzz",
    • "Prime", "32", "Fizz", "34", "Buzz", "Fizz", "Prime", "38", "Fizz", "Buzz", "Prime", "Fizz", "Prime", "44", "FizzBuzz",
    • "46", "Prime", "Fizz", "49", "Buzz", "Fizz", "52", "Prime", "Fizz", "Buzz", "56", "Fizz", "58", "Prime", "FizzBuzz",
    • "Prime", "62", "Fizz", "64", "Buzz", "Fizz", "Prime", "68", "Fizz", "Buzz", "Prime", "Fizz", "Prime", "74", "FizzBuzz",
    • "76", "77", "Fizz", "Prime", "Buzz", "Fizz", "82", "Prime", "Fizz", "Buzz", "86", "Fizz", "88", "Prime", "FizzBuzz",
    • "91", "92", "Fizz", "94", "Buzz", "Fizz", "Prime", "98", "Fizz", "Buzz"
    • ];
    • @test.it("test 5: Test execute()")
    • def test_case():
    • sample_list = [1, 'Prime', 'Prime', 4, 'Prime', 'Fizz', 'Prime', 8, 'Fizz', 'Buzz', 'Prime', 'Fizz', 'Prime', 14, 'FizzBuzz', 16,
    • 'Prime', 'Fizz', 'Prime', 'Buzz', 'Fizz', 22, 'Prime', 'Fizz', 'Buzz', 26, 'Fizz', 28, 'Prime', 'FizzBuzz',
    • 'Prime', 32, 'Fizz', 34, 'Buzz', 'Fizz', 'Prime', 38, 'Fizz', 'Buzz', 'Prime', 'Fizz', 'Prime', 44, 'FizzBuzz', 46,
    • 'Prime', 'Fizz', 49, 'Buzz', 'Fizz', 52, 'Prime', 'Fizz', 'Buzz', 56, 'Fizz', 58, 'Prime', 'FizzBuzz', 'Prime', 62,
    • 'Fizz', 64, 'Buzz', 'Fizz', 'Prime', 68, 'Fizz', 'Buzz', 'Prime', 'Fizz', 'Prime', 74, 'FizzBuzz', 76, 77, 'Fizz',
    • 'Prime', 'Buzz', 'Fizz', 82, 'Prime', 'Fizz', 'Buzz', 86, 'Fizz', 88, 'Prime', 'FizzBuzz', 91, 92, 'Fizz', 94,
    • 'Buzz', 'Fizz', 'Prime', 98, 'Fizz', 'Buzz']
    • for sample, expectd in zip(range(1,101), sample_list):
    • test.assert_equals(CodeWarKata776(sample).execute(), expectd)
    • for (sample, expected) in (1..=100).zip(sample_list) {
    • assert_eq!(fizzbuzz(sample), expected);
    • }
    • }