I'm trying to create a function that returns True is the string passed in is '1'(valid) or '1*'(valid) or in the form of '(' + valid + '|' + valid + ')' (valid). What I mean by the last part is for example:
(1|1) <- since its in the form of '(' + valid + '|' + valid + ')' and '1' is valid.
((1|1)|1) <- since its in the form of '(' + valid + '|' + valid + ')' and (1|1) is valid and so is '1'
((1|(1|1))|1) <- since its in the form of '(' + valid + '|' + valid + ')' and (1|(1|1)) is valid and so is '1'
My plan:
- 1.) if the string passed in is '1', return True (Base Case)
- 2.) if the string passed in is '1'+'*', return True
- 3.) Else, if the string passed in begins with a '(' and ends with ')' look at the contents inside of the bracket and find the '|' symbol that's not inside of another bracket. Once it is found run the function again on the string on the left of the found symbol, and also run the function again on the string on the right of the found symbol. If both are True then the string is True (Recursive Step)
- 4.) Return False if anything else is passed in.
My code based on my plan:
def check(s):
if s == '1':
return True
elif s == '1'+'*':
return True
else:
if s.startswith('(') and s.endswith(')'):
#TO-DO
pass
return False
Some examples:
check('((1|1)|1)')
True
check('((1|1*)|1)')
True
check('(1|1)')
True
check('1')
True
check('1*')
True
check('((1|(1|1))|1)')
True
check('((1|(1|1))|((1|(1|1))|1))')
True
check('*1')
False
check('2')
False
check('((1|(1|1));1)')
False
I spent 3 days on this and still couldn't figure out how to do the recursive step so I'm hoping someone can help me with this. I can't figure out how to find the '*' symbol that is not within another set of brackets. I think if I'am able to find that '*' symbol than I can do the rest myself.
I'm sure that we can do this using the re module but let's just pretend we can't use that.
(1||1|)valid? Because accroding to your description it is, but it seems strange. Is(1|)really valid?