File tree 3 files changed +86
-1
lines changed
3 files changed +86
-1
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments