File tree Expand file tree Collapse file tree 2 files changed +48
-5
lines changed Expand file tree Collapse file tree 2 files changed +48
-5
lines changed Original file line number Diff line number Diff line change 4
4
Card = collections .namedtuple ('Card' , ['rank' , 'suit' ])
5
5
6
6
7
- class Deck ( object ) :
7
+ class Deck :
8
8
ranks = [str (i ) for i in range (2 , 11 )] + list ('JKQA' )
9
9
suits = 'spades diamonds clubs hearts' .split ()
10
10
11
11
def __init__ (self ):
12
- self ._cards = [Card (rank , suit ) for rank in self .ranks
13
- for suit in self .suits ]
12
+ self ._cards = [Card (rank , suit ) for suit in self .suits
13
+ for rank in self .ranks ]
14
14
15
15
def __len__ (self ):
16
16
return len (self ._cards )
17
17
18
18
def __getitem__ (self , position ):
19
19
return self ._cards [position ]
20
20
21
+ def __repr__ (self ):
22
+ return 'Deck()'
23
+
21
24
22
25
def main ():
23
26
deck = Deck ()
24
27
# print(len(deck))
25
- print (random .choice (deck ))
26
-
28
+ for __ in range (5 ):
29
+ print (random .choice (deck ))
30
+ # print(deck[12::13])
31
+ # for card in reversed(deck):
32
+ # print(card)
27
33
28
34
if __name__ == "__main__" :
29
35
main ()
Original file line number Diff line number Diff line change
1
+ from math import hypot
2
+
3
+
4
+ class Vector :
5
+ def __init__ (self , x = 0 , y = 0 ):
6
+ self .x = x
7
+ self .y = y
8
+
9
+ def __repr__ (self ):
10
+ return "Vector({!r}, {!r})" .format (self .x , self .y )
11
+
12
+ def __abs__ (self ):
13
+ return hypot (self .x , self .y )
14
+
15
+ def __bool__ (self ):
16
+ return bool (abs (self ))
17
+
18
+ def __add__ (self , other ):
19
+ x = self .x + other .x
20
+ y = self .y + other .y
21
+ return Vector (x , y )
22
+
23
+ def __mult__ (self , scalar ):
24
+ return Vector (self .x * scalar , self .y * scalar )
25
+
26
+
27
+ def main ():
28
+ v1 = Vector (2 , 4 )
29
+ v2 = Vector (2 , 1 )
30
+
31
+ print (v1 )
32
+ print (v2 )
33
+ # assert (v1 + v2) == Vector(4, 5)
34
+ # assert abs( * 3)
35
+
36
+ if __name__ == "__main__" :
37
+ main ()
You can’t perform that action at this time.
0 commit comments