Skip to content

Instantly share code, notes, and snippets.

@mattseymour
Created November 22, 2014 13:13
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 mattseymour/25d86505ff46729de5cf to your computer and use it in GitHub Desktop.
Save mattseymour/25d86505ff46729de5cf to your computer and use it in GitHub Desktop.
Python - 100 lightbulbs problem
"""
This is a simple bit of code which solves the 100 lightbulbs problem.
Its not the most efficient but it does the job.
"""
def switch(list_length, iterations):
switches_on = []
switches = { i:False for i in range(1,list_length+1)}
for i in range(1, iterations+1):
for j in range(1, list_length+1):
if j % i == 0:
switches[j] = False if switches[j] else True
if i == list_length:
if switches[j]:
switches_on.append(j)
return switches_on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment