-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.py
More file actions
45 lines (29 loc) · 811 Bytes
/
ArrayStack.py
File metadata and controls
45 lines (29 loc) · 811 Bytes
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
class ArrayStack:
def __init__(self):
self.data=[]
def __len__(self):
return len(self.data)
def is_empty(self):
return len(self.data) == 0
def push(self,e):
self.data.append(e)
def top(self):
if self.is_empty():
return None
else:
return self.data[-1]
def pop(self):
if self.is_empty():
return None
else:
return self.data.pop()
def reverse_file(filename):
S=ArrayStack()
original = open(filename)
for line in original:
S.push(line.rstrip("\n"))
original.close()
output=open(filename,"w")
while not S.is_empty():
output.write(S.pop() + "\n")
output.close()