forked from shnela/python_course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum_exercise.py
64 lines (45 loc) · 1.77 KB
/
enum_exercise.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from collections import defaultdict
from enum import Enum
from random import randint, choice
from typing import List, Dict
class Color(Enum):
red = 'RED'
green = 'GREEN'
class WeightCategory(Enum):
light = 'LIGHT'
heavy = 'HEAVY'
class Apple:
def __init__(self, color: Color, weight: int):
self.color = color
self.weight = weight
def __str__(self):
return f'Apple(color={self.color.value}, weight={self.weight})'
@staticmethod
def generate_random_apple():
return Apple(color=choice(list(Color)), weight=randint(50, 150))
def get_weight_category(self) -> WeightCategory:
"""Return WeightCategory.light if weight is below 100"""
pass
def group_apples_by_color(apples: List[Apple]) -> Dict[Color, List[Apple]]:
pass
def group_apples_by_color_then_weight(apples: List[Apple]) -> Dict[Color, Dict[WeightCategory, List[Apple]]]:
pass
if __name__ == '__main__':
light_apple = Apple(color=Color.red, weight=55)
assert light_apple.get_weight_category() == WeightCategory.light
random_apple = Apple.generate_random_apple()
print(random_apple)
# Exercise1: generate 100 random apples
apples = list()
assert apples
# Exercise2: group apples by color
apples_by_color = group_apples_by_color(apples)
assert apples_by_color[Color.red]
assert all(a.color == Color.red for a in apples_by_color[Color.red])
# Exercise3: group apples by color, then by WeightCategory
apples_by_color_and_weight = group_apples_by_color_then_weight(apples)
assert apples_by_color_and_weight[Color.red][WeightCategory.light]
assert all(
a.color == Color.red and a.weight < 100
for a in apples_by_color_and_weight[Color.red][WeightCategory.light]
)