Python Forum

Full Version: Python Coding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Need Help in python code for below conversion:

"()" => "00"
")(" => "-11"
"((a) => "10a0"
"(a(b)c) => "0a0b0c"
"()a((((" => "00a1111"
what have you tried so far?
(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:(
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
(Oct-26-2021, 11:10 PM)kshtriyadin1994 Wrote: [ -> ]"()" => "00"
")(" => "-11"
"((a) => "10a0"
"(a(b)c) => "0a0b0c"
"()a((((" => "00a1111"
What is the logic in this? It is like magic.