Comparing cards
For built-in types, there are conditional operators (<, >, ==, etc.) that compare values and determine when one is greater than, less than, or equal to another. For user-defined types, we can override the behavior of the built-in operators by providing a method named __cmp__. But in Python 3+, the cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)
The cmp method takes two parameters, self and other, and returns a positive number if the first object is greater, a negative number is the second object is greater, and 0 if they are equal to each other. The correct ordering for cards is not obvious. For example, which is better, the 3 of Clubs or the 2 of Diamonds? One has a higher rank, but the other has a higher suit. In order to compare cards, you have to decide whether rank or suit is more important.
The answer might depend on what game you are playing, but to keep things simple, we’ll make the arbitrary choice that suit is more important, so all of the Spades outrank all of the Diamonds, and so on.
def cmp(self,other):
t1 = (self.suit,self.rank)
t2 = (other.suit,other.rank)
return (t1 > t2) - (t1 < t2)
from Thinking in Python
Comparing cards的更多相关文章
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- How to appraise Hearthstone card values
https://elie.net/blog/hearthstone/how-to-appraise-hearthstone-card-values/ In 2014, I became an avid ...
- BZOJ 1004 【HNOI2008】 Cards
题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...
- Codeforces Round #384 (Div. 2) 734E Vladik and cards
E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Comparing the MSTest and Nunit Frameworks
I haven't seen much information online comparing the similarities and differences between the Nunit ...
- bzoj 1004 Cards
1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...
- codeforces 744C Hongcow Buys a Deck of Cards
C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...
- CF 204B Little Elephant and Cards
题目链接: 传送门 Little Elephant and Cards time limit per test:2 second memory limit per test:256 megab ...
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
随机推荐
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- 【linux】之安装mysql常用配置
下载mysql地址 http://dev.mysql.com/downloads/mysql/ 选择下面这个 查看是否存在mysql安装包 rpm -qa|grep -i mysql 删除mysql安 ...
- POWERDESIGNER中显示样式设置
1.调整表.视图的显示样式. 右键选中的对象,选择format(或ctrl+t),在弹出窗口中选中content,可以设置只显示表名还是把所有列也显示出来. 2.如何显示表中字段的code. tool ...
- C#中判断子窗体是否存在
可在父窗体中定义一个全局变量 public static Form form; 然后在秀出子窗体的点击事件中增加加下代码 if (form == null) { //窗体不存在时,实例化子窗体,并显示 ...
- 黄聪:wordpress在IIS8中设置默认编码(windows2012服务器)
web.config中配置 <?xml version="1.0" encoding="UTF-8"?> <configuration> ...
- python (16) 如何在linux下安装lxml(pip安装,ubuntu下,centos下)
首先需要明白lxml包依赖其他包,必须先安装其他包然后再安装lxml 安装python-pip:方便安装python的第三方包 [root@******/]# wget https://bootstr ...
- 标准电流信号为什么是4-20MA?(网络摘录)
一来源: 4-20mA.DC(1-5V.DC)信号制是国际电工委员会(IEC):过程控制系统(是连接仪表.变送设备.控制设备.计算机采样设备)用模拟信号标准.我国从DDZ-Ⅲ型电动仪表开始采用这一国际 ...
- window下安装oracle数据库
Oracle 11g安装 1.解压下载的包,然后进入包内,点击setup.exe开始安装 . 2.出现如下:一般把那个小对勾取消,点击下一步进行, 弹出下图这个后点‘是’ 3.下图后,选择创建和配置数 ...
- ylbtech-Bill(发票管理)-数据库设计
ylbtech-dbs:ylbtech-Bill(发票管理)-数据库设计 -- =============================================-- DatabaseName ...
- 深入ThreadLocal之三(ThreadLocal可能引起的内存泄露)
threadlocal里面使用了一个存在弱引用的map,当释放掉threadlocal的强引用以后,map里面的value却没有被回收.而这块value永远不会被访问到了. 所以存在着内存泄露. 最好 ...