Skip to content

Commit 882a858

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 412
1 parent e28740d commit 882a858

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
- [383 Ransom Note](https://leetcode.com/problems/ransom-note/description/)
225225
- [392 Is Subsequence](https://leetcode.com/problems/is-subsequence/description/)
226226
- [399 Evaluate Division](https://leetcode.com/problems/evaluate-division/description/)
227+
- [412 Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/)
227228
- [427 Construct Quad Tree](https://leetcode.com/problems/construct-quad-tree/description/)
228229
- [433 Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/description/)
229230
- [452 Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def fizzBuzz(self, n: int) -> List[str]:
8+
"""
9+
Given an integer n, return a string array answer (1-indexed) where:
10+
- answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
11+
- answer[i] == "Fizz" if i is divisible by 3.
12+
- answer[i] == "Buzz" if i is divisible by 5.
13+
- answer[i] == i (as a string) if none of the above conditions are true.
14+
"""
15+
res = []
16+
for i in range(1, n + 1):
17+
if i % 3 == 0 and i % 5 == 0:
18+
res.append("FizzBuzz")
19+
elif i % 3 == 0:
20+
res.append("Fizz")
21+
elif i % 5 == 0:
22+
res.append("Buzz")
23+
else:
24+
res.append(str(i))
25+
return res

tests/test_412_fizz_buzz.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._412_fizz_buzz import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["n", "expected"],
10+
argvalues=[
11+
(3, ["1", "2", "Fizz"]),
12+
(5, ["1", "2", "Fizz", "4", "Buzz"]),
13+
(
14+
15,
15+
[
16+
"1",
17+
"2",
18+
"Fizz",
19+
"4",
20+
"Buzz",
21+
"Fizz",
22+
"7",
23+
"8",
24+
"Fizz",
25+
"Buzz",
26+
"11",
27+
"Fizz",
28+
"13",
29+
"14",
30+
"FizzBuzz",
31+
],
32+
),
33+
],
34+
)
35+
def test_func(n: int, expected: List[str]):
36+
"""Tests the solution of a LeetCode problem."""
37+
fizzBuzz = Solution().fizzBuzz(n)
38+
assert fizzBuzz == expected

0 commit comments

Comments
 (0)