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

时间: 2024-12-24 23:07:27

Add, remove, shuffle and sort的相关文章

Maste Note for OCR / Vote disk Maintenance Operations (ADD/REMOVE/REPLACE/MOVE)

Doc ID 428681.1 Applies to: Oracle Database - Enterprise Edition - Version 10.2.0.1 to 11.2.0.1.0 [Release 10.2 to 11.2]Information in this document applies to any platform. Goal The goal of this note is to provide steps to add, remove, replace or mo

shuffle和sort分析

MapReduce中的Shuffle和Sort分析 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Scheme,ML 等.MapReduce 框架的核心步骤主要分两部分:Map 和Reduce.当你向MapReduce 框架提交一个计算作业时,它会首先把计算作业拆分成若干个Map 任务,然后分配到不同的节点上去执行,每一个Map 任务处理输入数据中的一部分

Hadoop-2.2.0中文文档—— MapReduce下一代- 可插入的 Shuffle 和 Sort

简介 可插入的 shuffle 和 sort 功能,允许在shuffle 和 sort 逻辑中用可选择的实现类替换.这个情况的例子是:用一个不是HTTP的应用协议,如RDMA来 shuffle 从Map节点中到Reducer节点的数据:或者用自定义的允许 Hash聚合和Limit-N查询的算法来代替sort逻辑. 重要: 可插入的 shuffle  sort 功能是实验性的.不稳定.这意味着提供的API可能改变或破坏未来Hadoop版本的兼容性. 实现一个自定义的 Shuffle 和 Sort

mapreduce shuffle 和sort 详解

    MapReduce 框架的核心步骤主要分两部分:Map 和Reduce.当你向MapReduce 框架提交一个计算作业时,它会首先把计算作业拆分成若干个Map 任务,然后分配到不同的节点上去执行,每一个Map 任务处理输入数据中的一部分,当Map 任务完成后,它会生成一些中间文件,这些中间文件将会作为Reduce 任务的输入数据.Reduce 任务的主要目标就是把前面若干个Map 的输出汇总到一起并输出. 本文的重点是剖析MapReduce 的核心过程--Shuffle和Sort.在本文

WIX: Hide installed program from the Add/Remove Programs window.

Reference article : How to hide an entry in the Add/Remove Programs applet? In Wix source files, set property ARPSYSTEMCOMPONENT = 1 would do this, for example: <Product> <PropertyId="ARPSYSTEMCOMPONENT">1</Property> </Produ

How to hide an entry in the Add/Remove Programs applet?

Original link: http://www.winhelponline.com/articles/15/1/How-to-hide-an-entry-in-the-AddRemove-Programs-applet.html ------------------------Following content is only for knowledge sharing. --------------------------- This article discusses the metho

list在遍历过程中的add/remove

平时开发过程中,很多人估计都遇到过一个问题:在遍历集合的的过程中,进行add或者remove操作的时候,会出现2类错误,包括:java.util.ConcurrentModificationException for in遍历过程中add/remove导致的错误java.lang.IndexOutOfBoundsException 越界错误,for循环的时候删除元素. 那么我们应该怎样避免这个问题呢? 首先对于add操作:建议利用原生的for循环.remove操作利用foreach操作 具体代码

HashSet——add remove contains方法底层代码分析(hashCode equals 方法的重写)

引言:我们都知道HashSet这个类有add   remove   contains方法,但是我们要深刻理解到底是怎么判断它是否重复加入了,什么时候才移除,什么时候才算是包括????????? add()方法 首先我们看下这个代码 1 package com.xt.set; 2 3 import java.util.HashSet; 4 import java.util.Iterator; 5 import java.util.Set; 6 7 public class AddTest { 8

ListIterator add remove 使用注意

add方法示例 //在最前面添加 List<String> list1 = new LinkedList<String>(Arrays.asList(new String[] { "a", "b", "c" })); ListIterator<String> listIterator1 = list1.listIterator(); listIterator1.add("D"); listI