恢复牌序

给定一堆牌,如 ABCDEF,将第一张放到桌上,第二张放到牌堆最后,反复操作后,得到桌上的牌序为 ACEBFD,求恢复原来的牌序。

目前没想到别的解法,逆向操作:

lista = ['A', 'B', 'C', 'D', 'E', 'F']
stack = []

while lista:
    stack.append(lista.pop(0))
    if lista:
        lista.append(lista.pop(0))
print stack

while stack:
    if lista:
        lista.insert(0, lista.pop(-1))
    lista.insert(0, stack.pop(-1))

print lista
Advertisements
此条目发表在Python分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s