Skip to content

Commit bd7a286

Browse files
author
lev epshtein
committed
adding adv data-types
1 parent 4c04f9f commit bd7a286

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

data-types/cards.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import collections
2+
3+
Card = collections.namedtuple('Card', ['rank', 'suit'])
4+
5+
class FrenchDeck:
6+
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
7+
suits = 'spades diamonds clubs hearts'.split()
8+
9+
def __init__(self):
10+
self._cards = [Card(rank, suit) for suit in self.suits
11+
for rank in self.ranks]
12+
13+
def __len__(self):
14+
return len(self._cards)
15+
16+
def __getitem__(self, position):
17+
return self._cards[position]
18+
19+
if __name__ == "__main__":
20+
print ("Let's create one car 7 diamons:")
21+
beer_card = Card('7','diamons')
22+
print(beer_card)
23+
print ("*" * 10)
24+
print ("Let's create deck")
25+
deck = FrenchDeck()
26+
print ("Cards in deck:")
27+
print(len(deck))
28+
print ("*" * 10)
29+
print("Let's take random card:")
30+
print ("*" * 10)
31+
from random import choice
32+
print(choice(deck))
33+
for card in deck:
34+
print(card)
35+
print ("*" * 10)
36+
print("Print reversted deck:")
37+
print ("*" * 10)
38+
for card in reversed(deck):
39+
print(card)
40+
print (Card('Q', 'hearts') in deck)
41+
print (Card('7', 'beasts') in deck)
42+
print ("*" * 10)
43+
print("Let's sorted:")
44+
print ("*" * 10)
45+
suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
46+
def spades_high(card):
47+
rank_value = FrenchDeck.ranks.index(card.rank)
48+
return rank_value * len(suit_values) + suit_values[card.suit]
49+
for card in sorted(deck, key=spades_high):
50+
print(card)

data-types/t-shirtts.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## T-shirts with list comprehension
2+
3+
colors = ['black', 'white']
4+
sizes = ['S', 'M', 'L']
5+
6+
for color in colors:
7+
for size in sizes:
8+
print((color,size))
9+
print ("Let's create now list of tuples:")
10+
11+
tshirts = [(color, size) for color in colors for size in sizes]
12+
13+
print ("List of tuples:")
14+
print (tshirts)
15+
print (type(tshirts))
16+
17+
for tshirt in ('%s %s' %(c,s) for c in colors for s in sizes):
18+
print(tshirt)

0 commit comments

Comments
 (0)