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
#1
Hi everyone,

I am struggling with solving below task which I received as a home assignment in my Python course to practice lists (I've been learning Python loosely for a few months).

Task description (translated from Hungarian):

"On the occasion of the Turkish Sultan's birthday, he sent his first servant to the prison to open the doors of all 100 cells. The Sultan had his second servant close every second door after him. With the third servant, the Sultan made him open every third door in case it was closed, and closed it if it was found open. The fourth slave changed the state of every fourth door, and so on, up to the 100th slave. Which cell doors were left open at the end?"

This is where I got so far:

Basically I was trying to use for loops to close/open every 2nd/3rd/4th doors in a sequence. But this only gets me until the fourth slave while the task requires 100 slaves and also it doesn't quite seem correct up to that point either. So I'm out of my depth at this point. Undecided
Could anyone help me with finding the solution? I guess it needs some other approach...

("t" is list)

t=[1]*100
for i in range(0,len(t),2):
    t[i]=0
for i in range(0,len(t),3):
    if t[i]==1:
        t[i]=0
    else:
        t[i]=1
for i in range(0,len(t),4):
    if t[i]==1:
        t[i]=0
    else:
        t[i]=1
print(t)

for i in range(len(t)):
    if t[i]==1:
        print(f"{i+1}. cell: open")
    else:
        print(f"{i+1}. cell: closed")
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 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,843 Dec-06-2021, 07:15 PM
Last Post: BashBedlam
  simple task with lists... Maxwell123 3 2,422 Jun-27-2020, 01:00 PM
Last Post: GOTO10
  I need help to solve this task using while statement rico4pepe 6 9,316 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