-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversedIteration.py
35 lines (32 loc) · 1.1 KB
/
ReversedIteration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from collections import namedtuple
_Suits = ('spades','hearts','clubs','diamonds')
_Ranks = tuple(range(2,11))+tuple('JQKA')
Card = namedtuple('Card', 'rank suit')
class CardDeck:
def __init__(self):
self.length = len(_Suits)*len(_Ranks)
def __len__(self):
return self.length
def __iter__(self):
return self.CardDeckIterator(self.length)
def __reversed__(self):
return self.CardDeckIterator(self.length, reverse=True)
class CardDeckIterator:
def __init__(self, length, reverse = False):
self.length = length
self.reverse = reverse
self.i = 0
def __iter__(self):
return self
def __next__(self):
if self.i >= self.length:
raise StopIteration
else:
if self.reverse:
index = self.length - 1 - self.i
else:
index = self.i
suit = _Suits[index // len(_Ranks)]
rank = _Ranks[index % len(_Ranks)]
self.i += 1
return Card(rank, suit)