-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz.html
173 lines (127 loc) · 5.42 KB
/
fizzbuzz.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
<p><!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="markdown.css" type="text/css" rel="stylesheet"></link>
<link href="prettify.css" type="text/css" rel="stylesheet"></link>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/google-code-prettify/prettify.js"></script>
<script type="text/javascript" src="js/myscripts.js"></script>
<title>Thousand Note - FizzBuzz</title>
</head></p>
<p><body onload="styleCode()"></p>
<p><a href="index.html">Thousandnote</a></p>
<h1>FizzBuzz</h1>
<p>Problem:</p>
<blockquote>
<p>Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".</p>
</blockquote>
<h2>Your solution</h2>
<p>In Java,</p>
<pre><code>public static void runthenumbers() throws IOException{
String divisable;
boolean mod;
for(int x=1; x<=100;x++)
{
divisable="";
mod=false;
if (x%3==0){
divisable="Fizz";
mod=true;
}
if(x%5==0){
divisable=divisable + "Buzz";
mod=true;
}
if(mod==true){
out.println(divisable);
}
else if (mod==false){
out.println(x);
}
}
</code></pre>
<p>Good job! Your code works well. It gets the job done.</p>
<h2>My feedback</h2>
<p>I know that you came up with the most straightforward solution, and
it isn't the most elegant, but that's ok. I took the liberty to simplify your code a little bit,</p>
<pre><code>public static void runthenumbers() throws IOException{
String txt;
for(int x=1; x<=100;x++)
{
txt="";
if (x%3==0){
txt="Fizz";
}
if(x%5==0){
txt=txt + "Buzz";
}
if(x%3!=0 && x%5!=0){
txt=Integer.toString(x);
}
out.println(txt);
}
}
</code></pre>
<p>There is no real need for you to have a separate boolean to determine if you are going to print out the string <code>divisible</code> or the integer <code>i</code> in the for loop. You can handle that with a single conditional: <code>if(x%3!=0 && x%5!=0)</code>.</p>
<h2>A Python Solution</h2>
<p>Eventually, I'd like you to start programming in Python, so I'll also provide the solution in Python so you can begin to pick up the language,</p>
<pre><code>"""
Fizzbuzz.py
Write a program that prints the numbers from 1 to 100. But for multiples of three
print 'Fizz' instead of the number and for the multiples of five print 'Buzz'.
For numbers which are multiples of both three and five print 'FizzBuzz'.
"""
def fizzbuzz():
# Inclusion-exclusion principle:
# |A union B| = |A| + |B| - |A intersect B|
out = []
for i in range(1,16):
tmp = ''
if i % 3 == 0:
tmp += 'Fizz'
if i % 5 == 0:
tmp += 'Buzz'
if i % 3 != 0 and i % 5 != 0:
tmp = i
out.append(tmp)
return out
def test():
"""
Make sure we get the output we intend.
"""
wanted = [1,2,'Fizz',4,'Buzz','Fizz',7,8,'Fizz','Buzz',11,'Fizz',13,14,'FizzBuzz']
got = fizzbuzz()
assert wanted == got
return "True"
if __name__ == '__main__':
print __doc__
print "Test successful:", test()
</code></pre>
<p>Here I've only solved the problem up to <code>15</code> but it holds generally up to <code>100</code> and beyond.</p>
<p>This is a general framework for you to follow in creating Python programs with a <code>__main__</code>. It isn't necessary to include <code>__main__</code> in Python, but because you are coming from Java it might be a little more familiar to you. I will explain more in the coming weeks.</p>
<p>Obligatory <a href="http://xkcd.com/353/">XKCD</a>: <img src="http://imgs.xkcd.com/comics/python.png" alt="XKCD Python" title="XKCD Python" /></p>
<h2>Some fun with math</h2>
<p>I studied Math-Computer Science in undergrad, and so I like to play with the math behind the code. The comment in my Python solution, about the <a href="http://en.wikipedia.org/wiki/Inclusion-exclusion_principle">Inclusion-exclusion principle</a>,</p>
<pre><code>|A union B| = |A| + |B| - |A intersect B|
</code></pre>
<p>is just a reminder to me of how to understand the <a href="http://en.wikipedia.org/wiki/Set_(mathematics)">sets</a> involved. The notation <code>|A|</code> means <code>the number of elements in the set A</code>. So in our case, we can calculate the # of Fizzes, Buzzes, and Fizzbuzzes as follows,</p>
<pre><code>Let A be the set of multiples of 3
Let B be the set of multiples of 5
</code></pre>
<p>Then,</p>
<pre><code>(A intersect B) is the set of multiples divisible by 3 and 5
</code></pre>
<p>And,</p>
<pre><code>(A union B) is the set of multiples divisible by either 3 or 5
</code></pre>
<p>So,</p>
<pre><code>|A| = Floor(100 / 3) = 33
|B| = Floor(100 / 5) = 20
|A intersect B| = Floor(100 / 15) = 6
</code></pre>
<p>The key here is to observe that numbers divisible by both <code>3</code> <em>and</em> <code>5</code> are divisible by their least common multiple <code>15</code>. And so, we can conclude,</p>
<pre><code>|A union B| = 33 + 20 - 6 = 47
</code></pre>
<p>This may prove useful in understanding, if not solving, the next problem I will assign.</p>
<p></body>
</html></p>