forked from taizilongxu/interview_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack
More file actions
92 lines (79 loc) · 1.96 KB
/
stack
File metadata and controls
92 lines (79 loc) · 1.96 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Stack():
def __init__(self):
self.vec = []
def push(self, item):
self.vec.append(item)
def pop(self):
self.vec.pop()
def top(self):
return self.vec[len(self.vec) - 1]
def is_empty(self):
return self.vec == []
def size(self):
return len(self.vec)
'''
#convert num to different base
def convert1(stk, num, base):
digit = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
if num > 0:
stk.push(digit[num % base])
convert(stk, num // base, base)
def convert2(stk, num, base):
digit = ['0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
while num > 0:
stk.push(digit[num % base])
num = num // base
s = Stack()
n= []
convert2(s, 12345, 8)
while s.is_empty() == False:
n.append(s.top())
s.pop()
print(n)
'''
#stack permutation
def permutationstacks(stk1, stk2):
tmp1 = Stack()
tmp2 = Stack()
while stk2.is_empty() == False:
tmp2.push(stk2.top())
stk2.pop()
while stk1.is_empty() == False or (tmp1.is_empty() == False and tmp1.top() == tmp2.top()):
if tmp1.is_empty() == False and tmp1.top() == tmp2.top():
stk2.push(tmp1.top())
tmp1.pop()
tmp2.pop()
else:
tmp1.push(stk1.top())
stk1.pop()
# print(stk1.vec)
# print(tmp1.vec)
# print(tmp2.vec)
# print('\n')
if stk1.is_empty() == True and tmp1.is_empty() == True:
return True
return False
def permutationstk():
s = Stack()
s.vec = [4, 3, 2, 1]
s1 = Stack()
s1.vec = [3, 2, 4, 1]
s2 = Stack()
s2.vec = [1, 2, 3, 4]
print(permutationstacks(s, s1))
print(permutationstacks(s, s2))
'''
s = Stack()
print(s.is_empty())
s.push(4)
s.push('dog')
print(s.top())
print(s.size())
s.pop()
s.top()
print(s.size())
'''
if __name__ == '__main__':
permutationstk()