A mirror of my website's source code.

Revised The FizzBuzz Program

by steve0greatness, Thursday, 18 January 2024, 01:37:25 (1705541845), pushed by steve0greatness, Monday, 6 May 2024, 02:55:34 (1714964134)

Author identity: Steve0Greatness <75220768+Steve0Greatness@users.noreply.github.com>

faee4ba040b58665ae6c05221975aeab35e85ac1

blog-posts/fizzbuzz.md

@@ -1,5 +1,46 @@

                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            ---
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            title: The FizzBuzz Program
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            date: 2022 Feb 20
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        updated: 2024 Jan 17
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                            ---
                                        
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        <p>A FizzBuzz Program is a program used in many job interviews to see if a programmer is good at problem solving. There are many ways to make one.</p><p>First let me tell you why I write these programs. These programs, at least in my opinion, are good when you're learning a new programming language. It gives you a problem to solve, and all you need to do to solve it. Incase you're wondering, the problem is to make a program that counts from 1 to 100 and replaces all multiples of 3 with Fizz, all multiples of 5 with Buzz, and multiples of both with FizzBuzz. Generally in interviews, they also ask you to add on more multiples, such as multiples of 7 are replaced with Fuzz, and multiples of 11 are replaced with Bizz.</p><p>Now that I've told you what a FizzBuzz Program is, let me show you how I make them in Psuedo-Code.</p><div class="code">for i in 1-100 {<div style="margin-left:1em">toPrint = ""<br>print(toPrint)</div>}</div><p>The first thing I do is I create a for loop, and within it I put a print statement and a variable named toPrint.</p><div class="code">def check(checktomulti, multi, toreturn) {<div style="margin-left:1em">if checktomulti % multi == 0 {<div style="margin-left:1em">return toreturn</div>}<br>return ""</div>}<br>def checkEmpty(string, number) {<div style="margin-left:1em">if string == "" {<div style="margin-left:1em">return number</div>}<br>return string</div>}<br>for i in 1-100 {<div style="margin-left:1em">toPrint = checkEmpty(check(i, 3, "Fizz") + check(i, 5, "Buzz"), i)<br>print(toPrint)</div>}</div><p>The next thing I do is I define a function that checks if one number is a multiple of another, and if it is, then it returns the string, otherwise, it returns an empty string.</p><p>Then I make a function that checks if a string is an empty one, if it is, then it returns a number.</p><p>Once I have these 2 functions, I go back into the for loop and make the toPrint variable have the variable for checking if a string is empty(and if it is replace it with a number) check if 2 of the other function that check if one number is a multiple of another(and if it is, return a string). Finally, it prints the output.</p><p>I've tried this method many times. Below are some examples of this method in action!</p><ul><li><a href="https://replit.com/@StevesGreatness/FizzBuzzKotlin">Kotlin</a></li><li><a href="https://replit.com/@StevesGreatness/FizzBuzzlua">Lua</a></li><li><a href="https://replit.com/@StevesGreatness/FizzBuzzpython">Python</a></li><li><a href="https://replit.com/@StevesGreatness/FizzBuzzRuby">Ruby</a></li></ul>
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        A FizzBuzz Program is a program used in many job interviews to see if a programmer is good at problem solving. There are many ways to make one.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        First let me tell you why I write these programs. These programs, at least in my opinion, are good when you're learning a new programming language. It gives you a problem to solve, and all you need to do to solve it. Incase you're wondering, the problem is to make a program that counts from 1 to 100 and replaces all multiples of 3 with Fizz, all multiples of 5 with Buzz, and multiples of both with FizzBuzz. Generally in interviews, they also ask you to add on more multiples, such as multiples of 7 are replaced with Fuzz, and multiples of 11 are replaced with Bizz.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        Now that I've told you what a FizzBuzz Program is, let me show you how I make them in Python.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        ```python
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        for i in range(1, 100):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            toPrint = ""
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            print(toPrint)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        ```
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        The first thing I do is I create a for loop, and within it I put a print statement and a variable named toPrint.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        ```python
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        def check(checktomulti, multi, toreturn):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if checktomulti % multi == 0:
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                return toreturn
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            return ""
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        def checkEmpty(string, number):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            if string == "":
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                                return number
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            return string
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        for i in range(1, 100):
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            toPrint = checkEmpty(check(i, 3, "Fizz") + check(i, 5, "Buzz"), i)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                            print(toPrint)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        ```
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        The next thing I do is I define a function that checks if one number is a multiple of another, and if it is, then it returns the string, otherwise, it returns an empty string.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        Then I make a function that checks if a string is an empty one, if it is, then it returns a number.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        Once I have these 2 functions, I go back into the for loop and make the `toPrint` variable have the variable for checking if a string is empty(and if it is replace it with a number) check if 2 of the other function that check if one number is a multiple of another(and if it is, return a string). Finally, it prints the output.
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        I've tried this method many times. Below are some examples of this method in action!
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        * [Kotlin](https://replit.com/@StevesGreatness/FizzBuzzKotlin)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        * [Lua](https://replit.com/@StevesGreatness/FizzBuzzlua)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        * [Python](https://replit.com/@StevesGreatness/FizzBuzzpython)
                                        
                                        
                                        
                                    
                                
                                
                                
                            
                                
                                    
                                        
                                        * [Ruby](https://replit.com/@StevesGreatness/FizzBuzzRuby)