Skip to content

Instantly share code, notes, and snippets.

@marcolussetti
Created June 27, 2019 01:17
Show Gist options
  • Select an option

  • Save marcolussetti/6ded92351bbbba7edd3f85fe72d238ba to your computer and use it in GitHub Desktop.

Select an option

Save marcolussetti/6ded92351bbbba7edd3f85fe72d238ba to your computer and use it in GitHub Desktop.
An extensible fizz buzz example using Python's OrderedDict as lookup tables
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
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