-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
126 lines (96 loc) · 2.04 KB
/
practice.py
File metadata and controls
126 lines (96 loc) · 2.04 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#def swapPos(list, pos1, pos2):
# def swapPos(list, pos1, pos2):
# list[pos1], list[pos2] = list[pos2], list[pos1]
# return list
#swapPos([1,2,3,4],2,4)
#ef factorial(num):
# if num < 2:
# return num
# else:
# num = num * factorial(num-1)
# return num
#def fib(n):
# a=0
# b=1
# for i in range(n):
# c=a+b
# a=b
# b = c
# print(a)
#fib(7)
#def fib(n):
# if n == 0:
# return 0
# if n == 1:
# return 1
# return fib(n-1) + fib(n-2)
#def digsum(num):
# tot = 0
# for i in range(0,len(str(num))):
# tot += int(str(num)[i])
# print(tot)
#digsum(2356)
##def sumDigits(num):
# if num == 0:
# return 0
# return int(num % 10) + sumDigits(num//10)
###BINARY TREES
#class Node:
# def __init__(self, val):
# self.left = None
# self.right = None
# self.val = val
#n1 = Node(1)
#n2 = Node(2)
#n3 = Node(3)
#n4 = Node(4)
#n5 = Node(5)
#n6 = Node(6)
#n1.left = n2
#n1.right = n3
#n2.left = n4
#n2.right = n5
#n3.left = n6
##Left, Root Right
#def printInorder(root):
# if root:
# printInorder(root.left)
# print(root.val)
# printInorder(root.right)
#def printPostorder(root):
# if root:
# printInorder(root.left)
# printInorder(root.right)
# print(root.val)
#def printPreorder(root):
# if root:
# print(root.val)
# printInorder(root.left)
# printInorder(root.right)
####STACK PRACTICE
#Input = "()" = VALID
#Input = "(()" = INVALID
#Input = "(Hi))" = INVALID
#
# stack.pop()
# stack.push()
def validParen(str):
#stack = []
#stack.pop()
#stack.append()
#print(('INVALID', 'VALID')[str.count('(') == str.count(')')])
stack = []
for c in str:
if c == '(':
stack.push(1)
elif c == ')':
if len(stack) == 0:
return False
else:
stack.pop()
if len(stack) != 0:
return False
else:
return True
stack = []
print(stack.pop())