-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0412-fizz-buzz.py
More file actions
66 lines (59 loc) · 2.07 KB
/
Copy path0412-fizz-buzz.py
File metadata and controls
66 lines (59 loc) · 2.07 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
from typing import List
import unittest
# https://leetcode.com/problems/fizz-buzz/
# python3 -m unittest strings/0412-fizz-buzz.py
class Solution(unittest.TestCase):
# # Approach #1
# # Time: O(N)
# # Space: O(1)
# def fizzBuzz(self, n: int) -> List[str]:
# # return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n+1)]
# answer = [""] * n
# for idx in range(1, n+1):
# if idx%3 == 0:
# answer[idx-1] = "Fizz"
# if idx%5 == 0:
# answer[idx-1] += "Buzz"
# if not answer[idx-1]:
# answer[idx-1] = str(idx)
# return answer
# # Approach #2
# # Time: O(N)
# # Space: O(1)
# def fizzBuzz(self, n: int) -> List[str]:
# source = ["", "Fizz", "Buzz", "FizzBuzz"]
# answer = [""] * n
# for idx in range(1, n+1):
# sid = int(idx%3 == 0) + int(idx%5 == 0) * 2
# if sid == 0:
# answer[idx-1] = str(idx)
# else:
# answer[idx-1] = source[sid]
# return answer
# Approach #3. the use of a modulus operator may have "some" impact on time complexity
# Time: O(N)
# Space: O(1)
def fizzBuzz(self, n: int) -> List[str]:
fizz, buzz = 3, 5
answer = [""] * n
for idx in range(1, n + 1):
if idx == fizz:
answer[idx - 1] = "Fizz"
fizz += 3
if idx == buzz:
answer[idx - 1] += "Buzz"
buzz += 5
if not answer[idx - 1]:
answer[idx - 1] = str(idx)
return answer
def test(self):
for n, expected in [
(3, ["1", "2", "Fizz"]),
(5, ["1", "2", "Fizz", "4", "Buzz"]),
(
15,
["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"],
),
]:
output = self.fizzBuzz(n)
self.assertEqual(expected, output, f"expected: {expected}, output: {output}")