-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
63 lines (45 loc) · 1.13 KB
/
stack.py
File metadata and controls
63 lines (45 loc) · 1.13 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
class Stack:
def __init__(self) -> None:
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
def size(self):
return len(self.items)
def is_empty(self):
return self.items == []
def par_checker(symbol_string: str) -> bool:
s = Stack()
balanced = True
index = 0
while index < len(symbol_string) and balanced:
symbol = symbol_string[index]
if symbol == '(':
s.push(symbol)
else:
if s.is_empty():
balanced = False
else:
s.pop()
index += 1
if s.is_empty() and balanced:
return True
else:
return False
def divid_by_2(decimal: int) -> str:
string = ''
s = Stack()
while decimal > 0:
rem = decimal % 2
s.push(rem)
decimal = decimal//2
while not s.is_empty():
string += str(s.pop())
return string
if __name__ == "__main__":
while True:
s = input('input:')
print(divid_by_2(int(s)))