[go: nahoru, domu]

Skip to content

Commit

Permalink
Adding __eq__ to Card
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney committed Mar 4, 2016
1 parent fb6404e commit 9ea23ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
10 changes: 6 additions & 4 deletions book/book.tex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
\title{Think Python}
\author{Allen B. Downey}
\newcommand{\thetitle}{Think Python: How to Think Like a Computer Scientist}
\newcommand{\theversion}{2nd Edition, Version 2.2.14}
\newcommand{\theversion}{2nd Edition, Version 2.2.15}
\newcommand{\thedate}{}

% these styles get translated in CSS for the HTML version
Expand Down Expand Up @@ -8291,6 +8291,8 @@ \section{Debugging}
In this example you could also use the built-in function {\tt sorted},
which returns a new, sorted list and leaves the original alone.
\index{sorted!function}
\index{function!sorted}
\begin{verbatim}
>>> t2 = sorted(t)
Expand Down Expand Up @@ -8857,8 +8859,8 @@ \section{Looping and dictionaries}
%
Again, the keys are in no particular order. To traverse the keys
in sorted order, you can use the built-in function {\tt sorted}:
\index{keys method}
\index{method!keys}
\index{sorted!function}
\index{function!sorted}
\begin{verbatim}
>>> for key in sorted(h):
Expand Down Expand Up @@ -10791,7 +10793,7 @@ \section{Dictionary subtraction}
diff = subtract(hist, words)
print("Words in the book that aren't in the word list:")
for word in diff.keys():
for word in diff:
print(word, end=' ')
\end{verbatim}
%
Expand Down
7 changes: 7 additions & 0 deletions code/Card.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def __str__(self):
return '%s of %s' % (Card.rank_names[self.rank],
Card.suit_names[self.suit])

def __eq__(self, other):
"""Checks whether self and other have the same rank and suit.
returns: boolean
"""
return self.suit == other.suit and self.rank == other.rank

def __lt__(self, other):
"""Compares this card to other, first by suit, then rank.
Expand Down
23 changes: 23 additions & 0 deletions code/Card_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""This file contains code for use with "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2014 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""

from __future__ import print_function, division

import unittest
from Card import Card, Deck


class Test(unittest.TestCase):

def testDeckRemove(self):
deck = Deck()
card23 = Card(2, 3)
deck.remove_card(card23)


if __name__ == "__main__":
unittest.main()

0 comments on commit 9ea23ce

Please sign in to comment.