forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfizz_buzz.py
More file actions
36 lines (30 loc) · 937 Bytes
/
fizz_buzz.py
File metadata and controls
36 lines (30 loc) · 937 Bytes
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
# Approach 1
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer = []
for i in range(1, n + 1):
divisible_by_3 = i % 3 == 0
divisible_by_5 = i % 5 == 0
ans_str = ''
if divisible_by_3:
ans_str = "Fizz"
if divisible_by_5:
ans_str += "Buzz"
if not divisible_by_3 and not divisible_by_5:
ans_str = str(i)
answer.append(ans_str)
return answer
# Approach 2
class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer = []
mapping = {3: 'Fizz', 5: 'Buzz'}
for i in range(1, n + 1):
ans_str = ''
for key in mapping:
if i % key == 0:
ans_str += mapping[key]
if not ans_str:
ans_str = str(i)
answer.append(ans_str)
return answer