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.html

View raw Download
text/html • 9.19 kiB
HTML document, ASCII text, with very long lines (653)
        
            
1
<!DOCTYPE html>
2
<html lang="en-us" prefix="og: https://ogp.me/ns# article: http://ogp.me/ns/article# profile: https://ogp.me/ns/profile#">
3
4
<head>
5
6
7
<meta charset="UTF-8" />
8
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
9
<title>The FizzBuzz Program - S0G</title>
10
<link rel="stylesheet" href="/src/global.css" />
11
<meta property="og:locale" content="en_US" />
12
<meta property="og:site_name" content="Steve0Greatness" />
13
<meta property="og:image" content="/OG-Image.png" />
14
15
<link rel="stylesheet" href="/src/code-blocks.css" />
16
<link rel="stylesheet" href="/src/blog.css" />
17
<link rel="alternate" href="/blog/fizzbuzz.txt" type="text/plain" title="Post source" />
18
<meta property="og:title" content="The FizzBuzz Program" />
19
<meta property="og:type" content="article" />
20
<meta property="article:published_time" content="2022-02-20T00:00:00Z" />
21
<meta property="article:author" content"https://steve0greatness.github.io" />
22
<meta property="article:modified_time" content="2024-02-02T00:00:00Z" />
23
<meta property="profile:first_name" content="Steve0Greatness" />
24
<meta property="profile:username" content="Steve0Greatness" />
25
<meta property="profile:gender" content="male" />
26
<meta property="og:url" content="https://steve0greatness.github.io/blog/fizzbuzz.html" />
27
28
</head>
29
30
<body>
31
<header>
32
<h2><a href="/"><img src="/SteveLogo.webp" height="35" width="215" alt="Steve0Greatness" /></a></h2>
33
<nav>
34
<a href="/blog">Blog</a>
35
<a href="/list/link-tree.html">Link Tree</a>
36
</nav>
37
</header>
38
39
<nav aria-label="breadcrumbs" aria-roledescription="Site breadcrumb">
40
<ol class="breadcrumbs">
41
42
<li>
43
<a href="/">Index</a>
44
</li>
45
46
<li >
47
<a
48
49
href="/blog"
50
>Blog Index</a>
51
</li>
52
53
<li >
54
<a
55
aria-current="location"
56
href="/blog/fizzbuzz.html"
57
>The FizzBuzz Program</a>
58
</li>
59
60
61
</ol>
62
</nav>
63
<main>
64
<h1>The FizzBuzz Program</h1>
65
<article>
66
<header>
67
<div role="toolbar" class="toolbar">
68
<strong>Share</strong>
69
<a href="https://toot.kytta.dev/?text=Take a look at this article by @S0G@mastodon.social: https://steve0greatness.github.io/blog/fizzbuzz.html" title="Share to Mastodon">
70
<img src="/toot-kytta-dev-icon.png" width="16" height="16" aria-hidden="true" title="Share to Mastodon" />
71
</a>
72
<a href="/blog/fizzbuzz.html" title="Direct link">
73
<img src="/link-icon.png" width="16" height="16" aria-hidden="true" title="Direct link" />
74
</a>
75
<a href="/blog/fizzbuzz.txt" title="Markdown source">
76
<img src="/md-src.png" width="16" height="16" aria-hidden="true" />
77
</a>
78
</div>
79
<div class="time-stamps">
80
<time datetime="2022-02-20T00:00:00-08:00">2022 Feb 20 PST</time>
81
- <span aria-hidden="true" style="font-style:italic">Revision as of: </span> <time datetime="2024-02-02T00:00:00-08:00" aria-label="Revision">2024 Feb 2 PST</time>
82
83
</div>
84
</header>
85
<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>
86
87
<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>
88
89
<p>Now that I've told you what a FizzBuzz Program is, let me show you how I make them in Python.</p>
90
91
<div class="codehilite">
92
<pre><span></span><code><span class="k">for</span> <span class="n">Index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">100</span><span class="p">):</span>
93
<span class="n">Response</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
94
<span class="nb">print</span><span class="p">(</span><span class="n">Response</span><span class="p">)</span>
95
</code></pre>
96
</div>
97
98
<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>
99
100
<div class="codehilite">
101
<pre><span></span><code><span class="k">def</span> <span class="nf">FizzCompute</span><span class="p">(</span><span class="n">Number</span><span class="p">,</span> <span class="n">Divisor</span><span class="p">,</span> <span class="n">String</span><span class="p">):</span>
102
<span class="k">return</span> <span class="n">String</span> <span class="k">if</span> <span class="n">Number</span> <span class="o">%</span> <span class="n">Divisor</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span>
103
104
<span class="k">def</span> <span class="nf">SubstituteEmpty</span><span class="p">(</span><span class="n">String</span><span class="p">,</span> <span class="n">Number</span><span class="p">):</span>
105
<span class="k">return</span> <span class="n">String</span> <span class="k">if</span> <span class="n">String</span> <span class="o">!=</span> <span class="s2">&quot;&quot;</span> <span class="k">else</span> <span class="n">Number</span>
106
107
<span class="k">for</span> <span class="n">Index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">100</span><span class="p">):</span>
108
<span class="n">Response</span> <span class="o">=</span> <span class="n">SubstituteEmpty</span><span class="p">(</span><span class="n">FizzCompute</span><span class="p">(</span><span class="n">Index</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="s2">&quot;Fizz&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="n">FizzCompute</span><span class="p">(</span><span class="n">Index</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="s2">&quot;Buzz&quot;</span><span class="p">),</span> <span class="n">Index</span><span class="p">)</span>
109
<span class="nb">print</span><span class="p">(</span><span class="n">Response</span><span class="p">)</span>
110
</code></pre>
111
</div>
112
113
<p><details>
114
<summary>Output</summary>
115
<samp>1
116
2
117
Fizz
118
4
119
Buzz
120
Fizz
121
7
122
8
123
Fizz
124
Buzz
125
11
126
Fizz
127
13
128
14
129
FizzBuzz
130
16
131
17
132
Fizz
133
19
134
Buzz
135
Fizz
136
22
137
23
138
Fizz
139
Buzz
140
26
141
Fizz
142
28
143
29
144
FizzBuzz
145
31
146
32
147
Fizz
148
34
149
Buzz
150
Fizz
151
37
152
38
153
Fizz
154
Buzz
155
41
156
Fizz
157
43
158
44
159
FizzBuzz
160
46
161
47
162
Fizz
163
49
164
Buzz
165
Fizz
166
52
167
53
168
Fizz
169
Buzz
170
56
171
Fizz
172
58
173
59
174
FizzBuzz
175
61
176
62
177
Fizz
178
64
179
Buzz
180
Fizz
181
67
182
68
183
Fizz
184
Buzz
185
71
186
Fizz
187
73
188
74
189
FizzBuzz
190
76
191
77
192
Fizz
193
79
194
Buzz
195
Fizz
196
82
197
83
198
Fizz
199
Buzz
200
86
201
Fizz
202
88
203
89
204
FizzBuzz
205
91
206
92
207
Fizz
208
94
209
Buzz
210
Fizz
211
97
212
98
213
Fizz</samp>
214
</details></p>
215
216
<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>
217
218
<p>Then I make a function that checks if a string is an empty one, if it is, then it returns a number.</p>
219
220
<p>Once I have these 2 functions, I go back into the for loop and make the <code>Response</code> 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>
221
222
<p>I've tried this method many times. Below are some examples of this method in action!</p>
223
224
<ul>
225
<li><a href="https://replit.com/@StevesGreatness/FizzBuzzKotlin">Kotlin</a></li>
226
<li><a href="https://replit.com/@StevesGreatness/FizzBuzzlua">Lua</a></li>
227
<li><a href="https://replit.com/@StevesGreatness/FizzBuzzpython">Python</a></li>
228
<li><a href="https://replit.com/@StevesGreatness/FizzBuzzRuby">Ruby</a></li>
229
</ul>
230
231
</article>
232
</main>
233
<footer>
234
<div class="footer-link-list-holder" role="group">
235
<span aria-hidden="true" id="footer-label-site-details" class="footer-link-list-label">Site Meta</span>
236
<ol class="footer-link-list" aria-labelledby="footer-label-site-details">
237
<li><a href="/list/website-sources-mirrors.html">Source Code and Mirrors</a></li>
238
<li><a href="https://steve0greatness.github.io/extras">Extras</a></li>
239
</ol>
240
</div>
241
<div class="footer-link-list-holder" role="group">
242
<span aria-hidden="true" id="footer-label-social-accounts" class="footer-link-list-label">Social Accounts</span>
243
<ol class="footer-link-list" aria-labelledby="footer-label-social-accounts">
244
<li><a href="https://mastodon.social/@S0G" rel="me">Mastodon</a></li>
245
<li><a href="https://youtube.com/@s0g">YouTube</a></li>
246
<li><a href="/list/link-tree.html">More...</a></li>
247
</ol>
248
</div>
249
</footer>
250
</body>
251
252
</html>