-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-kata-fizz-buzz-game.html
273 lines (248 loc) · 21.6 KB
/
code-kata-fizz-buzz-game.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE html>
<html lang="en">
<head>
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,400italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="http://www.emadmokhtar.com/theme/stylesheet/style.min.css">
<link rel="stylesheet" type="text/css" href="http://www.emadmokhtar.com/theme/stylesheet/pygments.min.css">
<link rel="stylesheet" type="text/css" href="http://www.emadmokhtar.com/theme/stylesheet/font-awesome.min.css">
<link href="http://www.emadmokhtar.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Emad Mokhtar's Framework Atom">
<link href="http://www.emadmokhtar.com/feeds/all.rss.xml" type="application/rss+xml" rel="alternate" title="Emad Mokhtar's Framework RSS">
<link rel="shortcut icon" href="http://www.emadmokhtar.com/images/favicon.ico" type="image/x-icon">
<link rel="icon" href="http://www.emadmokhtar.com/images/favicon.ico" type="image/x-icon">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="" />
<meta name="author" content="EmadMokhtar" />
<meta name="description" content="I stared to practice recently after reading more about how practicing is important for programmer to become professional and one way of practicing is Coding Kata. I found a lot of problem to implement to practice and perform coding kata. This post I'll talk about Fizz Buzz Game a.k.a. Bizz Buzz Game, it's numerical/math game, if the number is divisible by 3 replace it with fizz, or if number divisible by 5 replace it with buzz, or if number is divisible by 3 and 5 replace it with fizzbuzz. For example if we count from 1 to 20 it'll be like this: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz. So how to code this game: First I thought I can solve this game by: If (this number is divisible by 3 and not divisible by 5) return fizz else if (this number is not divisible by 3 and divisible by 5) return buzz else if (this number is divisible by 3 and divisible by 5) return buzzfizz else if(this number is not divisible by 3 and not divisible by 5) return the number …" />
<meta name="keywords" content="code, coding, game, kata, programmer, programming">
<meta property="og:site_name" content="Emad Mokhtar's Framework"/>
<meta property="og:title" content="Code Kata: Fizz Buzz Game"/>
<meta property="og:description" content="I stared to practice recently after reading more about how practicing is important for programmer to become professional and one way of practicing is Coding Kata. I found a lot of problem to implement to practice and perform coding kata. This post I'll talk about Fizz Buzz Game a.k.a. Bizz Buzz Game, it's numerical/math game, if the number is divisible by 3 replace it with fizz, or if number divisible by 5 replace it with buzz, or if number is divisible by 3 and 5 replace it with fizzbuzz. For example if we count from 1 to 20 it'll be like this: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz. So how to code this game: First I thought I can solve this game by: If (this number is divisible by 3 and not divisible by 5) return fizz else if (this number is not divisible by 3 and divisible by 5) return buzz else if (this number is divisible by 3 and divisible by 5) return buzzfizz else if(this number is not divisible by 3 and not divisible by 5) return the number …"/>
<meta property="og:locale" content="en_US"/>
<meta property="og:url" content="http://www.emadmokhtar.com/code-kata-fizz-buzz-game.html"/>
<meta property="og:type" content="article"/>
<meta property="article:published_time" content="2011-12-20 22:50:00+03:00"/>
<meta property="article:modified_time" content=""/>
<meta property="article:author" content="http://www.emadmokhtar.com/author/emadmokhtar.html">
<meta property="article:section" content="Developer"/>
<meta property="article:tag" content="code"/>
<meta property="article:tag" content="coding"/>
<meta property="article:tag" content="game"/>
<meta property="article:tag" content="kata"/>
<meta property="article:tag" content="programmer"/>
<meta property="article:tag" content="programming"/>
<meta property="og:image" content="http://www.emadmokhtar.com/images/profile.jpg">
<title>Emad Mokhtar's Framework – Code Kata: Fizz Buzz Game</title>
</head>
<body>
<aside>
<div>
<a href="http://www.emadmokhtar.com">
<img src="http://www.emadmokhtar.com/images/profile.jpg" alt="" title="">
</a>
<h1><a href="http://www.emadmokhtar.com"></a></h1>
<p>Geek developer who's in search of code perfection.</p>
<nav>
<ul class="list">
<li><a href="http://www.emadmokhtar.com">Blog</a></li>
<li><a href="http://www.emadmokhtar.com/pages/podcasts.html#podcasts">Podcasts</a></li>
<li><a href="http://www.emadmokhtar.com/pages/projects.html#projects">Projects</a></li>
<li><a href="https://emadmokhtar.github.io/resume" target="_blank">Resume</a></li>
</ul>
</nav>
<ul class="social">
<li><a class="sc-envelope-o" href="mailto:emad@emadmokhtar.com" target="_blank"><i class="fa fa-envelope-o"></i></a></li>
<li><a class="sc-github" href="https://www.github.com/EmadMokhtar" target="_blank"><i class="fa fa-github"></i></a></li>
<li><a class="sc-linkedin" href="https://www.linkedin.com/in/emadmokhtar/" target="_blank"><i class="fa fa-linkedin"></i></a></li>
<li><a class="sc-stack-overflow" href="http://stackoverflow.com/users/373051/emad-mokhtar" target="_blank"><i class="fa fa-stack-overflow"></i></a></li>
<li><a class="sc-facebook" href="https://www.facebook.com/emadmokhtarframework/" target="_blank"><i class="fa fa-facebook"></i></a></li>
<li><a class="sc-twitter" href="https://twitter.com/emadmokhtar" target="_blank"><i class="fa fa-twitter"></i></a></li>
</ul>
</div>
</aside>
<main>
<nav>
<a href="http://www.emadmokhtar.com">Home</a>
<a href="https://emadmokhtar.github.io/resume">Resume</a>
<a href="http://www.emadmokhtar.com/feeds/all.atom.xml">Atom</a>
<a href="http://www.emadmokhtar.com/feeds/all.rss.xml">RSS</a>
</nav>
<article>
<header>
<h1 id="code-kata-fizz-buzz-game">Code Kata: Fizz Buzz Game</h1>
<p>Posted on Tue 20 December 2011 in <a href="http://www.emadmokhtar.com/category/developer.html">Developer</a> <b>Read in 3 min.</b></p>
</header>
<div>
<p>I stared to practice recently after reading more about how practicing is important for programmer to become professional and one way of practicing is <a href="http://www.emadmokhtar.com/coding-kata.html">Coding Kata</a>. I found a lot of problem to implement to practice and perform coding kata.</p>
<p>This post I'll talk about Fizz Buzz Game a.k.a. Bizz Buzz Game, it's numerical/math game, if the number is divisible by 3 replace it with fizz, or if number divisible by 5 replace it with buzz, or if number is divisible by 3 and 5 replace it with fizzbuzz. For example if we count from 1 to 20 it'll be like this: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz.</p>
<p>So how to code this game:</p>
<p>First I thought I can solve this game by:</p>
<p>If (this number is divisible by 3 and not divisible by 5) return fizz</p>
<p>else if (this number is not divisible by 3 and divisible by 5) return
buzz</p>
<p>else if (this number is divisible by 3 and divisible by 5) return
buzzfizz</p>
<p>else if(this number is not divisible by 3 and not divisible by 5)
return the number</p>
<div class="highlight"><pre><span></span> <span class="c1">/// <summary></span>
<span class="c1">/// Solve fizz buzz problem</span>
<span class="c1">/// if number divisible by 3 and not by 5 return fizz</span>
<span class="c1">/// if number divisible by 5 and not by 3 return buzz</span>
<span class="c1">/// if number divisible by 15 return fizzbuzz</span>
<span class="c1">/// </summary></span>
<span class="c1">/// <param name="number">The number.</param></span>
<span class="c1">/// <returns>fizz, buzz, or fizzbuzz</returns></span>
<span class="k">static</span> <span class="k">public</span> <span class="kt">string</span> <span class="nf">FizzBuzz</span><span class="p">(</span><span class="kt">int</span> <span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">string</span> <span class="n">returnString</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">3</span> <span class="p">==</span> <span class="m">0</span> <span class="p">&&</span> <span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">!=</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"fizz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">3</span> <span class="p">!=</span> <span class="m">0</span> <span class="p">&&</span> <span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"buzz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">3</span> <span class="p">==</span> <span class="m">0</span> <span class="p">&&</span> <span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"fizzbuzz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">3</span> <span class="p">!=</span> <span class="m">0</span> <span class="p">&&</span> <span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">!=</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="n">number</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">returnString</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>But when I look at the code I feel like I can optimize it more by remove
the and between two logical conditions, as the following.</p>
<div class="highlight"><pre><span></span> <span class="c1">/// <summary></span>
<span class="c1">/// Solve fizz buzz problem</span>
<span class="c1">/// if number divisible by 3 and not by 5 return fizz</span>
<span class="c1">/// if number divisible by 5 and not by 3 return buzz</span>
<span class="c1">/// if number divisible by 15 return fizzbuzz</span>
<span class="c1">/// </summary></span>
<span class="c1">/// <param name="number">The number.</param></span>
<span class="c1">/// <returns>fizz, buzz, or fizzbuzz</returns></span>
<span class="k">static</span> <span class="k">public</span> <span class="kt">string</span> <span class="nf">FizzBuzz</span><span class="p">(</span><span class="kt">int</span> <span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">string</span> <span class="n">returnString</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">3</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"fizzbuzz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="nf">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">!=</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"fizz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"buzz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="nf">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">!=</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="n">number</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">returnString</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>After observing the results, I found that fizzbuzz only appears when the number is divisible by 15, so I rewrite my pseudocodecode</p>
<p>If (this number is divisible by 15) return buzfizz</p>
<p>else if (this number is divisible by 3) return fizz</p>
<p>else if (this number is divisible by 5) return buzz</p>
<p>else return the number</p>
<div class="highlight"><pre><span></span> <span class="c1">/// <summary></span>
<span class="c1">/// Solve fizz buzz problem</span>
<span class="c1">/// if number divisible by 3 and not by 5 return fizz</span>
<span class="c1">/// if number divisible by 5 and not by 3 return buzz</span>
<span class="c1">/// if number divisible by 15 return fizzbuzz</span>
<span class="c1">/// </summary></span>
<span class="c1">/// <param name="number">The number.</param></span>
<span class="c1">/// <returns>fizz, buzz, or fizzbuzz</returns></span>
<span class="k">static</span> <span class="k">public</span> <span class="kt">string</span> <span class="nf">FizzBuzz</span><span class="p">(</span><span class="kt">int</span> <span class="n">number</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">string</span> <span class="n">returnString</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">15</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"fizzbuzz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="nf">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">3</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"fizz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="nf">if</span> <span class="p">(</span><span class="n">number</span> <span class="p">%</span> <span class="m">5</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="s">"buzz"</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="p">{</span>
<span class="n">returnString</span> <span class="p">=</span> <span class="n">number</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">returnString</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p><img alt="output" src="http://www.emadmokhtar.com/images/fizzbuzzoutput.png"></p>
<p>This Coding Kate done, I've enjoyed it as taook me to collage day's when
having fun solving small problems, I hope you enjoyed it as well. See
you next Coding Kata.</p>
</div>
<div class="tag-cloud">
<p>
<a href="http://www.emadmokhtar.com/tag/code.html">code</a>
<a href="http://www.emadmokhtar.com/tag/coding.html">coding</a>
<a href="http://www.emadmokhtar.com/tag/game.html">game</a>
<a href="http://www.emadmokhtar.com/tag/kata.html">kata</a>
<a href="http://www.emadmokhtar.com/tag/programmer.html">programmer</a>
<a href="http://www.emadmokhtar.com/tag/programming.html">programming</a>
</p>
</div>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'emadmokhtarsframework';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
</article>
<footer>
<p>© Emad Mokhtar </p>
<p>Built using <a href="http://getpelican.com" target="_blank">Pelican</a> - <a href="https://github.com/alexandrevicenzi/flex" target="_blank">Flex</a> theme by <a href="http://alexandrevicenzi.com" target="_blank">Alexandre Vicenzi</a></p> </footer>
</main>
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-11401860-2', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BlogPosting",
"name": "Code Kata: Fizz Buzz Game",
"headline": "Code Kata: Fizz Buzz Game",
"datePublished": "2011-12-20 22:50:00+03:00",
"dateModified": "",
"author": {
"@type": "Person",
"name": "EmadMokhtar",
"url": "http://www.emadmokhtar.com/author/emadmokhtar.html"
},
"image": "http://www.emadmokhtar.com/images/profile.jpg",
"url": "http://www.emadmokhtar.com/code-kata-fizz-buzz-game.html",
"description": "I stared to practice recently after reading more about how practicing is important for programmer to become professional and one way of practicing is Coding Kata. I found a lot of problem to implement to practice and perform coding kata. This post I'll talk about Fizz Buzz Game a.k.a. Bizz Buzz Game, it's numerical/math game, if the number is divisible by 3 replace it with fizz, or if number divisible by 5 replace it with buzz, or if number is divisible by 3 and 5 replace it with fizzbuzz. For example if we count from 1 to 20 it'll be like this: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz. So how to code this game: First I thought I can solve this game by: If (this number is divisible by 3 and not divisible by 5) return fizz else if (this number is not divisible by 3 and divisible by 5) return buzz else if (this number is divisible by 3 and divisible by 5) return buzzfizz else if(this number is not divisible by 3 and not divisible by 5) return the number …"
}
</script></body>
</html>