Skip to content

Commit 70057fa

Browse files
authored
New exercise for control flow: "FizzBuzz" (#8)
* feat(06): add an exercise, "FizzBuzz" * docs(06): use better keywords for background
1 parent b697f6c commit 70057fa

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

06-controlflow/fizzbuzz-answer.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def fizzbuzz(n):
2+
"""
3+
Print numbers from 1 to `n`.
4+
For multiples of 3, print "fizz" instead of the number.
5+
For multiples of 5, print "buzz" instead of the number.
6+
For multiples of both 3 and 5, print "fizzbuzz" instead of the number.
7+
8+
>>> fizzbuzz(3)
9+
1
10+
2
11+
fizz
12+
>>> fizzbuzz(15)
13+
1
14+
2
15+
fizz
16+
4
17+
buzz
18+
fizz
19+
7
20+
8
21+
fizz
22+
buzz
23+
11
24+
fizz
25+
13
26+
14
27+
fizzbuzz
28+
"""
29+
for x in range(1, n + 1):
30+
if x % 15 == 0:
31+
print("fizzbuzz")
32+
elif x % 3 == 0:
33+
print("fizz")
34+
elif x % 5 == 0:
35+
print("buzz")
36+
else:
37+
print(x)
38+
39+
40+
if __name__ == "__main__":
41+
import doctest
42+
43+
doctest.testmod(verbose=True)

06-controlflow/fizzbuzz.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def fizzbuzz(n):
2+
"""
3+
Print numbers from 1 to `n`.
4+
For multiples of 3, print "fizz" instead of the number.
5+
For multiples of 5, print "buzz" instead of the number.
6+
For multiples of both 3 and 5, print "fizzbuzz" instead of the number.
7+
8+
>>> fizzbuzz(3)
9+
1
10+
2
11+
fizz
12+
>>> fizzbuzz(15)
13+
1
14+
2
15+
fizz
16+
4
17+
buzz
18+
fizz
19+
7
20+
8
21+
fizz
22+
buzz
23+
11
24+
fizz
25+
13
26+
14
27+
fizzbuzz
28+
"""
29+
30+
31+
if __name__ == "__main__":
32+
import doctest
33+
34+
doctest.testmod(verbose=True)

06-controlflow/guide.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# control flow
1+
# control flow
2+
3+
## FizzBuzz
4+
5+
- difficulty: ★☆☆
6+
- time: ~ 15m for beginners
7+
- background: conditional, iteration
8+
9+
Solve fizzbuzz in classical way.

0 commit comments

Comments
 (0)