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

     

  • 相关阅读:
    数据库的三大范式
    mysql中变量的定义
    java实现用两个栈实现队列
    java实现替换空格
    java实现二维数组中查找
    struts2核心配置之Action
    struts2核心配置之struts.xml
    初识struts2
    $.ajax()参数详解
    百度Map-JSAPI-覆盖物范围查询标记
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4008476.html
Copyright © 2011-2022 走看看