forked from UWPCE-PythonCert/IntroToPython-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfizz_buzz.py
More file actions
executable file
·77 lines (60 loc) · 1.5 KB
/
fizz_buzz.py
File metadata and controls
executable file
·77 lines (60 loc) · 1.5 KB
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
#!/usr/bin/env python
"""
Fizz Buzz examples -- from most straightforward, to most compact.
"""
# basic approach:
def fizzbuzz1(n):
for i in range(1, n+1):
if i%3 == 0 and i%5 == 0:
print "FizzBuzz"
elif i%3 == 0:
print "Fizz"
elif i%5 == 0:
print "Buzz"
else:
print i
def fizzbuzz2(n):
"""
Why evaluate i%3 and i%5 twice?
"""
for i in range(1, n+1):
msg = ''
if i%3 == 0:
msg += "Fizz"
if i%5 == 0:
msg += "Buzz"
if msg:
print msg
else:
print i
def fizzbuzz3(n):
"""
use conditional expressions:
"""
for i in range(1, n+1):
msg = "Fizz" if i%3 == 0 else ''
msg += "Buzz" if i%5 == 0 else ''
print msg or i
def fizzbuzz4(n):
"""
the one liner
"""
for i in range(1,n+1): print ( "Fizz" * (not (i%3)) + "Buzz" * (not (i%5)) ) or i
def fizzbuzz_ruby(n):
"""
This is a one-liner version inspired by the Ruby one-liner
found here:
http://www.commandercoriander.net/blog/2013/02/03/fizzbuzz-in-one-line
This uses list comprehensions, and slicing, and is, well, pretty darn ugly!
"""
for word in [ ("".join(["Fizz",][0:1-i%3]+["Buzz",][0:1-i%5]) or `i`) for i in range(1, n+1)]: print word
if __name__ == "__main__":
fizzbuzz1(16)
print
fizzbuzz2(16)
print
fizzbuzz3(16)
print
fizzbuzz4(16)
print
fizzbuzz_ruby(16)