Python Forum
Python Coding - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Coding (/thread-35380.html)



Python Coding - kshtriyadin1994 - Oct-26-2021

Need Help in python code for below conversion:

"()" => "00"
")(" => "-11"
"((a) => "10a0"
"(a(b)c) => "0a0b0c"
"()a((((" => "00a1111"


RE: Python Coding - Larz60+ - Oct-27-2021

what have you tried so far?


RE: Python Coding - kshtriyadin1994 - Oct-28-2021

(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:(


RE: Python Coding - Larz60+ - Oct-28-2021

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



RE: Python Coding - ibreeden - Oct-28-2021

(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.