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