A mirror of my website's source code.

By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 fizzbuzz.md

View raw Download
text/x-script.python • 2.76 kiB
Python script, ASCII text executable, with very long lines (570)

title: The FizzBuzz Program date: 2022 Feb 20 updated: 2024 Feb 2

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.

for Index in range(1, 100):
   Response = ""
   print(Response)

The first thing I do is I create a for loop, and within it I put a print statement and a variable named toPrint.

def FizzCompute(Number, Divisor, String):
   return String if Number % Divisor == 0 else ""

def SubstituteEmpty(String, Number):
   return String if String != "" else Number
   
for Index in range(1, 100):
   Response = SubstituteEmpty(FizzCompute(Index, 3, "Fizz") + FizzCompute(Index, 5, "Buzz"), Index)
   print(Response)

Output 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz

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 Response 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!

                
                    
1
title: The FizzBuzz Program
2
date: 2022 Feb 20
3
updated: 2024 Feb 2
4
5
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.
6
7
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.
8
9
Now that I've told you what a FizzBuzz Program is, let me show you how I make them in Python.
10
11
```python
12
for Index in range(1, 100):
13
Response = ""
14
print(Response)
15
```
16
17
The first thing I do is I create a for loop, and within it I put a print statement and a variable named toPrint.
18
19
```python
20
def FizzCompute(Number, Divisor, String):
21
return String if Number % Divisor == 0 else ""
22
23
def SubstituteEmpty(String, Number):
24
return String if String != "" else Number
25
26
for Index in range(1, 100):
27
Response = SubstituteEmpty(FizzCompute(Index, 3, "Fizz") + FizzCompute(Index, 5, "Buzz"), Index)
28
print(Response)
29
```
30
<details>
31
<summary>Output</summary>
32
<samp>1
33
2
34
Fizz
35
4
36
Buzz
37
Fizz
38
7
39
8
40
Fizz
41
Buzz
42
11
43
Fizz
44
13
45
14
46
FizzBuzz
47
16
48
17
49
Fizz
50
19
51
Buzz
52
Fizz
53
22
54
23
55
Fizz
56
Buzz
57
26
58
Fizz
59
28
60
29
61
FizzBuzz
62
31
63
32
64
Fizz
65
34
66
Buzz
67
Fizz
68
37
69
38
70
Fizz
71
Buzz
72
41
73
Fizz
74
43
75
44
76
FizzBuzz
77
46
78
47
79
Fizz
80
49
81
Buzz
82
Fizz
83
52
84
53
85
Fizz
86
Buzz
87
56
88
Fizz
89
58
90
59
91
FizzBuzz
92
61
93
62
94
Fizz
95
64
96
Buzz
97
Fizz
98
67
99
68
100
Fizz
101
Buzz
102
71
103
Fizz
104
73
105
74
106
FizzBuzz
107
76
108
77
109
Fizz
110
79
111
Buzz
112
Fizz
113
82
114
83
115
Fizz
116
Buzz
117
86
118
Fizz
119
88
120
89
121
FizzBuzz
122
91
123
92
124
Fizz
125
94
126
Buzz
127
Fizz
128
97
129
98
130
Fizz</samp>
131
</details>
132
133
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.
134
135
Then I make a function that checks if a string is an empty one, if it is, then it returns a number.
136
137
Once I have these 2 functions, I go back into the for loop and make the `Response` 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.
138
139
I've tried this method many times. Below are some examples of this method in action!
140
141
* [Kotlin](https://replit.com/@StevesGreatness/FizzBuzzKotlin)
142
* [Lua](https://replit.com/@StevesGreatness/FizzBuzzlua)
143
* [Python](https://replit.com/@StevesGreatness/FizzBuzzpython)
144
* [Ruby](https://replit.com/@StevesGreatness/FizzBuzzRuby)
145