Python Forum
Stuck with using lists to solve task
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stuck with using lists to solve task
#3
Hi and welcome.

No doubt there are a few different ways this could be done, such as using 0 and 1 for the status of the cell doors. Then, you could use if door: which would return True or False for 1 and 0. That way, you don't need to code if door == 0:, but I would go with a more literal approach and you may not have been introduced to constructing custom functions yet.

I would use the step parameter of the range function. Have you learned about that?

As an example, this will close every other door, as the second servant has been asked to do.
door_open = "open" # door is open
door_closed = "closed" # door is closed

cell_doors = [door_open]*100 # open all the doors

for door in range(1, 100, 2): # close every second door
    if cell_doors[door] == door_open:
        cell_doors[door] = door_closed
The above is where I would start from: it sets all the doors to 'open' and then closes every second door. Technically, you don't need the if: branch at this stage, because we know the status from the get-go, but I've put it in, for completeness.

Caveat: this potential solution is simply my first thoughts and I'll have a think some more about if it's going to be practical or not.

To add: I think this could work, if you set up servants = range(1, 101) then nest for servant in servants: with what I have above, using servant as the 'step' parameter, and simply alternate each door (if it's closed, open; if it's open, close it). For that to work, start with all of the doors closed, not open, and 0 as the first door, not 1.

I can't be more specific, without breaking the Forum rules; I'm close to doing that, as it is.
gery576 likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Messages In This Thread
Stuck with using lists to solve task - by gery576 - Jan-15-2024, 11:03 AM
RE: Stuck with using lists to solve task - by rob101 - Jan-15-2024, 01:48 PM
RE: Stuck with using lists to solve task - by DPaul - Jan-16-2024, 06:57 AM
RE: Stuck with using lists to solve task - by DPaul - Jan-16-2024, 08:44 AM
RE: Stuck with using lists to solve task - by DPaul - Jan-16-2024, 06:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to solve this task? DERO 2 1,903 Dec-06-2021, 07:15 PM
Last Post: BashBedlam
  simple task with lists... Maxwell123 3 2,483 Jun-27-2020, 01:00 PM
Last Post: GOTO10
  I need help to solve this task using while statement rico4pepe 6 9,429 Apr-02-2020, 11:34 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020