Why is the following output giving the response instead of what I am expecting?

MATOS

I have the following python code:

a=open("example.nx",'r').read()
b=a.replace("\n","").replace("  ","").split("|")
for i in b: 
    if (len(i)==2 or len(i)==1) and i.__contains__("}") or i.__contains__(")"):
        """doesn't belong is probably a closing bracket"""
        index=b.index(i)
        b[index-1]=b[index-1]+b[index]#add the closing brackets to the one before deleting it
        del b[index]
    """check for brackets opened that aren't closed"""
    if (i.__contains__("(") and not i.__contains__(")")) or (i.__contains__("{") and not i.__contains__("}")):
        #these are not closed
        index=b.index(i)
        if b[index+1].__contains__(")") or b[index+1].__contains__("}"):
            #add them together then delete the extra
            b[index]=b[index]+b[index+1]
            del b[index+1] 
del b[len(b)-1]
print(b)

And I have the file example.nx:

bp myClass(
    myClass[arg1]{
        set myArg to arg1|
    }
)|
def myFunction[arg2,arg3]{
    set a to arg2|
    set b to arg3|
}|
set var1 to 3|
set var2 to 4|
myFunction(var1,var2)|

I get the output of:

['bp myClass(myClass[arg1]{set myArg to arg1})', 'def myFunction[arg2,arg3]{set a to arg2', 'set b to arg3}', 'set var1 to 3', 'set var2 to 4myFunction(var1,var2)']

I was expecting to get(Look at last elements):

['bp myClass(myClass[arg1]{set myArg to arg1})', 'def myFunction[arg2,arg3]{set a to arg2', 'set b to arg3}', 'set var1 to 3', 'set var2 to 4','myFunction(var1,var2)']

I have added breakpoints and have broken the issue down to this block:

if (len(i)==2 or len(i)==1) and i.__contains__("}") or i.__contains__(")"):
        """doesn't belong is probably a closing bracket"""
        index=b.index(i)
        b[index-1]=b[index-1]+b[index]#add the closing brackets to the one before deleting it
        del b[index]

I don't know why this is happening because at the end of the list where the 2 elements merge they don't fit the length requirements so they shouldn't even set off the if statement.

thebjorn

You are missing parenthesis in the first if:

txt = """
bp myClass(
    myClass[arg1]{
        set myArg to arg1|
    }
)|
def myFunction[arg2,arg3]{
    set a to arg2|
    set b to arg3|
}|
set var1 to 3|
set var2 to 4|
myFunction(var1,var2)|
"""
tokens = txt.replace('\n', '').replace("  ","").split("|")

for token in tokens: 
    if (len(token) == 2 or len(token) == 1) and ('}' in token or ')' in token):
        # these are missing                     ^                            ^
        # doesn't belong is probably a closing bracket
        i = tokens.index(token)
        tokens[i - 1] = tokens[i-1] + tokens[i]  # add the closing brackets to the one before deleting it
        del tokens[i]
    # check for brackets opened that aren't closed
    if ('(' in token and ')' not in token) or ('{' in token and '}' not in token):
        # these are not closed
        i = tokens.index(token)
        if ')' in tokens[i+1] or '}' in tokens[i+1]:
            # add them together then delete the extra
            tokens[i] = tokens[i] + tokens[i+1]
            del tokens[i+1] 
del tokens[len(tokens)-1]
print(tokens)

I've also changed your variables around, a and b are not good variable names. i can be a good variable name, if it is used as an integer index into a list.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I not getting output for the following code?

not giving output the way I’m expecting

Output of split is not what I was expecting

Why am I not getting output when I run the following assembly

I am expecting output as int, but getting float

What will be the output of following and why?

Why am I getting 32767 after each output in the following code?

Why the following code block is giving output "ZZZ(some garbage value)" instead of "ZZ(some garbage value)"

explode() is giving me blank values I am not expecting

Why is the console not printing the characters i am expecting

Why am I not able to fetch email with the following code (the second console statement not giving me anything)?

Why I am getting wrong output 2686924 instead of 281?

Why is my regex putting a character in a different capture group than what I am expecting?

Why am I getting this message in hackerrank "~ no response on stdout ~"? I don't know what I am missing>

Why I am not getting change in output when I write the following code in CSS?

Why do following output `$var` instead of `3`?

I am not quite understanding the output of the following function

Why am I not getting a response from my asterisk server with the following twisted SIP python code

Reading csv file in C, strtok not returning what I am expecting?

C++ std::unique is not showing what I am expecting from it

spaCy: Matcher end token offset not what I am expecting

Installed Gnome on Sputnik 16.04, not seeing what I am expecting

What am I doing wrong - syntax error unexpected '\n', expecting =>

java conditional operator,why following code is giving output as true?

Why does my loop not work as I am expecting?

readmore if giving the opposite response to what I expect

What am I doing wrong in the following for loop?

Why i am getting the following error in compilation:

Why am I getting a NullPointerException in the following code?

TOP Ranking

HotTag

Archive