Python compile error


  • 0
    G

    Well, I am a python new user. Recently, I am studying python and want to do some exercise on leetcode. This is my first python code on leetcode. I have test it on my PC in python2 and python3 environment. It works well. However, when I submit it on the leetcode. It warns "compile error". What's wrong with it ? Thank you.

    def reverseWords(self, s):
        global revStr
        revStr = ""
    
        blankIndexList = []
        wordList = []
    
        for i in range( len(s) ):
            if s[i] == ' ':
                continue
    
            s = s[i:]
            break
    
        for i in range( len(s) - 1, -1, -1):
            if s[i] == ' ':
                continue
    
            s = s[:i+1]
            break
        
        for i in range( len(s) - 1 ):
            if s[i] != ' ' and s[i+1] == ' ':
                blankIndexList.append(i+1)
    
            if s[i] == ' ' and s[i+1] != ' ':
                blankIndexList.append(i)
    
        blankListLen = len(blankIndexList)
        revStr += s[ blankIndexList[blankListLen-1]+1:]
        
        for j in range( blankListLen//2 - 2, -1, -1 ):
            revStr += " " 
            revStr += s[ blankIndexList[2*j+1]+1:blankIndexList[2*j+2] ]
    
        revStr += " "
        revStr += s[ : blankIndexList[0] ]
        return revStr

  • 0
    S

    global is forbidden.

    In this case, global is unnecessary.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.