Skip to content

Commit 43ef663

Browse files
authored
Add files via upload
# Implement a stack that is identical to stack of plates # When a stack reach to a capacity push the new element to a new stack # that is continuation of the previous stack
1 parent c73124d commit 43ef663

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Q_stackPlates.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Implement a stack that is identical to stack of plates
2+
# When a stack reach to a capacity push the new element to a new stack
3+
# that is continuation of the previous stack
4+
5+
class StackPlates():
6+
def __init__(self, capacity):
7+
self.capacity = capacity
8+
self.stacks = []
9+
10+
def __str__(self):
11+
return self.stacks
12+
13+
14+
def push(self, item):
15+
# if there is space add it to the end
16+
if len(self.stacks) > 0 and (len(self.stacks[-1])) < self.capacity:
17+
self.stacks[-1].append(item)
18+
else:
19+
# because its 2 dimensional list
20+
self.stacks.append([item])
21+
22+
23+
def pop(self):
24+
while len(self.stacks) and len(self.stacks[-1]) == 0:
25+
self.stacks.pop()
26+
if len(self.stacks) == 0:
27+
return None
28+
else:
29+
return self.stacks[-1].pop()
30+
31+
32+
def pop_at(self, stack_num):
33+
if len(self.stacks[stack_num]) > 0:
34+
return self.stacks[stack_num].pop()
35+
else:
36+
return None

0 commit comments

Comments
 (0)