zoukankan      html  css  js  c++  java
  • Add, remove, shuffle and sort

    To deal cards, we would like a method that removes a card from the deck and returns it. The list method pop provides a convenient way to do that. Since pop removes the last card in the list, we are in effect dealing from the bottom of the deck. To add a card, we can use the list method append. As another example, we can write a Deck method named shuffle using the function shuffle from the random module.

        def pop_card(self):
            return self.cards.pop()
    
        def add_card(self,card):
            return self.cards.append(card)
    
        def shuffle(self):
            random.shuffle(self.cards)
    
        def sort(self):
            for i in range(len(self.cards)):
                for j in range(i+1,len(self.cards)):
                    if(self.cards[i].cmp(self.cards[j])>0):
                        self.cards[i],self.cards[j] = self.cards[j],self.cards[i]

    A method like this that uses another function without doing much real work is sometimes called a veneer, the metaphor comes from woodworking, where it is common to glue a thin layer of good quality wood to the surface of a cheaper piece of wood.

    from Thinking in Python

     

  • 相关阅读:
    vijos1776:关押罪犯
    vijos1774:机器翻译
    vijos1775:乌龟棋
    vijos1792:摆花
    vijos1100:加分二叉树
    vijos1706:舞会
    单调栈
    bzoj1677:求和
    bzoj1340: [Baltic2007]Escape逃跑问题
    bzoj4716: 假摔
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4008476.html
Copyright © 2011-2022 走看看