2012-11-20 234 views
6

我已经被赋予创建代码的任务。任务如下:从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开始计数,并删除下一个第三个元素,这是幸福等等,直到有一个人离开。

我该怎么做?

+0

谁能帮助我????????/ – user1839493

+0

那么,谁做出选择的人总是在列表中的第一个? –

+0

我已经使用我自己的代码的第一部分是:下一步= 1,而len(名称)> 1:names.pop(Next)Next = Next + step Next =(Next - 1)%len(names)打印名称返回名称[0],可用于返回幸存者,但是当我尝试实现第二部分时,建议也好像它似乎不起作用 \t 我试图使用此:assert name in survivor中的步骤名称):如果生存者==名称:但它不起作用它表示UnboundLocalError:在分配之前引用的局部变量'步骤'返回步骤 – user1839493

回答

6

这似乎工作:

from collections import deque 
def survivor(names, step):  
    circle = deque(names) 
    while len(circle) > 1: 
     circle.rotate(1-step) 
     print circle.popleft() 
    return circle[0] 

它打印海盗的受害者的姓名,并返回幸存者的名字:

In [17]: crew = ["Andrew", "Brenda", "Craig", "Deidre", 
    ....: "Edward", "Felicity", "Greg", "Harriet"] 

In [18]: survivor(crew, 2) 
Brenda 
Deidre 
Felicity 
Harriet 
Craig 
Greg 
Edward 
Out[18]: 'Andrew' 

In [19]: survivor(crew, 3) 
Craig 
Felicity 
Andrew 
Edward 
Brenda 
Harriet 
Deidre 
Out[19]: 'Greg' 
+0

谢谢列夫,这完美地工作。你不知道我多么感激这一点,我一直坚持这个多年。谢谢 – user1839493

+0

@ user1839493如果你明白它是如何工作的,而不是把它交给老师,这只会有所帮助(对不起,如果我猜错了)。此外,如果它解决了您的问题,您可以[将答案标记为已接受](http://meta.stackexchange.com/a/5235/181223)。 –

+0

你能向我解释一下这件东西吗? – user1839493

1

下面的代码应该做的,你问的一切,包括执行safeN功能:

import collections 
import itertools 

def walk_plank(names, N): 
    "Walk everyone down the plank." 
    circle = collections.deque(names) 
    while circle: 
     circle.rotate(-N) 
     yield circle.pop() 

def save_last(names, N): 
    "Save the last person from walking the plank." 
    for name in walk_plank(names, N): 
     pass 
    return name 

def safeN(names, name): 
    "Find the best N to save someone from walking the plank." 
    assert name in names, 'Name must be in names!' 
    for N in itertools.count(1): 
     if save_last(names, N) == name: 
      return N 

编辑:以上是在Windows中使用IDLE时上述代码的一些示例用法。

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> import collections, itertools 
>>> def walk_plank(names, N): 
     "Walk everyone down the plank." 
     circle = collections.deque(names) 
     while circle: 
      circle.rotate(-N) 
      yield circle.pop() 

>>> def save_last(names, N): 
     "Save the last person from walking the plank." 
     for name in walk_plank(names, N): 
      pass 
     return name 

>>> def safeN(names, name): 
     "Find the best N to save someone from walking the plank." 
     assert name in names, 'Name must be in names!' 
     for N in itertools.count(1): 
      if save_last(names, N) == name: 
       return N 

>>> names = 'Andrew Brenda Craig Deidre Edward Felicity Greg Harriet'.split() 
>>> tuple(walk_plank(names, 2)) 
('Brenda', 'Deidre', 'Felicity', 'Harriet', 'Craig', 'Greg', 'Edward', 'Andrew') 
>>> save_last(names, 2) 
'Andrew' 
>>> safeN(names, 'Andrew') 
2 
>>> safeN(names, 'Brenda') 
19 
>>> save_last(names, 19) 
'Brenda' 
>>> tuple(walk_plank(names, 19)) 
('Craig', 'Harriet', 'Andrew', 'Felicity', 'Deidre', 'Edward', 'Greg', 'Brenda') 
>>> 
+0

感谢您的帮助Noctis Skytower,我现在可以执行第二部分的任务 – user1839493

+0

我已经使用我自己的代码,第一部分是: Next = step - 1 while len(names)> 1: names .pop(下一页) 接着=下一个步骤+接着 =(下一个 - 1)%LEN(地名) 打印名 返回名称[0] ,致力于返回幸存者但是当我尝试实现第二部分建议以及它似乎并没有工作 – user1839493

+0

我试图用这个: 断言姓名 步骤中的幸存者(姓名,步骤): 如果幸存者==名称: 但它不起作用它说UnboundLocalError:分配之前引用的本地变量'step' 返回步骤 – user1839493

相关问题