Apr-11-2022, 12:18 PM
I hope not to sound very weird here, basically, suppose that you have the following strings:
So the output for the cases above would be:
I was thinking of building it like this:
s1 = '{[]}' s2 = '[(])' s3 = '()[]{}'And you want to build a program/function that verifies that such strings are
'valid'
if: > **Open brackets are closed by the same type of brackets**
So the output for the cases above would be:
theFunction(s1)
> **valid**
theFunction(s2)
> **NOT valid**
theFunction(s3)
> **valid**
I was thinking of building it like this:
def theFunction(s): the_dic = {'(':')', '[':']', '{':'}', } if any(s.startswith(ch) for ch in the_dic.keys()): for char in s: #...? result = 'valid' else: result = 'NOT valid' return resultHowever, I'm not good enough at advanced nested-looping, may I get some help please?