-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz_advanced.py
106 lines (86 loc) · 2.84 KB
/
fizzbuzz_advanced.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# coding: utf-8
"""
#==============================#
| FizzBuzz - Fulll hiring test |
#==============================#
> Thomas Rigole
---------------
> Advanced version :
Choice of custom & scalable rules, via command-line argument parser.
If required on a larger scale, use a ruleset
JSON file to create the fizzbuzz_map dict.
"""
# Imports --------------------------------------------------------------------
import argparse
# Functions ------------------------------------------------------------------
def parse_arguments():
"""
Parse command-line arguments : upper bound 'n' of the FizzBuzz program
and optional custom rules.
Returns
-------
Namespace
Parsed arguments : upper bound n (+ ruleset)
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Custom FizzBuzz program"
)
# Positional argument for upper bound n
parser.add_argument('n', type=int, help="FizzBuzz sequence upper bound")
# Optional arguement for custom rules
parser.add_argument('--rules', type=str, nargs='+',
help="FizzBuzz custom rules as 'divisor:word' \
(e.g., 3:Fizz 5:Buzz).")
return parser.parse_args()
def format_rules(rules_string):
"""
Convert the custom rules string to a dictionary.
Parameters
----------
rules_string : str
Rule(s) passed as 'divisor:word'
Returns
-------
rules_dict : dict
"""
try:
return {int(divisor): word for divisor, word
in (rule.split(':') for rule in rules_string)}
except ValueError:
print("Invalid custom rules format. Please enter a valid ruleset:\n \
> 'divisor:word' (e.g., --rules 3:Fizz 5:Buzz)")
return False
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)
# main -----------------------------------------------------------------------
def main():
args = parse_arguments()
if args.rules:
fizzbuzz_map = format_rules(args.rules)
if not fizzbuzz_map:
return # Exit if error (custom rules format)
else:
fizzbuzz_map = {3: 'Fizz', 5: 'Buzz'}
for sequence in fizzbuzz_generator(args.n, fizzbuzz_map):
print(sequence)
if __name__ == "__main__":
main()