-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces_stack.py
43 lines (34 loc) · 1.01 KB
/
pieces_stack.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
from typing import Union, List
from piece import Piece
class PiecesStack:
def __init__(self, pieces: Union[None, List[Piece]] = None):
if pieces is None:
self.pieces = []
else:
self.pieces = pieces
def get_size(self) -> int:
return len(self.pieces)
def is_empty(self) -> bool:
if not self.pieces:
return True
else:
return False
def add(self, piece: Piece) -> bool:
if self.is_add_valid(piece):
self.pieces.append(piece)
return True
return False
def pop(self) -> None:
if not self.is_empty():
self.pieces.pop()
def top(self) -> Union[None, Piece]:
if not self.is_empty():
return self.pieces[-1]
return None
def is_add_valid(self, piece: Piece) -> bool:
top_piece = self.top()
if top_piece is None:
return True
if piece.size > top_piece.size:
return True
return False