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 ...
随机推荐
- Learning Puppet — Variables, Conditionals, and Facts
Begin $my_variable = "A bunch of text" notify {$my_variable:} Yup, that’s a variable, all ...
- Thinkpad X240修改bios引导方式
来源:http://blog.csdn.net/jsship/article/details/19121149 修改笔记本的BIOS设置!这是非常重要的步骤之一.否则,你的U盘不能引导手提电脑进入PE ...
- 【freemaker】之FreeMakerUtil工具类
Freemaker生成文件常用工具类 public class FreemakerUtil { private static FreemakerUtil util; private static Co ...
- 我的Android最佳实践之—— 解决闪空界面问题
进入应用时,由于应用的启动Activity都会有默认的theme,所以会跳一下原始界面,才启动我们定义的theme. 修改这个问题的方法,就是给应用启动的Activity设置一个空的theme.如下面 ...
- 8. redis的主从复制和sentinal
一. redis主从复制(读写分离) redis的主从复制分为两类节点:1个master和多个slave,master进行读写操作,slav进行只读操作 启动步骤: 主节点照常启动,slave节点启动 ...
- jdbc:java数据库连接
1.导jar包 四大参数: 1.加载驱动类:Class.forName("com.mysql.jdbc.Driver"); 2.url:jdbc:mysql://localhost ...
- Hadoop系统架构
一.Hadoop系统架构图 Hadoop1.0与hadoop2.0架构对比图 YARN架构: ResourceManager –处理客户端请求 –启动/监控ApplicationMaster –监控N ...
- PLSQL_数据泵导入导出数据Impdp/ Expdp(概念)
2014-08-31 Created By BaoXinjian
- hdu 1536 S-Nim(sg函数模板)
转载自:http://blog.csdn.net/sr_19930829/article/details/23446173 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...
- POJ 1611 The Suspects(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...