-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz_basic.py
55 lines (44 loc) · 1.28 KB
/
fizzbuzz_basic.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
#!/usr/bin/env python
# coding: utf-8
"""
#==============================#
| FizzBuzz - Fulll hiring test |
#==============================#
> Thomas Rigole
---------------
"""
def fizzbuzz_generator(n, fizzbuzz_map):
"""
Generates sequences of values/FizzBuzz
based on a custom ruleset: fizzbuzz_map.
Parameters
----------
n : int
Upper bound of the FizzBuzz algorithm
fizzbuzz_map : dict
Ruleset composed of divisor(s) and associated word(s)
Yields
------
str
The number itself or the FizzBuzz translation
"""
for i in range(1, n + 1):
sequence = ''.join([word for divisor, word in fizzbuzz_map.items()
if i % divisor == 0])
yield sequence or str(i)
def main():
# Input N
try:
n = int(input("Enter the upper bound N: "))
if n <= 0:
raise ValueError("The number must be positive")
except ValueError as e:
print(f"Invalid input format: {e}. Please enter a positive integer.")
return # Exit if error (invalid format)
# FizzBuzz ruleset
fizzbuzz_map = {3: 'Fizz', 5: 'Buzz'}
# Sequence generation
for sequence in fizzbuzz_generator(n, fizzbuzz_map):
print(sequence)
if __name__ == "__main__":
main()