-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_konsep.py
More file actions
63 lines (52 loc) · 1.29 KB
/
Copy pathstack_konsep.py
File metadata and controls
63 lines (52 loc) · 1.29 KB
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
class Stack:
def __init__(self) :
self.items = []
def pop(self):
if self.isEmpty():
raise RuntimeError("percobaan pop pada stack kosong")
topIdx = len(self.items)-1
item = self.items[topIdx]
del self.items[topIdx]
return item
def push(self, item):
self.items.append(item)
def top(self):
if self.isEmpty():
raise RuntimeError("Percobaan akses top pada stack kosong")
topIdx = len(self.items)-1
return self.items[topIdx]
def isEmpty(self):
return len(self.items) == 0
def main():
s = Stack()
lst = list(range(10))
lst2 = []
for k in lst:
s.push(k)
if s.top() == 9:
print("Tes 1 berhasil")
else:
print("Tes 1 gagal")
while not s.isEmpty():
lst2.append(s.pop())
lst2.reverse()
if lst2 != lst:
print("Tes 2 gagal")
else:
print("Tes 2 berhasil")
try:
s.pop()
print("Tes 3 gagal")
except RuntimeError:
print("Tes 3 berhasil")
except:
print("Tes 3 gagal")
try:
s.top()
print("Tes 4 gagal")
except RuntimeError:
print("Tes 4 berhasil")
except:
print(" Tes 4 gagal")
if __name__ == "__main__":
main()