Skip to content

Commit 55c8284

Browse files
authored
Create stack
1 parent 0572b2e commit 55c8284

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

DS-using-python/stack

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
class Stack():
2+
3+
def __init__(self):
4+
self.vec = []
5+
def push(self, item):
6+
self.vec.append(item)
7+
8+
def pop(self):
9+
self.vec.pop()
10+
11+
def top(self):
12+
return self.vec[len(self.vec) - 1]
13+
14+
def is_empty(self):
15+
return self.vec == []
16+
17+
def size(self):
18+
return len(self.vec)
19+
20+
'''
21+
#convert num to different base
22+
def convert1(stk, num, base):
23+
digit = ['0', '1', '2', '3', '4', '5', '6', '7',
24+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
25+
if num > 0:
26+
stk.push(digit[num % base])
27+
convert(stk, num // base, base)
28+
29+
def convert2(stk, num, base):
30+
digit = ['0', '1', '2', '3', '4', '5', '6', '7',
31+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
32+
while num > 0:
33+
stk.push(digit[num % base])
34+
num = num // base
35+
36+
37+
s = Stack()
38+
n= []
39+
convert2(s, 12345, 8)
40+
while s.is_empty() == False:
41+
n.append(s.top())
42+
s.pop()
43+
print(n)
44+
'''
45+
46+
#stack permutation
47+
def permutationstacks(stk1, stk2):
48+
tmp1 = Stack()
49+
tmp2 = Stack()
50+
while stk2.is_empty() == False:
51+
tmp2.push(stk2.top())
52+
stk2.pop()
53+
54+
while stk1.is_empty() == False or (tmp1.is_empty() == False and tmp1.top() == tmp2.top()):
55+
if tmp1.is_empty() == False and tmp1.top() == tmp2.top():
56+
stk2.push(tmp1.top())
57+
tmp1.pop()
58+
tmp2.pop()
59+
else:
60+
tmp1.push(stk1.top())
61+
stk1.pop()
62+
# print(stk1.vec)
63+
# print(tmp1.vec)
64+
# print(tmp2.vec)
65+
# print('\n')
66+
if stk1.is_empty() == True and tmp1.is_empty() == True:
67+
return True
68+
return False
69+
70+
def permutationstk():
71+
s = Stack()
72+
s.vec = [4, 3, 2, 1]
73+
s1 = Stack()
74+
s1.vec = [3, 2, 4, 1]
75+
s2 = Stack()
76+
s2.vec = [1, 2, 3, 4]
77+
print(permutationstacks(s, s1))
78+
print(permutationstacks(s, s2))
79+
80+
'''
81+
s = Stack()
82+
print(s.is_empty())
83+
s.push(4)
84+
s.push('dog')
85+
print(s.top())
86+
print(s.size())
87+
s.pop()
88+
s.top()
89+
print(s.size())
90+
'''
91+
if __name__ == '__main__':
92+
permutationstk()

0 commit comments

Comments
 (0)