412. Fizz Buzz Python 3 Solution for LeetCode and HackerRank problems

Requirements

Input: integer n

Output: string array answer (1-indexed) where:

  • answer[i] == “FizzBuzz” when i is divisible by both 3 and 5.
  • answer[i] == “Fizz” when i is divisible by 3.
  • answer[i] == “Buzz” when i is divisible by 5.
  • answer[i] == i for no condition matched above.

Examples

n: 3
result is: ['1', '2', 'Fizz']
n: 5
result is: ['1', '2', 'Fizz', '4', 'Buzz']
n: 23
result is: ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz', '16', '17', 'Fizz', '19', 'Buzz', 'Fizz', '22', '23']
n: 15
result is: ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']

Solution in Python 3

Using If elif and modulus operator to determine if the number i is divisible by 3 and 5 then by 3 then by 5 only.

When no condition matches, the actual value of i is stored in the string array and move to the next iteration.

Test Solution

import time
start_time = time.time()
def main():
    print("start")
    

    n = 15
    print ("n: " + str(n))
    
    sol = Solution()
    result = sol.fizzBuzz(n)
    print("result is: "+ str(result))

    print("end")

class Solution:
    def fizzBuzz(self, n: int) -> list[str]:
        result = []
        for i in range(1, n+1):
            if i%3 == 0:
                if i%5 == 0:
                    result.append("FizzBuzz")
                else:
                    result.append("Fizz")
            elif i%5 == 0:
                result.append("Buzz")
            else:
                result.append(str(i))
        return result
if __name__ == "__main__":
    main()
print("--- %s ms ---" % int(((time.time() - start_time)*1000)))

The If / Elif can be either this

class Solution:
    def fizzBuzz(self, n: int) -> list[str]:
        result = []
        for i in range(1, n+1):
            if i%3 == 0:
                if i%5 == 0:
                    result.append("FizzBuzz")
                else:
                    result.append("Fizz")
            elif i%5 == 0:
                result.append("Buzz")
            else:
                result.append(str(i))
        return result

Or like this

class Solution:
    def fizzBuzz(self, n: int) -> list[str]:
        result = []
        for i in range(1, n+1):
            if i%3 == 0:
                if i%5 == 0:
                    result.append("FizzBuzz")
                else:
                    result.append("Fizz")
            elif i%5 == 0:
                result.append("Buzz")
            else:
                result.append(str(i))
        return result

Or checking if divisible by 15, 3, 5 This also works.

class Solution:
    def fizzBuzz(self, n: int) -> list[str]:
        result = []
        for i in range(1, n+1):
            if i%15 == 0:
                result.append("FizzBuzz")
            elif i%3 == 0:
                result.append("Fizz")
            elif i%5 == 0:
                result.append("Buzz")
            else:
                result.append(str(i))
        return result


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.