我已经被赋予创建代码的任务。任务如下:从python 2.7的列表中删除每个第n个元素
You are the captain of a sailing vessel and you and your crew have been captured by pirates. The pirate captain has all of you standing in a circle on the deck of his ship trying to decide in which order you should walk the plank. Eventually he decides on the following method:
(a) The pirate captain asks you to pick a number N.
(b) The first person to walk the plank will be the Nth person (starting from you).
(c) The captain will then continue around the circle forcing every Nth person to walk the plank.
(d) Once there is only one person left, that person will be given freedom.
For example: The crew consists of: Andrew, Brenda, Craig, Deidre, Edward, Felicity, Greg and Harriet. Andrew selects N=2. The crew will walk the plank in the order: Brenda, Deidre, Felicity, Harriet, Craig, Greg, Edward. Andrew will be given freedom.
代码中,我至今是:
def survivor(names, step):
names = ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]
Next = step - 1
names.pop(Next)
print names
这将从列表中删除第n次的人,但我不知道如何通过列表循环继续移除第n个人。
我需要它,所以让我们假设step = 3,然后我需要它删除craig,然后从craig开始计数,并删除下一个第三个元素,这是幸福等等,直到有一个人离开。
我该怎么做?
谁能帮助我????????/ – user1839493
那么,谁做出选择的人总是在列表中的第一个? –
我已经使用我自己的代码的第一部分是:下一步= 1,而len(名称)> 1:names.pop(Next)Next = Next + step Next =(Next - 1)%len(names)打印名称返回名称[0],可用于返回幸存者,但是当我尝试实现第二部分时,建议也好像它似乎不起作用 \t 我试图使用此:assert name in survivor中的步骤名称):如果生存者==名称:但它不起作用它表示UnboundLocalError:在分配之前引用的局部变量'步骤'返回步骤 – user1839493