Created
June 27, 2019 01:17
-
-
Save marcolussetti/6ded92351bbbba7edd3f85fe72d238ba to your computer and use it in GitHub Desktop.
An extensible fizz buzz example using Python's OrderedDict as lookup tables
This file contains hidden or 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
| from collections import OrderedDict | |
| def fizzdizz(start, end, lookup_table): | |
| # Map integers in given range to the lookup values | |
| results = OrderedDict({i: [v for k, v in lookup_table.items() if i % k == 0] for i in range(start, end)}) | |
| # Join in string and fall back to key if no successful lookups | |
| results = [("".join(v) if len(v) > 0 else str(k)) for k, v in results.items()] | |
| print("\n".join(results)) | |
| # Lookup table, order matters for final printed results (e.g. "fizzbuzz" vs "buzzfizz") | |
| lookup = OrderedDict([(3, "fizz"), (5, "buzz")]) | |
| fizzdizz(1, 100 + 1, lookup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment