DEV Community

Vishnuprasad Ranganatha
Vishnuprasad Ranganatha

Posted on

An intersting coding challenge

Given an array of sequential integers, with multiples of 3 and 5 replaced, determine if it's a valid FizzBuzz sequence.

In a valid FizzBuzz sequence:

  • Multiples of 3 are replaced with "Fizz".
  • Multiples of 5 are replaced with "Buzz".
  • Multiples of both 3 and 5 are replaced with "FizzBuzz".
  • All other numbers remain as integers.
  1. is_fizz_buzz(["FizzBuzz", 16, 17, "Fizz", 19, "Buzz"]) should return True
  2. is_fizz_buzz(["Fizz", "Buzz", 101, "Fizz", 103, 104, "FizzBuzz"]) should return True
  3. is_fizz_buzz([1, 2, "Fizz", "Buzz", 5]) should return False

Solution:

  1. find the original array or range of numbers;
  2. check the fizzbuzz condition of the number in original array, and check the fizzbuzz value in input array is same. if both different, return False.
  3. if all are same, return True.

This problem is a bit different because it is not just asking for solving fizz buzz, instead, it is asking to validate the sequence. this will add some additional complexity.

Below is the solution code:

def is_fizz_buzz(arr):
    anum = None;
    anum_index = None;
    for i, item in enumerate(arr):
        if isinstance(item, int):
            anum = item;
            anum_index = i;
            break;
    original_range = [*range(item-i, item-i+len(arr))];
    for item1, item2 in zip(original_range, arr):
        if not item1%3 and not item1%5:
            if item2 != "FizzBuzz":
                return False
        elif not item1%5:
            if item2 != "Buzz":
                return False
        elif not item1%3:
            if item2 != "Fizz":
                return False
    return True;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)