-
Notifications
You must be signed in to change notification settings - Fork 6
/
map and lambda.py
67 lines (58 loc) · 1.44 KB
/
map and lambda.py
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
# def fibonacci(nterms):
# a = 0
# b = 1
# l = []
# if nterms == 0:
# pass
# elif nterms == 1:
# l.append(a)
# else:
# l.append(a)
# l.append(b)
# for i in range(2, nterms):
# c = a + b
# a = b
# b = c
# l.append(c)
# return l
# cube = lambda x: x**3
# if __name__ == '__main__':
# n = int(input())
# print(list(map(cube, fibonacci(n))))
# Program to find whether given input string has balanced brackets or not
def isBalanced(s):
a = []
for i in range(len(s)):
if s[i] == '{' or s[i] == '[' or s[i] == '(':
a.append(s[i])
if s[i] == '}':
if len(a) == 0:
return "NO"
else:
if a[-1] == '{':
a.pop()
else:
break
if s[i] == ']':
if len(a) == 0:
return "NO"
else:
if a[-1] == '[':
a.pop()
else:
break
if s[i] == ')':
if len(a) == 0:
return "NO"
else:
if a[-1] == '(':
a.pop()
else:
break
if len(a) == 0:
return "YES"
else:
return "NO"
inp = input('Enter your query string: ')
#sample input: {)[](}]}]}))}(())(
print(isBalanced(inp))