简体   繁体   中英

Nesting depth and check of valid or invalid values

I have a script for counting the paranthese depth of a text. My function counts the depth and checks for open parantheses, and is supposed to return the following tupel: (depth, valid, balanced).

The depth is controlling how many valid parantheses the text contains. Valid checks if there are to many or any closing paranthese missing their counterpart, of if its a negative value. Balanced check controlls if the value is 0 or not.

s = '((This teXt)((is)(five deep((, valid))and))balanced)\t'

te = ''.join(s).lower()

par = ''
for ch in te:
    if ch not in (' ', '\n', ',', '.', '-', '–', '—', '*', 
                  '«', '»', ':', ';', '’', '?', "'", '"', 
                  '/', '!', '…', '´', '`', '+', '[', ']',
                  '0', '1', '2', '3', '4', '5', '6', '7',
                  '8', '9','a', 'b', 'c', 'd', 'e', 'f', 
                  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
                  'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
                  'w', 'x', 'y', 'z', 'æ', 'ø', 'å', '\t'):
        par += ch

def max_dep():
    count = 0
    max_num = 0
    for i in par:
        if i == '(':
            count += 1
            if max_num < count:
               max_num = count
        if i == ')':
            count -= 1
    
    val = 0   
    for t in par:
        if t == '(':
            val += 1
        if t == ')':
            val -= 1
        if val == 0:
            val = True
        else:
            val = False
        
            
    bal = 0
    for x in par:
        if x == '(':
            bal += 1
        if x == ')':
            bal -= 1
        if bal == 0:
            bal = True
        else:
            bal = False
        

    

    
    return max_num, val, bal
    
    
print(max_dep())

Since 'val' = 0 and 'bal' = 0, I was hoping on the print (5, True, True), but as I hvae come to understand, 0 is never True. Is there any hope to get this function to print True for 0 or do I have to start over?

In short: The solution was to pull back the check/ statements from the for-loop.

'''s = '((This teXt)((is)(five deep((, valid))and))balanced)\t'

te = ''.join(s).lower()

par = ''
for ch in te:
    if ch not in (' ', '\n', ',', '.', '-', '–', '—', '*', 
                  '«', '»', ':', ';', '’', '?', "'", '"', 
                  '/', '!', '…', '´', '`', '+', '[', ']',
                  '0', '1', '2', '3', '4', '5', '6', '7',
                  '8', '9','a', 'b', 'c', 'd', 'e', 'f', 
                  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
                  'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
                  'w', 'x', 'y', 'z', 'æ', 'ø', 'å', '\t'):
        par += ch

def max_dep():
    count = 0
    max_num = 0
    for i in par:
        if i == '(':
            count += 1
            if max_num < count:
               max_num = count
        if i == ')':
            count -= 1
    
    val = 0   
    for t in par:
        if t == '(':
            val += 1
        if t == ')':
            val -= 1
   
    
    if val < 0:
        val = False
        
    else:
        val = True
            
    bal = 0
    for x in par:
        if x == '(':
            bal += 1
        if x == ')':
            bal -= 1

    
    
    if bal == 0:
        bal = True
        
    else:
        bal = False
        

    

    
    return max_num, val, bal
    
    
print(max_dep())'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM