Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Coding
#1
Need Help in python code for below conversion:

"()" => "00"
")(" => "-11"
"((a) => "10a0"
"(a(b)c) => "0a0b0c"
"()a((((" => "00a1111"
Reply
#2
what have you tried so far?
Reply
#3
(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:(
Gribouillis write Oct-28-2021, 05:41 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I fixed for you this time. Please use code tags on future posts.
Reply
#4
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
Reply
#5
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020