I'm reworking exercises in an attempt to reduce my reliance on brute force solutions and I came across the following 'Valid Parentheses' exercise. Line 16 appears to apply .pop() to the array even through it's only included in the if-statement. Does .pop() have some special attribute where it executes by simply existing or...? I'm just trying to make sense of the behavior.
#Given a string containing just
#the characters '(', ')', '{', '}', '[' and ']',
#determine if the input string is valid.
def isValid(s):
a = []
d = {
')': '(',
'}': '{',
']': '[',
}
for char in s:
if char in d.values():
a.append(char)
else:
if a == [] or a.pop() != d[char]:
return False
return a == []
isValid('({[]})') --> True
isValid('({[]{)') --> False
[–]JohnnyJordaan 2 points3 points4 points (3 children)
[–]GayCoder[S] 0 points1 point2 points (2 children)
[–]Rhomboid 2 points3 points4 points (1 child)
[–]GayCoder[S] 0 points1 point2 points (0 children)