|
@@ -0,0 +1,107 @@
|
|
|
+import collections
|
|
|
+from random import choice
|
|
|
+import math
|
|
|
+
|
|
|
+
|
|
|
+Card = collections.namedtuple('Card', ['rank', 'suit'])
|
|
|
+
|
|
|
+class FrenchDeck:
|
|
|
+ ranks = [str(n) for n in range(2, 11)] + list('JQKA')
|
|
|
+ suits = 'spades diamonds clubs hearts'.split()
|
|
|
+
|
|
|
+ def __init__(self) -> None:
|
|
|
+ self._cards = [Card(rank, suit) for suit in self.suits
|
|
|
+ for rank in self.ranks]
|
|
|
+
|
|
|
+ def __len__(self):
|
|
|
+ return len(self._cards)
|
|
|
+
|
|
|
+ def __getitem__(self, position):
|
|
|
+ return self._cards[position]
|
|
|
+
|
|
|
+ def spades_high(self, card: Card):
|
|
|
+ rank_value = self.ranks.index(card.rank)
|
|
|
+ print(rank_value*len())
|
|
|
+
|
|
|
+
|
|
|
+ def foo(self):
|
|
|
+ self.suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
|
|
|
+ print(self.suit_values)
|
|
|
+
|
|
|
+
|
|
|
+def card_test():
|
|
|
+ deck = FrenchDeck()
|
|
|
+
|
|
|
+ card = choice(deck)
|
|
|
+ print(deck[1])
|
|
|
+ deck.spades_high(deck[1])
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+'''
|
|
|
+vector: упрощенный класс, демонстрирующий некоторые специальные методы.
|
|
|
+'''
|
|
|
+class Vector:
|
|
|
+ def __init__(self, x=0, y=0) -> None:
|
|
|
+ self.x = x
|
|
|
+ self.y = y
|
|
|
+
|
|
|
+ def __repr__(self) -> str:
|
|
|
+ return f'Vector({self.x!r}, {self.y!r})'
|
|
|
+
|
|
|
+
|
|
|
+ def __abs__(self):
|
|
|
+ return math.hypot(self.x, self.y)
|
|
|
+
|
|
|
+ def __bool__(self):
|
|
|
+ return bool(abs(self))
|
|
|
+
|
|
|
+ def __add__(self, other):
|
|
|
+ x = self.x + other.x
|
|
|
+ y = self.y + other.y
|
|
|
+ return Vector(x, y)
|
|
|
+
|
|
|
+ def __mul__(self, scalar):
|
|
|
+ return Vector(self.x * scalar, self.y * scalar)
|
|
|
+
|
|
|
+
|
|
|
+def vector_test():
|
|
|
+ v1 = Vector(3, 4)
|
|
|
+ v2 = Vector(1, 2)
|
|
|
+ v3 = v1 + v2
|
|
|
+ v4 = v3 * 2
|
|
|
+ print(abs(v1))
|
|
|
+ print(v3)
|
|
|
+ print(v4)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def main():
|
|
|
+ vector_test()
|
|
|
+
|
|
|
+
|
|
|
+def test():
|
|
|
+
|
|
|
+
|
|
|
+ Car = collections.namedtuple('Car', 'color mileage')
|
|
|
+ car1 = Car('red', 150)
|
|
|
+ print(car1.color, car1.mileage)
|
|
|
+ print(car1._fields)
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|