-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.py
56 lines (41 loc) · 1.07 KB
/
fizzbuzz.py
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
"""
FizzBuzz module.
"""
import typing
class _Number:
def __init__(self, integer: int):
if integer < 0:
raise ValueError('Argument must be a positive integer')
self.integer = integer
def is_fizz(self):
"""
Whether this is a 'fizz' number.
"""
return self.integer % 3 == 0
def is_buzz(self):
"""
Whether this is a 'buzz' number.
"""
return self.integer % 5 == 0
def is_fizzbuzz(self):
"""
Whether this is a 'fizzbuzz' number.
"""
return self.is_fizz() and self.is_buzz()
def __repr__(self) -> str:
result = ''
if self.is_fizz():
result += 'fizz'
if self.is_buzz():
result += 'buzz'
if not result:
result = str(self.integer)
return result
def generate(till: int) -> typing.List[str]:
"""
Returns a list of FizzBuzz numbers from 1 to limit.
"""
result = []
for i in range(1, till + 1):
result.append(str(_Number(i)))
return result