In this example, we will use the Python for loop with the range function to show the drone’s path by lighting up lamps on the path of the drone.
You will be given two strings: lamps and drone. lamps represents a row of lamps, currently off, each represented by x. When these lamps are on, they should be represented by o.
The drone string represents the position of the drone T and its flight path up until this point =. The drone always flies left to right, and always begins at the start of the row of lamps. Anywhere the drone has flown, including its current position, will result in the lamp at that position switching on.
We will create the below function to return the resulting lamps string.
def fly_by(lamps, drone): lamps = list(lamps) for i in range(0, len(drone)): if (i < len(lamps)): lamps[i] = 'o' else: break return "".join(lamps)
As an example, if you enter these strings into the above function you will get the following outcome.
fly_by('xxxxxx', '====T') # return 'ooooox'
As you can see we are using the string length of the drone in the for loop to change the state of the lamp.
Hello, please only leave the comment which is related to the above topic, I will approve all comments that are related to the above topic but the comment must comes with a python solution.