MCPcopy Create free account
hub / github.com/acm-clan/algorithm-stone / AlgoStack

Class AlgoStack

animations/src/algo_stack.py:4–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2from . import *
3
4class AlgoStack(VGroup):
5 def __init__(self, scene, datas, **kwargs):
6 self.scene = scene
7 self.datas = datas
8 super().__init__(**kwargs)
9 # add base line
10 line = Line(start=UP, end=DOWN).scale(0.5).set_color(BLUE)
11 self.add(line)
12 self.base_line = line
13 # add node
14 self.nodes = []
15 for k in datas:
16 n = AlgoNode(str(k))
17 self.add(n)
18 self.nodes.append(n)
19 self.arrange()
20
21 def push(self, data):
22 n = AlgoNode(str(data))
23 self.add(n)
24 self.datas.append(data)
25 self.nodes.append(n)
26 self.arrange()
27 print("stack push size:", len(self.datas))
28
29 def pop(self):
30 p = self.nodes[-1]
31 self.remove(p)
32 self.scene.play(FadeOut(p.copy()))
33 del self.datas[-1]
34 del self.nodes[-1]
35 self.arrange()
36 print("stack pop size:", len(self.datas))
37
38 def empty(self):
39 return len(self.datas) == 0
40
41 def top(self):
42 if self.empty():
43 print("error call top data")
44 return None
45 return self.nodes[len(self.nodes)-1]
46
47 def top_data(self):
48 if self.empty():
49 print("error call top data")
50 return None
51 return self.datas[len(self.datas)-1]

Callers 1

constructMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected