2

这是最终产品。如果其他人有任何削减它的技巧,请告诉我!非常感谢您的帮助!

def triple_cut(deck):
    ''' (list of int) -> NoneType

    Modify deck by finding the first joker and putting all the cards above it
    to the bottom of deck, and all the cards below the second joker to the top
    of deck. 
    >>> deck = [2, 7, 3, 27, 11, 23, 28, 1, 6, 9, 13, 4]
    >>> triple_cut(deck) 
    >>> deck 
    [1, 6, 9, 13, 4, 27, 11, 23, 28, 2, 7, 3]
    '''     
    joker1 = deck.index(JOKER1)
    joker2 = deck.index(JOKER2)
    first = min(joker1, joker2)
    first_cards = []

    for cards in range(len(deck[:first])):
        cards = 0 
        pop = deck.pop(cards)
        first_cards.append(pop)

    joker1 = deck.index(JOKER1)
    joker2 = deck.index(JOKER2)
    second = max(joker1, joker2)    
    second_cards = []

    for cards in deck[second + 1:]:
        pop = deck.pop(deck.index(cards))
        second_cards.append(pop)

    second_cards.reverse()

    for card in second_cards: 
        deck.insert(0, card)

    deck.extend(first_cards)   

raah 我需要输入更多,因为我的帖子主要是代码:请添加更多详细信息 sss ss

4

2 回答 2

0

对第二次修订的回答

当函数完成后,它不会改变牌组。

问题是你没有给出完整的代码。我猜它看起来像这样:

def triple_cut(deck)
    …
    deck = q

根据您的文档字符串,您将其称为

deck = […]
triple_cut(deck)

并且对作业deck = q没有从triple_cut(). 您不能修改方法的形式参数,因此赋值对triple_cut 保持本地并且影响模块级变量deck

写这个的正确方法是

def triple_cut(cuttable)
    …
    return cuttable[first:] + cuttable[:first]

deck = […]
deck = triple_cut(deck)

cuttable出于解释的目的,我将参数的名称更改为to 。您可以保留参数名称,deck但我想表明,当您认为您正在分配到甲板时,您实际上是在分配给可切割的并且该分配不会执行triple_cut()。

于 2013-10-18T22:55:07.307 回答
0

一个提示:

p = list('abcdefghijkl')
pivot = p.index('g')
q = p[pivot:] + p[:pivot]

列表切片是您的朋友。

于 2013-10-18T17:33:53.040 回答