走板蟒的代码

时间:2013-11-15 17:11:17

标签: python

你有没有得到这个代码来处理我迄今为止失败的板条以获得doctest来处理我的代码

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

1 个答案:

答案 0 :(得分:0)

这应该有效:

def survivor(names, step):
    step -= 1 
    Next = step
    while len(names) > 1:
        names.pop(Next) 
        Next = (Next + step) % len(names)
    return names[0]