Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created December 27, 2021 15:27
Show Gist options
  • Select an option

  • Save les-peters/9d9714286e6e62c2a3187bd2400d9096 to your computer and use it in GitHub Desktop.

Select an option

Save les-peters/9d9714286e6e62c2a3187bd2400d9096 to your computer and use it in GitHub Desktop.
Fireworks
question = """
You're in charge of the fireworks display, and you have a list of fireworks
to shoot off. You want to make sure you don't fire the same colors twice in a row.
Given an array of fireworks, return a valid firing order. You decide how you want an
impossible solution to work!
Example:
$ orderFireworks(['green','green','green','red','red','blue'])
$ ['green','red','green','red','green','blue']
"""
from functools import reduce
def orderFireworks(color_array):
fire_order = []
color_hash = {}
for color in color_array:
if color not in color_hash:
color_hash[color] = 0
color_hash[color] += 1
print(color_hash)
odd_colors = list(filter(lambda x: (color_hash[x] % 2 == 1), color_hash.keys()))
for color in odd_colors:
fire_order.append(color)
color_hash[color] -= 1
if color_hash[color] == 0:
del color_hash[color]
sum = 0
for color in color_hash:
sum += color_hash[color]
while sum > 0:
for color in color_hash:
if color_hash[color] > 0:
fire_order.append(color)
color_hash[color] -= 1
sum -= 1
return fire_order
print(orderFireworks(['green','green','green','red','red','blue']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment