2012-11-20 232 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

相關問題