Oct-26-2021, 11:10 PM
Oct-27-2021, 01:54 AM
what have you tried so far?
Oct-28-2021, 05:08 AM
(Oct-27-2021, 01:54 AM)Larz60+ Wrote: [ -> ]what have you tried so far?
def fun(str): print(str) dir = {} cbflg = 0 obflg = 0 cbfnd = 0 str1 = '' for i in range(len(str)): if str[i] == ')' and cbflg == 0: cbflg = 0 str1 += '-1' elif str[i] == '(': j = i + 1 for j in range(len(str) - i): if str[j] == ')': cbfnd = 1 print(j, str[j]) print(cbfnd) if cbfnd == 1: str1 += '0' else: str1 += '1' obflg = 0 str1 += '1' else: str1 += str[i] print(str1) fun('))(()')trying, with string but no success:(
Oct-28-2021, 07:58 AM
This is not quite right, but will give you something to play with:
def fun(svalue): ovalue = '' conversions = { "()": "00", ")(": "-11", "((a)": "10a0", "(a(b)c)": "0a0b0c", "()a((((": "00a1111" } # check full value for key, value in conversions.items(): if key in svalue: ovalue = ovalue + value print(f"input: {svalue}, output: {ovalue}") fun('))(()')
Output:input: ))((), output: 00-11