Skip to content

Commit 7f38505

Browse files
committed
pro6 solved
1 parent 63b7047 commit 7f38505

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

unit3/balancing.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{ } [ ] ( )
2+
{ [ ] }
3+
{ [ ] } { ( ) }
4+
{
5+
{ [ } ]
6+
{ ( ) ]

unit3/output.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
5 9 2 * + 6 5 * + =
2-
5 8 * 6 10 / + =
3-
5 8 - 9 - =
4-
5 8 * 9 * =
1+
True
2+
True
3+
True
4+
False
5+
False
6+
False
7+
False

unit3/pro4.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ def calculator(self, input, output):
7979

8080

8181

82-
83-
84-
8582
a = Algorithms()
8683
a.calculator(open("infix_input.txt", 'r'), open("output.txt", 'w+'))
8784

unit3/pro6.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from opus7.stackAsLinkedList import StackAsLinkedList
2+
3+
class Algorithms(object):
4+
def __init__(self):
5+
self.stack = StackAsLinkedList()
6+
7+
def isBalanced(self, input, output):
8+
dic = {"]":"[", "}":"{", ")":"("}
9+
10+
for line in input.readlines():
11+
for word in line:
12+
if word in dic.values():
13+
self.stack.push(word)
14+
result = False
15+
elif word in dic.keys():
16+
if self.stack.getTop() == dic[word]:
17+
self.stack.pop()
18+
result = True
19+
else:
20+
self.stack.push(word)
21+
result = False
22+
output.write(str(result) + "\n")
23+
24+
25+
a = Algorithms()
26+
a.isBalanced(open("balancing.txt", "r"), open("output.txt", "w+"))
27+

0 commit comments

Comments
 (0)