6

我被赋予了创建代码的任务。任务如下:

你是一艘帆船的船长,你和你的船员被海盗抓获。海盗船长让你们所有人在他的船甲板上围成一圈,试图决定你应该按照什么顺序走木板。最终他决定采用以下方法:

(a) 海盗船长让你选一个数字 N。

(b) 第一个走木板的人将是第 N 个人(从你开始)。

(c) 船长将继续绕圈,迫使每第 N 个人走木板。

(d) 一旦只剩下一个人,那个人就会得到自由。

例如:工作人员包括:Andrew、Brenda、Craig、Deidre、Edward、Felicity、Greg 和 Harriet。安德鲁选择 N=2。工作人员将按照以下顺序走木板:布伦达、迪德、费利西蒂、哈丽特、克雷格、格雷格、爱德华。安德鲁将获得自由。

我到目前为止的代码是:

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 开始计数并删除下一个 3rd 元素,即 felicity 等等,直到剩下一个人。

我怎样才能做到这一点?

4

2 回答 2

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'
于 2012-11-20T19:42:57.293 回答
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')
>>> 
于 2012-11-20T21:22:06.897 回答