行走plankpython的代码

2024-05-12 18:50:24 发布

您现在位置:Python中文网/ 问答频道 /正文

你有没有让代码工作,因为我走的木板,到目前为止,我没有得到一个医生测试工作的代码

def survivor(names, step):
    """
    >>> survivor([1,2,3,4,5,6,7,8],3)
    7
    >>> survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"], 3)
    'Greg'
    >>> survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"], 1)
    'Harriet'
    >>> survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"], 6)
    'Andrew'
    >>> survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"], 2)
    'Andrew'
    >>> survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"], 4)
    'Felicity'
    >>> type(survivor([1,2,3,4,5,6,7,8],3))
    <type 'int'>
    >>> type(survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"], 3))
    <type 'str'>

    """

def survivor(names, step):
    names = ["Andrew", "Brenda", "Craig", "Deidre", "Edward", "Felicity", "Greg", "Harriet"]
    next = step - 1
    names.pop(next)
    print names

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

Tags: thenamenamesdeftypeedwardandrewsurvivor