id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com cartalk_puzzlers > hall_of_lights
prev  |  next  |  chance

This is Car Talk Puzzler #201144, Hall of Lights: RAY: This puzzler is from my "ceiling light" series. Imagine, if you will, that you have a long, long corridor that stretches out as far as the eye can see. In that corridor, attached to the ceiling are lights that are operated with a pull cord. There are gazillions of them, as far as the eye can see. Let's say there are 20,000 lights in a row. They're all off. Somebody comes along and pulls on each of the chains, turning on each one of the lights. Another person comes right behind, and pulls the chain on every second light. TOM: Thereby turning off lights 2, 4, 6, 8 and so on. RAY: Right. Now, a third person comes along and pulls the cord on every third light. That is, lights number 3, 6, 9, 12, 15, etcetera. Another person comes along and pulls the cord on lights number 4, 8, 12, 16 and so on. Of course, each person is turning on some lights and turning other lights off. If there are 20,000 lights, at some point someone is going to come skipping along and pull every 20,000th chain. When that happens, some lights will be on, and some will be off. Can you predict which lights will be on? Write hall_of_lights(n), which, when passed a number of lights, n, (all initially off), returns the list of the light numbers that will be on when all the switching is done, by n people. The result should be a list of integers, in increasing order, but whereas Python uses index 0 for the first light, this puzzle calls the first light number 1, so that's what you should do.


hall_of_lights(10) → [1, 4, 9]
hall_of_lights(5) → [1, 4]
hall_of_lights(8) → [1, 4]

...Save, Compile, Run (ctrl-enter)

def hall_of_lights(n):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 322 Post-solution available

Copyright Nick Parlante 2017 - privacy