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.
Could anyone help me with finding the solution? I guess it needs some other approach...
("t" is list)
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.
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")