-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
18 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Problem: FizzBuzz | ||
Write a function called fizzbuzz that takes a positive integer n as input and returns a list of strings from 1 to n. | ||
However, there are three additional rules: | ||
For numbers that are multiples of 3, the list should contain "Fizz" instead of the number. | ||
For numbers that are multiples of 5, the list should contain "Buzz" instead of the number. | ||
For numbers that are multiples of both 3 and 5, the list should contain "FizzBuzz" instead of the number. | ||
The input n will be a positive integer (n > 0). | ||
""" | ||
|
||
def fizzbuzz(n: int) -> list[str]: | ||
return ["Fizz" if x % 3 == 0 and x % 5 != 0 else "Buzz" if x % 5 == 0 and x % 3 != 0 else "FizzBuzz" if x % 3 == 0 and x % 5 == 0 else f"{x}" for x in range(1, n + 1)] | ||
|
||
print(fizzbuzz(15)) | ||
# Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"] |