I'm writing code in Python 3 - or trying to - that takes in a string of braces such as '{{]]' and looks for an adjacent pair of braces which is either '[' followed immediately by ']', or '{' followed immediately by '}', or '(' followed immediately by ')'. It returns the first pair it finds, or False if it doesn't find any. To that end, I wrote some code:
# write a function that scans a sequence of braces to find a "married pair", or else returns false if there is none.
def find(mystring):
for x in mystring[0:-1]:
y=mystring[mystring.index(x)+1:][0]
if (x == '[' and y == ']') or (x == '{' and y == '}') or (x == '(' and y == ')'):
return x,y
else:
continue
return False
So OK it works when I try the find function on the string ({}). It returns ('{','}') as expected. But when I try find ('{{}}') it unexpectedly returns False. Running ([()]) unexpectedly returns False. ([{}]) returns True as expected.
What is going on here?
mystring.index(x) doesn't return the index you're looking at. It returns the first index of the given character. So when you're at the second { character of {{}}, index(x) returns 0. If you're using indexes while iterating, use enumerate.
A fixed version could be like this:
def find(mystring):
for i,x in enumerate(mystring[:-1]):
y = mystring[i+1]
if (x == '[' and y == ']') or (x == '{' and y == '}') or (x == '(' and y == ')'):
return x,y
return False
.index() returns the FIRST index of the occurrence of that char, so when you have more then 1, you get the wrong y.
you can pair adjacent chars using zip like this:
def find(mystring):
pairs = zip(mystring, mystring[1:])
for x, y in pairs:
if (x == '[' and y == ']') or (x == '{' and y == '}') or (x == '(' and y == ')'):
return x, y
else:
continue
return False
print(find('({})'))
print(find('{{}}'))
print(find('([()])'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With