Last active
January 4, 2017 12:12
Revisions
-
Nathaniel Sun renamed this gist
Jan 4, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Nathaniel Sun revised this gist
Nov 1, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # The Light Bulb Problem There are 150 light bulbs each with a flip switch, indexed from 1 to 150. Initially, they are all on. First, you look at each of the light bulbs sequentially, and flip the switch if its index number is divisible by 3. -
Nathaniel Sun revised this gist
Nov 1, 2016 . No changes.There are no files selected for viewing
-
Nathaniel Sun renamed this gist
Nov 1, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Nathaniel Sun created this gist
Nov 1, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ There are 150 light bulbs each with a flip switch, indexed from 1 to 150. Initially, they are all on. First, you look at each of the light bulbs sequentially, and flip the switch if its index number is divisible by 3. Then, you iterate through all the light bulbs again, but this time you only flip the switch if the index number is divisible by 5. How many light bulbs are on now? 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ { "off": 60, "on": 90, "size": 150 } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ import json class LightBulb(object): on = None def __init__(self, on=False): self.on = on def turn_on(self): self.on = True def turn_off(self): self.on = False def flip_switch(self): self.on = not self.on class Bus(object): light_bulbs = [] def __init__(self, size): for i in range(size): self.light_bulbs.append(LightBulb(on=True)) def analyze(self): on_count = 0 off_count = 0 for lb in self.light_bulbs: if lb.on: on_count += 1 else: off_count += 1 return { 'size': len(self.light_bulbs), 'on': on_count, 'off': off_count } def flip_if_index_is_a_multiple_of(self, divisor): for index, light_bulb in enumerate(self.light_bulbs): human_readable_index = index + 1 if human_readable_index % divisor == 0: light_bulb.flip_switch() def main(): bus = Bus(size=150) bus.flip_if_index_is_a_multiple_of(divisor=3) bus.flip_if_index_is_a_multiple_of(divisor=5) analysis = bus.analyze() with open('result.json', 'w+') as file: file.write(json.dumps(analysis, indent=4, sort_keys=True)) if __name__ == '__main__': main()