Skip to content

Instantly share code, notes, and snippets.

@Syakyr
Last active March 6, 2018 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Syakyr/b24ed04dc326a2a9f0c0c085eb38a5bb to your computer and use it in GitHub Desktop.
Save Syakyr/b24ed04dc326a2a9f0c0c085eb38a5bb to your computer and use it in GitHub Desktop.
FIzzbuzz with Dynamic Inputs
# Creates a functions that inputs the number of arrays,
# and a dict of number to string pairs.
#
# This is my attempt to pythonise the fizzbuzz algorithm
# such that it is dynamic, and you can put as many terms
# as you like with regular intervals.
# The default settings work such that the output would
# be a dict which the keys are initialised from 1 to 100,
# and the values would have 'fizz' in keys that have
# multiples of 3, and 'buzz' in keys that have multiples
# of 5.
def fizzbuzz(array_no=100, fizzbuzz_dict={3: 'fizz', 5: 'buzz'}):
# Creates a list from 1 to array_no
no_array = list(range(1, array_no+1))
# Creates a lambda formula that creates a list with multiples
formula = lambda a, dict_a: (([''] * (a-1) + [dict_a]) * \
((array_no + a)//a))[:array_no]
# Maps the fizzbuzz_dict to the formula function
fizzbuzz_dict2 = map(lambda x: formula(x, fizzbuzz_dict[x]), fizzbuzz_dict)
# Joins the fizzbuzz to a single list
fizzbuzz_dict3 = list(map(lambda x: ''.join(x), zip(*fizzbuzz_dict2)))
# Returns a dict with the number list and the fizzbuzz results
return dict(zip(no_array, fizzbuzz_dict3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment